示例#1
0
文件: rbm.go 项目: 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
}
示例#2
0
文件: rbm.go 项目: sguzwf/mlf
// 输入和输出都有 bias 项
func (rbm *RBM) SampleHidden(v *util.Vector, n int, binary bool) *util.Vector {
	rbm.lock.RLock()
	defer rbm.lock.RUnlock()
	hiddenDim := rbm.options.NumHiddenUnits + 1
	visibleDim := rbm.lock.weights.NumValues()

	hiddenUnits := util.NewVector(hiddenDim)
	visibleUnits := util.NewVector(visibleDim)
	hiddenUnits.Set(0, 1.0)
	visibleUnits.Set(0, 1.0)

	for j := 1; j < visibleDim; j++ {
		visibleUnits.Set(j, v.Get(j))
	}

	// 更新 hidden units
	for i := 1; i < hiddenDim; i++ {
		prob := rbm.logistic(visibleUnits, i, true)
		if binary {
			hiddenUnits.Set(i, rbm.bernoulli(prob))
		} else {
			hiddenUnits.Set(i, prob)
		}
	}

	// reconstruct n-1 次
	for nCD := 0; nCD < n; nCD++ {
		for j := 1; j < visibleDim; j++ {
			var prob float64
			prob = rbm.logistic(hiddenUnits, j, false)
			visibleUnits.Set(j, prob)
		}
		for i := 1; i < hiddenDim; i++ {
			prob := rbm.logistic(visibleUnits, i, true)
			if binary {
				hiddenUnits.Set(i, rbm.bernoulli(prob))
			} else {
				hiddenUnits.Set(i, prob)
			}
		}
	}

	return hiddenUnits
}
示例#3
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
}