Created
February 11, 2015 07:53
-
-
Save nubela/5e917303aa2fb08d0b0a to your computer and use it in GitHub Desktop.
2-layered MLP - xor problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
import numpy as np | |
from pylearn2.datasets.dense_design_matrix import DenseDesignMatrix | |
from pylearn2.models import mlp | |
from pylearn2.termination_criteria import EpochCounter | |
from pylearn2.training_algorithms import sgd | |
import theano | |
def gen_xor_data(): | |
x_input = [[randint(0, 1), randint(0, 1)] for _ in range(1000)] | |
y_target = [] | |
for a, b in x_input: | |
if a + b == 1: | |
y_target += [[1]] | |
else: | |
y_target += [[0]] | |
return x_input, y_target | |
if __name__ == "__main__": | |
# build training data | |
x_input, y_target = gen_xor_data() | |
np_x_input = np.array(x_input) | |
np_y_target = np.array(y_target) | |
dataset = DenseDesignMatrix(X=np_x_input, y=np_y_target) | |
# build network layers | |
hidden_layer = mlp.Sigmoid(layer_name="hidden", dim=2, irange=.1, | |
init_bias=1.) #mlp == multi-layer perceptron / dim == array dimensions | |
output_layer = mlp.Linear(1, "output", irange=.1) | |
layers = [hidden_layer, output_layer] | |
#init trainer - to train layers with stochastic gradient descent (SGD) | |
trainer = sgd.SGD(learning_rate=.05, batch_size=10, termination_criterion=EpochCounter(400)) | |
#begin network | |
neural_network = mlp.MLP(layers, nvis=2) #setup a simple perceptron network with 2 input | |
trainer.setup(neural_network, dataset) #attach trainer to the neural network | |
#begin training the network with dataset | |
print "Begin training network.." | |
while True: | |
trainer.train(dataset=dataset) | |
# neural_network.monitor.report_epoch() | |
# neural_network.monitor() | |
if not trainer.continue_learning(neural_network): | |
print "Training ends." | |
break | |
#test network | |
for _ in range(5): | |
random_input = [randint(0, 1), randint(0, 1)] | |
print "Testing network with %d,%d" % (random_input[0], random_input[1]) | |
np_random_input = np.array([random_input]) | |
print np_random_input | |
print neural_network.fprop(theano.shared(np_random_input, name="inputs")).eval() | |
print "------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment