Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement