示例#1
0
// 计算 z = 1 + sum(exp(sum(w_i * x_i)))
//
// 在temp中保存 exp(sum(w_i * x_i))
func ComputeZ(weights *util.Matrix, features *util.Vector, label int, temp *util.Matrix) float64 {
	result := float64(1.0)
	numLabels := weights.NumLabels() + 1

	for iLabel := 1; iLabel < numLabels; iLabel++ {
		exp := math.Exp(util.VecDotProduct(features, weights.GetValues(iLabel-1)))
		result += exp

		tempVec := temp.GetValues(iLabel - 1)
		if tempVec.IsSparse() {
			for _, k := range features.Keys() {
				tempVec.Set(k, exp)
			}
		} else {
			tempVec.SetAll(exp)
		}
	}
	return result
}