In [35]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
In [40]:
# n points
n=100
x_data = np.random.rand(n).astype(np.float32)
y_data = x_data * 0.1 + 0.3
plt.scatter(y_data,x_data)
Out[40]:
In [7]:
# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
In [44]:
# W is a random value between -1 and 1
# bias is intilaized as '0'
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b
In [46]:
# Specify loss as Mean Square
loss = tf.reduce_mean(tf.square(y - y_data))
# optimizer as
optimizer = tf.train.MomentumOptimizer(0.4,0.1)
### Try different optimizers.
#AdamOptimizer(0.5)
#AdadeltaOptimizer(0.7)
#GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
In [47]:
c = np.array([[3.,4], [5.,6], [6.,7]])
print(np.mean(c,1))
Mean = tf.reduce_mean(c,1)
with tf.Session() as sess:
result = sess.run(Mean)
print(result)
In [29]:
init = tf.initialize_all_variables()
In [30]:
sess = tf.Session()
sess.run(init)
In [31]:
### Learns best fit is W: [0.1], b: [0.3]
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))
In [39]:
plt.plot(x_data, y_data, 'ro', label='Original data')
plt.plot(x_data, sess.run(W) * x_data + sess.run(b), label='Fitted line')
plt.legend()
plt.show()