各所からコピーしてやってみた Tensorflow 単回帰 matplotlibグラフ描画
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
plt.style.use("ggplot")
import numpy as np
import matplotlib.pyplot as plt
plt.style.use("ggplot")
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
#x_data = np.random.rand(100).astype(np.float32)
#y_data = x_data * 0.1 + 0.3
#x_data = np.random.rand(100).astype(np.float32)
#y_data = x_data * 0.1 + 0.3
x_data = [177,153,173,183,170,160,189]
y_data = [77,57,83,83,65,66,91]
y_data = [77,57,83,83,65,66,91]
# 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.)
#W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
#b = tf.Variable(tf.zeros([1]))
W = tf.Variable([-0.0], tf.float32)
b = tf.Variable([-0.0], tf.float32)
y = W * x_data + b
# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.000033)
train = optimizer.minimize(loss)
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.000033)
train = optimizer.minimize(loss)
# Before starting, initialize the variables. We will 'run' this first.
init = tf.global_variables_initializer()
init = tf.global_variables_initializer()
# Launch the graph.
sess = tf.Session()
sess.run(init)
sess = tf.Session()
sess.run(init)
# Fit the line.
#for step in range(10000001):
# sess.run(train)
# if step % 1000000 == 0:
# print(step, sess.run(W), sess.run(b))
#for step in range(10000001):
# sess.run(train)
# if step % 1000000 == 0:
# print(step, sess.run(W), sess.run(b))
for step in range(10000001):
sess.run(train)
if step % 1000000 == 0:
print(step, sess.run(W), sess.run(b))
sess.run(train)
if step % 1000000 == 0:
print(step, sess.run(W), sess.run(b))
x_predict = [0, 200]
plt.scatter(x_data, y_data, label='Height Weght')
plt.plot(x_predict, sess.run(W) * x_predict + sess.run(b), label='Fitted line')
plt.xlim(0, 195)
plt.ylim(-100, 100)
plt.legend()
plt.show()
plt.scatter(x_data, y_data, label='Height Weght')
plt.plot(x_predict, sess.run(W) * x_predict + sess.run(b), label='Fitted line')
plt.xlim(0, 195)
plt.ylim(-100, 100)
plt.legend()
plt.show()
# Close the Session when we're done.
#sess.close()
#sess.close()
参考にさせてもらったページ
http://tensorflow.classcat.com/2017/03/19/tensorflow-get-started-get-started/
