Exemplo n.º 1
0
func (s LScores) score(l Label) float32 {
	vsc, ok := s[string(l)]
	if !ok {
		return 0
	}
	sc, _ := data.AsFloat(vsc)
	return float32(sc)
}
Exemplo n.º 2
0
func mapToValues(v data.Map) ([]float64, error) {
	values := make([]float64, 0, len(v))
	for _, x := range v {
		val, err := data.AsFloat(x)
		if err != nil {
			return nil, err
		}
		values = append(values, val)
	}
	return values, nil
}
Exemplo n.º 3
0
// maxLabelScore returns the max score and its label in a data.Map.
// This function are same as LScores.Max() except error checking.
func maxLabelScore(scores data.Map) (label string, score float64, err error) {
	if len(scores) == 0 {
		err = errors.New("attempt to find a max score from an empty map")
		return "", 0, err
	}

	score = minusInf
	for l, s := range scores {
		sc, err := data.AsFloat(s)
		if err != nil {
			err = fmt.Errorf("score for %s is not a float: %v", l, err)
			return "", 0, err
		}
		if sc > score {
			label = l
			score = sc
		}
	}

	return label, score, nil
}
Exemplo n.º 4
0
// Softmax calculates softmax.
func Softmax(v data.Map) (data.Map, error) {
	ret := make(data.Map)

	if len(v) == 0 {
		return ret, nil
	}

	// copy values to an array to sort in logSumExp().
	values, err := mapToValues(v)
	if err != nil {
		return nil, err
	}

	lse := logSumExp(values)
	for k, x := range v {
		val, err := data.AsFloat(x)
		if err != nil {
			return nil, err
		}
		ret[k] = data.Float(math.Exp(val - lse))
	}
	return ret, nil
}
Exemplo n.º 5
0
func toFloat(d data.Value) float64 {
	ret, _ := data.AsFloat(d)
	return ret
}