Beispiel #1
0
Datei: rbm.go Projekt: sguzwf/mlf
func (rbm *RBM) logistic(v *util.Vector, index int, isRow bool) (output float64) {
	output = 0.0
	if isRow {
		output = util.VecDotProduct(v, rbm.lock.weights.GetValues(index))
	} else {
		for i := 0; i < rbm.lock.weights.NumLabels(); i++ {
			output += v.Get(i) * rbm.lock.weights.Get(i, index)
		}
	}
	output = 1.0 / (1 + math.Exp(-output))
	return
}
// 计算 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
}