Ejemplo n.º 1
0
func TrainRBM() {
	train_file := "./data_0.dat"
	validation_file := "./data_1.dat"
	class_sizes := []int{4, 2, 5, 1}
	hidden_layer_size := 2

	train_data_accessor := rbm.NewInstanceLoader(train_file, len(class_sizes))
	defer train_data_accessor.Close()

	validation_data_accessor := rbm.NewInstanceLoader(validation_file, len(class_sizes))
	defer validation_data_accessor.Close()

	class_biases, y_bias := rbm.GetBiases(class_sizes, train_data_accessor)
	var rbm_m rbm.SparseClassRBM
	(&rbm_m).Initialize(class_sizes, class_biases, hidden_layer_size, y_bias)

	var trainer rbm.RBMTrainer

	learning_rate := rbm.WeightT(0.001)
	regularization := rbm.WeightT(0.0)
	momentum := rbm.WeightT(0.0)
	gen_learning_imp := rbm.WeightT(0.0)
	gibs_chain_len := 1

	trainer.Initialize(&rbm_m, train_data_accessor, validation_data_accessor,
		learning_rate, regularization, momentum, gen_learning_imp, gibs_chain_len)

	trainer.Train()

	//Evaluate RBM
	//	test_file := "./data_2.dat"
}
Ejemplo n.º 2
0
// Predict makes a prediction with the given input.
func (m *SimpleModel) Predict(x []int, noise rbm.WeightT) rbm.WeightT {
	w := rbm.WeightT(0)
	for i, k := range x {
		w += m.feature_weights[i][k]
	}
	w += noise
	return Sigmoid(w)
}
Ejemplo n.º 3
0
// GenerateInstance generates a data sample form the given model.
func (m *SimpleModel) GenerateInstance() ([]int, int) {
	x := make([]int, len(m.feature_distribution))
	for i, v := range m.feature_distribution {
		k := SelectKFromDist(rbm.WeightT(rand.Float64()), v)
		x[i] = k
	}
	y := 0
	if m.Predict(x, m.noise_std) > m.threshold {
		y = 1
	}

	return x, y
}
Ejemplo n.º 4
0
func Sigmoid(x rbm.WeightT) rbm.WeightT {
	x_exp := math.Exp(float64(x))
	return rbm.WeightT(x_exp / (1 + x_exp))
}