Skip to content

Instantly share code, notes, and snippets.

@Oktai15
Last active April 30, 2018 10:09
Show Gist options
  • Save Oktai15/4b6617b916c0fa4feecab35be09c1bd6 to your computer and use it in GitHub Desktop.
Save Oktai15/4b6617b916c0fa4feecab35be09c1bd6 to your computer and use it in GitHub Desktop.
TensorFlow: simple example
import numpy as np
import tensorflow as tf
data_size = 10
input_size = 28 * 28
hidden1_output = 200
output_size = 1
data = tf.placeholder(tf.float32, shape=(data_size, input_size))
target = tf.placeholder(tf.float32, shape=(data_size, output_size))
h1_w1 = tf.Variable(tf.random_uniform((input_size, hidden1_output)))
h2_w1 = tf.Variable(tf.random_uniform((hidden1_output, output_size)))
hidden1_out = tf.maximum(tf.matmul(data, h1_w1), 0)
target_ = tf.matmul(hidden1_out, h2_w1)
loss = tf.losses.mean_squared_error(target_, target)
opt = tf.train.GradientDescentOptimizer(1e-3)
upd = opt.minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
feed = {data: np.random.randn(data_size, input_size), target: np.random.randn(data_size, output_size)}
for step in range(100):
loss_val, _ = sess.run([loss, upd], feed_dict=feed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment