Created
August 20, 2019 16:39
-
-
Save Patchyst/e431fb82d45f93513f4c6442a2cbe7fe to your computer and use it in GitHub Desktop.
Linear Regression Neural Network made with Tensor Flow
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 matplotlib.pyplot as plt | |
tf.reset_default_graph() | |
input_node = tf.placeholder(dtype=tf.float32, shape=None) | |
output_node = tf.placeholder(dtype=tf.float32, shape=None) | |
slope = tf.Variable(5.0, dtype=tf.float32) | |
y_intercept = tf.Variable(1.0, dtype=tf.float32) | |
conclusion_operation = slope * input_node + y_intercept | |
error_squared = tf.square(conclusion_operation - output_node) | |
loss = tf.reduce_mean(error_squared) | |
init = tf.global_variables_initializer() | |
x_values = [2, 4, 6, 8, 10] | |
y_values = [1, 3, 5, 7, 9] | |
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) | |
train = optimizer.minimize(loss) | |
with tf.Session() as sess: | |
sess.run(init) | |
for i in range(2500): | |
sess.run(train, feed_dict={input_node: x_values, output_node: y_values}) | |
if i % 100 == 0: | |
print(sess.run([slope, y_intercept])) | |
plt.plot(x_values, sess.run(conclusion_operation, feed_dict={input_node: x_values})) | |
print(sess.run(loss, feed_dict={input_node: x_values, output_node: y_values})) | |
plt.plot(x_values, y_values, 'ro', 'Expected Output') | |
plt.plot(x_values, sess.run(conclusion_operation, feed_dict={input_node: x_values})) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment