Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import numpy as np
  2. import theano; import theano.tensor as T; import theano.tensor.nnet as nnet
  3. X_train = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).reshape((4, 2))
  4. y_train = np.array([0, 1, 1, 0])
  5. x = T.dvector('x')
  6. y = T.dscalar('y')
  7. theta_0 = theano.shared(np.array(np.random.rand(3, 3), dtype=theano.config.floatX), name='theta_0')
  8. theta_1 = theano.shared(np.array(np.random.rand(4, 1), dtype=theano.config.floatX), name='theta_1')
  9. layer_1 = nnet.sigmoid(T.dot(theta_0.T, T.concatenate([np.array([1], dtype=theano.config.floatX), x])))
  10. layer_2 = T.sum(nnet.sigmoid(T.dot(theta_1.T, T.concatenate([np.array([1], dtype=theano.config.floatX), layer_1]))))
  11. cost_function = (layer_2 - y) ** 2
  12. train = theano.function(inputs=[x, y], outputs=cost_function, updates=[
  13. (theta_0, theta_0 - (0.1 * T.grad(cost_function, wrt=theta_0))),
  14. (theta_1, theta_1 - (0.1 * T.grad(cost_function, wrt=theta_1)))])
  15. predict = theano.function(inputs=[x], outputs=layer_2)
  16. for i in range(10000):
  17. for k in range(X_train.shape[0]):
  18. train(X_train[k], y_train[k])
  19. print(predict([0,1])); print(predict([1,1])); print(predict([1,0])); print(predict([0,0]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement