示例#1
0
func maxEigenvalue(m *linalg.Matrix) float64 {
	inVec := make(linalg.Vector, m.Rows)
	for i := range inVec {
		inVec[i] = rand.NormFloat64()
	}
	inMat := linalg.NewMatrixColumn(inVec)
	for i := 0; i < npPowerIterations; i++ {
		inMat = m.MulFast(inMat)
		vec := linalg.Vector(inMat.Data)
		vec.Scale(1 / vec.Mag())
	}
	outVec := linalg.Vector(m.MulFast(inMat).Data)
	return outVec.Mag()
}
示例#2
0
func rbmEnergy(r *RBM, input, output []bool) float64 {
	inputVec := make(linalg.Vector, len(input))
	for i, x := range input {
		if x {
			inputVec[i] = 1
		}
	}
	outputVec := make(linalg.Vector, len(output))
	for i, x := range output {
		if x {
			outputVec[i] = 1
		}
	}

	energy := inputVec.Dot(r.VisibleBiases)
	energy += outputVec.Dot(r.HiddenBiases)

	inputCol := linalg.NewMatrixColumn(inputVec)
	energy += outputVec.Dot(linalg.Vector(r.Weights.Mul(inputCol).Data))

	return -energy
}