Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Created June 28, 2015 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinThoma/571b6932e5826048fb9c to your computer and use it in GitHub Desktop.
Save MartinThoma/571b6932e5826048fb9c to your computer and use it in GitHub Desktop.
Give matrix input
#!/usr/bin/env python
"""Neural Network to decide for numbers in 0..15 if they are divisble by
2, 3, both or none.
"""
import lasagne
from lasagne import layers
from lasagne.updates import nesterov_momentum
from nolearn.lasagne import NeuralNet
import numpy
def nn_example():
feature_vectors, labels = [], []
for i in range(2):
for j in range(2):
for k in range(2):
for l in range(2):
feature_vectors.append([i, j, k, l])
sum_val = 2**0 * i + 2**1 * j + 2**2 * k + 2**3 * l
labels.append([sum_val % 2 == 0, sum_val % 3 == 0])
feature_vectors = numpy.array(feature_vectors, dtype=numpy.float32)
labels = numpy.array(labels, dtype=numpy.int32)
print(labels)
net1 = NeuralNet(layers=[('input', layers.InputLayer),
('hidden', layers.DenseLayer),
('hidden2', layers.DenseLayer),
('output', layers.DenseLayer),
],
# layer parameters:
input_shape=(None, 4),
hidden_num_units=3,
hidden2_num_units=2,
output_nonlinearity=lasagne.nonlinearities.sigmoid,
output_num_units=2,
# optimization method:
update=nesterov_momentum,
update_learning_rate=0.01,
update_momentum=0.9,
max_epochs=1000,
verbose=1,
)
# Train the network
net1.fit(feature_vectors, labels)
# Try the network
print("Predicted: %s" % str(net1.predict_proba([[0, 0, 1, 0]])))
if __name__ == '__main__':
nn_example()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment