Example #1
0
func rbmExactExpectation(r *RBM, layer []bool, hidden bool) linalg.Vector {
	var normalizer kahan.Summer64
	var outcomeSum []kahan.Summer64
	if hidden {
		outcomeSum = make([]kahan.Summer64, rbmTestHiddenSize)
	} else {
		outcomeSum = make([]kahan.Summer64, rbmTestVisibleSize)
	}

	for i := 0; i < (1 << uint(len(outcomeSum))); i++ {
		variableVec := boolVecFromInt(i, len(outcomeSum))
		var prob float64
		if hidden {
			prob = math.Exp(-rbmEnergy(r, layer, variableVec))
		} else {
			prob = math.Exp(-rbmEnergy(r, variableVec, layer))
		}
		normalizer.Add(prob)
		for j, b := range variableVec {
			if b {
				outcomeSum[j].Add(prob)
			}
		}
	}

	expectation := make(linalg.Vector, len(outcomeSum))
	norm := 1.0 / normalizer.Sum()
	for i, s := range outcomeSum {
		expectation[i] = norm * s.Sum()
	}
	return expectation
}
Example #2
0
// ExpectedHidden returns the expected value of
// the hidden layer given a visible vector.
func (r *RBM) ExpectedHidden(visible []bool) linalg.Vector {
	result := make(linalg.Vector, len(r.HiddenBiases))
	for i := range result {
		var sum kahan.Summer64
		for j, v := range visible {
			if v {
				sum.Add(r.Weights.Get(i, j))
			}
		}
		result[i] = sum.Sum()
	}

	result.Add(r.HiddenBiases)
	mapSigmoid(result)

	return result
}
Example #3
0
// ExpectedVisible returns the expected value of
// the visible layer given a hidden vector.
func (r *RBM) ExpectedVisible(hidden []bool) linalg.Vector {
	result := make(linalg.Vector, len(r.VisibleBiases))
	for i := range result {
		var sum kahan.Summer64
		for j, h := range hidden {
			if h {
				sum.Add(r.Weights.Get(j, i))
			}
		}
		result[i] = sum.Sum()
	}

	result.Add(r.VisibleBiases)
	mapSigmoid(result)

	return result
}