Skip to content

Instantly share code, notes, and snippets.

@khanhnamle1994
Last active March 7, 2018 03:24
Show Gist options
  • Select an option

  • Save khanhnamle1994/ae50acf1173ff37777720efeb02653d4 to your computer and use it in GitHub Desktop.

Select an option

Save khanhnamle1994/ae50acf1173ff37777720efeb02653d4 to your computer and use it in GitHub Desktop.
import numpy as np
import tensorflow as tf
# Batch size = 32, Input Dimension = 500, Hidden Dimension = 50
# Define computational graph
# Create placeholders
x = tf.placeholder(tf.float32, shape=(32, 500))
y = tf.placeholder(tf.float32, shape=(32, 500))
w1 = tf.placeholer(tf.float32, shape=(500, 50))
w2 = tf.placeholder(tf.float32, shape=(50, 500))
# Forward pass
h = tf.maximum(tf.matmul(x, w1), 0)
y_pred = tf.matmul(h, w2)
diff = y_pred - y
# Use predefined common losses
loss = tf.losses.mean_squared_error(y_pred, y)
# Compute loss of gradient
grad_w1, grad_w2 = tf.gradients(loss, [w1, w2])
# Run the graph multiple times
with tf.Session() as sess:
# Create numpy arrays
values = {x: np.random.randn(32, 500),
w1: np.random.randn(500, 50),
w2: np.random.randn(50, 500),
y: np.random.randn(32, 500),}
# Train the network
learning_rate = 1e-5
# Run the graph in a loop
for t in range(20):
out = sess.run([loss, grad_w1, grad_w2], feed_dict=values)
loss_val, grad_w1_val, grad_w2_val = out
values[w1] -= learning_rate * grad_w1_val
values[w2] -= learning_rate * grad_w2_val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment