Created
December 31, 2016 07:40
-
-
Save meowmiau/369393f41b679dd95f4ac4e2e16b0782 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 tensorflow as tf | |
import numpy as np | |
def F(x): | |
return x * 3.0 + 4.0 | |
def gen_data(n_batch = 10): | |
ret_x = [] | |
ret_y = [] | |
for _ in range(n_batch): | |
x = np.random.random() | |
y = F(x) | |
ret_x.append(x) | |
ret_y.append(y) | |
return np.array(ret_x, np.float32), np.array(ret_y, np.float32) | |
print gen_data() | |
tf_x = tf.placeholder(tf.float32, [10]) | |
tf_y = tf.placeholder(tf.float32, [10]) | |
theta1 = tf.Variable(tf.truncated_normal([1], stddev=0.1)) | |
theta2 = tf.Variable(tf.truncated_normal([1], stddev=0.1)) | |
pred_y = theta1 * tf_x + theta2 | |
err = tf.reduce_mean(tf.square(pred_y - tf_y)) | |
optimizer = tf.train.GradientDescentOptimizer(0.01) | |
train = optimizer.minimize(err) | |
# Launch the graph. | |
init = tf.initialize_all_variables() | |
sess = tf.Session() | |
sess.run(init) | |
x_data, y_data = gen_data() | |
print x_data | |
print x_data.shape | |
print tf_x.get_shape() | |
for i in range(1000): | |
x_data, y_data = gen_data() | |
sess.run(train, feed_dict={tf_x: x_data, tf_y: y_data}) | |
print sess.run(err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment