Exemple #1
-1
// Forward performs a forward transfer algorithm of Neural network
// and returns the output.
func (net *NeuralNetwork) Forward(input []float64) []float64 {
	output := make([]float64, len(net.OutputLayer))

	if len(input)+1 != len(net.InputLayer) {
		panic("Dimention doesn't match: The number units of input layer")
	}

	// Copy
	for i := range input {
		net.InputLayer[i] = input[i]
	}
	net.InputLayer[len(net.InputLayer)-1] = Bias

	// Transfer to hidden layer from input layer
	for i := 0; i < len(net.HiddenLayer)-1; i++ {
		sum := 0.0
		for j := range net.InputLayer {
			sum += net.HiddenWeight[j][i] * net.InputLayer[j]
		}
		net.HiddenLayer[i] = nnet.Sigmoid(sum)
	}
	net.HiddenLayer[len(net.HiddenLayer)-1] = Bias

	// Transfer to output layer from hidden layer
	for i := 0; i < len(net.OutputLayer); i++ {
		sum := 0.0
		for j := range net.HiddenLayer {
			sum += net.OutputWeight[j][i] * net.HiddenLayer[j]
		}
		output[i] = nnet.Sigmoid(sum)
	}
	net.OutputLayer = output

	return output
}
Exemple #2
-1
// P_H_Given_V returns p(h=1|v), the conditinal probability of activation
// of a hidden unit given a set of visible units.
func (rbm *GBRBM) P_H_Given_V(hiddenIndex int, v []float64) float64 {
	sum := 0.0
	for j := 0; j < rbm.NumVisibleUnits; j++ {
		sum += rbm.W[hiddenIndex][j] * v[j]
	}
	return nnet.Sigmoid(sum + rbm.C[hiddenIndex])
}
Exemple #3
-1
// PseudoLogLikelihood returns pseudo log-likelihood for a given input sample.
func (rbm *RBM) PseudoLogLikelihoodForOneSample(v []float64) float64 {
	bitIndex := rand.Intn(len(v))
	fe := rbm.FreeEnergy(v)
	feFlip := rbm.FreeEnergy(flip(v, bitIndex))
	cost := float64(rbm.NumVisibleUnits) * math.Log(nnet.Sigmoid(feFlip-fe))
	return cost
}
Exemple #4
-1
// P_V_Given_H returns p(v=1|h) the conditinal probability of activation
// of a visible unit given a set of hidden units.
func (rbm *RBM) P_V_Given_H(visibleIndex int, h []float64) float64 {
	sum := 0.0
	for i := 0; i < rbm.NumHiddenUnits; i++ {
		sum += rbm.W[i][visibleIndex] * h[i]
	}
	return nnet.Sigmoid(sum + rbm.B[visibleIndex])
}