Created
March 7, 2018 03:23
-
-
Save khanhnamle1994/eeb55d8994406f2c351dd768438934ef 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 theano | |
import theano.tensor as T | |
# Batch size = 32, Input Dimension = 500, Hidden Dimension = 50, Number of Classes = 5 | |
# Define symbolic variables | |
x = T.matrix('x') | |
y = T.vector('y', dtype='int64') | |
w1 = T.matrix('w1') | |
w2 = T.matrix('w2') | |
# Forward pass: compute scores | |
a = x.dot(w1) | |
a_relu = T.nnet.relu(a) | |
scores = a_relu.dot(w2) | |
# Forward pass: compute softmax loss | |
probs = T.nnet.softmax(scores) | |
loss = T.nnet.categorical_crossentropy(probs, y).mean() | |
# Backward pass: compute gradients | |
dw1, dw2 = T.grad(loss, [w1, w2]) | |
# Compile function | |
f = theano.function( | |
inputs = [x, y, w1, w2], | |
outputs = [loss, scores, dw1, dw2], | |
) | |
# Run the function | |
xx = np.random.rand(32, 500) | |
yy = np.random.randint(5, size=32) | |
ww1 = 1e-2 * np.random.randn(500, 50) | |
ww2 = 1e-2 * np.random.randn(50, 5) | |
learning_rate = 1e-1 | |
for t in xrange(20): | |
loss, scores, dww1, dww2 = f(xx, yy, ww1, ww2) | |
print loss | |
ww1 -= learning_rate * dww1 | |
ww2 -= learning_rate * dww2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment