Last active
June 19, 2017 15:06
-
-
Save gunnar2k/f93f4aa6a796f7f6d2282d151bd30991 to your computer and use it in GitHub Desktop.
Regression
This file contains hidden or 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 | |
learning_rate = 0.00475 | |
x_ph = tf.placeholder("float") | |
y_ph = tf.placeholder("float") | |
w = tf.Variable(0.0, name="weights") | |
b = tf.Variable(0.0, name="b") | |
init_op = tf.global_variables_initializer() | |
y_model = tf.nn.xw_plus_b(tf.expand_dims([x_ph], 0), tf.expand_dims([w], 0), [b]) # tf.add(tf.multiply(x_ph, w), b) | |
cost = tf.reduce_mean(tf.square(y_ph - y_model)) | |
cost_summary = tf.summary.scalar("cost", cost) | |
my_optimizer = tf.train.GradientDescentOptimizer(learning_rate) | |
train_op = my_optimizer.minimize(cost) | |
with tf.Session() as sess: | |
sess.run(init_op) | |
my_writer = tf.summary.FileWriter("./logs", sess.graph) | |
data = {x_ph: 2.1, y_ph: 5.0} | |
sess.run(train_op, feed_dict=data) | |
summary = sess.run(cost_summary, feed_dict=data) | |
my_writer.add_summary(summary, 1) | |
data = {x_ph: 3., y_ph: 7.1} | |
sess.run(train_op, feed_dict=data) | |
summary = sess.run(cost_summary, feed_dict=data) | |
my_writer.add_summary(summary, 2) | |
data = {x_ph: 10, y_ph: 21} | |
sess.run(train_op, feed_dict=data) | |
summary = sess.run(cost_summary, feed_dict=data) | |
my_writer.add_summary(summary, 3) | |
w_val = sess.run(w) | |
b_val = sess.run(b) | |
print(w_val, b_val) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment