예측값과 관측값의 제곱 오차를 최소로 하도록 파라메터를 결정하는 '최소 제곱법'
월별 기온 그래프(특징1 -> 다항식추가해서 특징5개)
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = tf.placeholder(tf.float32, [None, 5])
w = tf.Variable(tf.zeros([5, 1]))
y = tf.matmul(x, w)
t = tf.placeholder(tf.float32, [None, 1])
loss = tf.reduce_sum(tf.square(y-t))
train_step = tf.train.AdamOptimizer().minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# 트레이닝 세트 데이터를 준비한다.
train_t = np.array([5.2, 5.7, 8.6, 14.9, 18.2, 20.4,
25.5, 26.4, 22.8, 17.5, 11.1, 6.6])
train_t = train_t.reshape([12,1])
train_x = np.zeros([12, 5])
for row, month in enumerate(range(1, 13)):
for col, n in enumerate(range(0, 5)):
train_x[row][col] = month**n
i = 0
for _ in range(10000):
i += 1
sess.run(train_step, feed_dict={x:train_x, t:train_t})
if i % 1000 == 0:
loss_val = sess.run(loss, feed_dict={x:train_x, t:train_t})
print ('Step: %d, Loss: %f' % (i, loss_val))
w_val = sess.run(w)
print(w_val)
def predict(x):
result = 0.0
for n in range(0, 5):
result += w_val[n][0] * x**n
return result
fig = plt.figure()
subplot = fig.add_subplot(1,1,1)
subplot.set_xlim(1,12)
subplot.scatter(range(1,13), train_t)
linex = np.linspace(1,12,12) # 12개
liney = predict(linex)
subplot.plot(linex, liney)
'python 및 머신러닝 > 텐서플로로 시작하는 딥러닝' 카테고리의 다른 글
| 텐서플로로 시작하는 딥러닝 4장 - 은닉층의 역할 (0) | 2017.10.10 |
|---|---|
| 텐서플로로 시작하는 딥러닝 4장 - 합성곱 필터 (0) | 2017.09.26 |
| 텐서플로로 시작하는 딥러닝 3장 (0) | 2017.09.25 |