Created
December 5, 2016 20:27
-
-
Save paulhendricks/c38e9d0c4940c0472857d5ed63442519 to your computer and use it in GitHub Desktop.
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
import numpy as np | |
import theano; import theano.tensor as T; import theano.tensor.nnet as nnet | |
X_train = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2)) | |
y_train = np.array([0, 1, 1, 0]) | |
x = T.dvector('x') | |
y = T.dscalar('y') | |
theta_0 = theano.shared(np.array(np.random.rand(3, 3), dtype=theano.config.floatX), name='theta_0') | |
theta_1 = theano.shared(np.array(np.random.rand(4, 1), dtype=theano.config.floatX), name='theta_1') | |
layer_1 = nnet.sigmoid(T.dot(theta_0.T, T.concatenate([np.array([1], dtype=theano.config.floatX), x]))) | |
layer_2 = T.sum(nnet.sigmoid(T.dot(theta_1.T, T.concatenate([np.array([1], dtype=theano.config.floatX), layer_1])))) | |
cost_function = (layer_2 - y) ** 2 | |
train = theano.function(inputs=[x, y], outputs=cost_function, updates=[ | |
(theta_0, theta_0 - (0.1 * T.grad(cost_function, wrt=theta_0))), | |
(theta_1, theta_1 - (0.1 * T.grad(cost_function, wrt=theta_1)))]) | |
predict = theano.function(inputs=[x], outputs=layer_2) | |
for i in range(10000): | |
for k in range(X_train.shape[0]): | |
train(X_train[k], y_train[k]) | |
print(predict([0,1])); print(predict([1,1])); print(predict([1,0])); print(predict([0,0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment