Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
x_train = np.linspace(0, 10, 100)
y_train = x_train + np.random.normal(0,1,100)
learning_rate = 0.01
training_epoches = 100
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
w0 = tf.Variable(0.0, name="W0")
w1 = tf.Variable(0.0, name="W1")
# f(xi) = xi*w1 + w0
def f(X, w1, w0):
return tf.add(tf.multiply(X, w1), w0)
f_xi = f(X, w1, w0)
# loss function
lossF = tf.square(Y-f_xi)
train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(lossF)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for epoch in range(training_epoches):
for (x, y) in zip(x_train, y_train):
sess.run(train_op, feed_dict={X: x, Y: y})
w_val_0 = sess.run(w0)
w_val_1 = sess.run(w1)
sess.close()
plt.scatter(x_train, y_train)
y_learned = x_train*w_val_1 + w_val_0
plt.plot(x_train, y_learned, 'r')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.