Skip to content

Instantly share code, notes, and snippets.

@leechanwoo
Created August 23, 2017 04:53
Show Gist options
  • Save leechanwoo/725d7005844ed06c2082306fefa7fe06 to your computer and use it in GitHub Desktop.
Save leechanwoo/725d7005844ed06c2082306fefa7fe06 to your computer and use it in GitHub Desktop.
multicampus linear regression example
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x = tf.placeholder(tf.int32)
a = tf.Variable(5)
b = tf.Variable(2)
y = a*x + b
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
result = sess.run(y, {x: data})
print(result)
plt.plot(result)
samples = 1000
data = tf.constant([i for i in range(samples)], tf.float32)
label = 0.2 * data + 2.4 + tf.random_normal(shape=[samples,], stddev=70)
target = 0.2 * data + 2.4
with tf.Session() as sess:
_data, _label, _target = sess.run([data, label, target])
plt.scatter(_data, _label, 1)
plt.scatter(_data, _target, 1)
x = tf.placeholder(tf.float32)
y_ = tf.placeholder(tf.float32)
weight = tf.Variable(tf.truncated_normal(shape=[1,]))
bias = tf.Variable(tf.zeros(1))
y = x * weight + bias
loss = tf.losses.mean_squared_error(y_, y)
train_op = tf.train.GradientDescentOptimizer(1e-6).minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
losses = []
for i in range(10):
_, _loss = sess.run([train_op, loss], {x: _data, y_: _label})
losses.append(_loss)
print(_loss)
pred = sess.run(y, {x:_data})
plt.plot(losses)
plt.scatter(_data, pred, 1)
plt.scatter(_data, _target, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment