<jupyter_start><jupyter_text>**[LSE-01]** モジュールをインポートします。<jupyter_code>import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>**[LSE-02]** Placeholder x を定義します。<jupyter_code>x = tf.placeholder(tf.float32, [None, 5])<jupyter_output><empty_output><jupyter_text>**[LSE-03]** Variable w を定義します。<jupyter_code>w = tf.Variable(tf.zeros([5, 1]))<jupyter_output><empty_output><jupyter_text>**[LSE-04]** 計算式 y を定義します。<jupyter_code>y = tf.matmul(x, w)<jupyter_output><empty_output><jupyter_text>**[LSE-05]** Placeholder t を定義します。<jupyter_code>t = tf.placeholder(tf.float32, [None, 1])<jupyter_output><empty_output><jupyter_text>**[LSE-06]** 誤差関数 loss を定義します。<jupyter_code>loss = tf.reduce_sum(tf.square(y-t))<jupyter_output><empty_output><jupyter_text>**[LSE-07]** トレーニングアルゴリズム train_step を定義します。<jupyter_code>train_step = tf.train.AdamOptimizer().minimize(loss)<jupyter_output><empty_output><jupyter_text>**[LSE-08]** セッションを用意して、Variableを初期化します。<jupyter_code>### >>> with GPU 80%
#gpuConfig = tf.ConfigProto(
#    gpu_options = tf.GPUOptions(
#        per_process_gpu_memory_fraction = 0.8),
#    device_count = {'GPU':0})
#sess = tf.Session(config = gpuConfig)
### <<<
#
### >>> with CPU only
sess = tf.Session()
### <<<
sess.run(tf.global_variables_initializer())
### 下記はPython3.6ではエラーとなるので、上記に変更した。
#sess.run(tf.initialize_all_variables())<jupyter_output><empty_output><jupyter_text>**[LSE-09]** トレーニングセットのデータを用意します。<jupyter_code>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<jupyter_output><empty_output><jupyter_text>**[LSE-10]** 勾配降下法によるパラメーターの最適化を100000回繰り返します。<jupyter_code>i = 0
for _ in range(100000):
    i += 1
    sess.run(train_step, feed_dict={x:train_x, t:train_t})
    if i % 10000 == 0:
        loss_val = sess.run(loss, feed_dict={x:train_x, t:train_t})
        print ('Step: %d, Loss: %f' % (i, loss_val))<jupyter_output>Step: 10000, Loss: 31.015018
Step: 20000, Loss: 29.294701
Step: 30000, Loss: 28.025995
Step: 40000, Loss: 27.321125
Step: 50000, Loss: 25.772881
Step: 60000, Loss: 24.780525
Step: 70000, Loss: 23.842400
Step: 80000, Loss: 22.976669
Step: 90000, Loss: 22.178181
Step: 100000, Loss: 42.597778<jupyter_text>**[LSE-11]** さらに100000回繰り返します。<jupyter_code>for _ in range(100000):
    i += 1
    sess.run(train_step, feed_dict={x:train_x, t:train_t})
    if i % 10000 == 0:
        loss_val = sess.run(loss, feed_dict={x:train_x, t:train_t})
        print ('Step: %d, Loss: %f' % (i, loss_val))<jupyter_output>Step: 110000, Loss: 20.753553
Step: 120000, Loss: 20.128706
Step: 130000, Loss: 19.529371
Step: 140000, Loss: 18.985298
Step: 150000, Loss: 18.479834
Step: 160000, Loss: 18.012241
Step: 170000, Loss: 17.579357
Step: 180000, Loss: 17.178448
Step: 190000, Loss: 16.806240
Step: 200000, Loss: 16.461517<jupyter_text>**[LSE-12]** トレーニング後のパラメーターの値を確認します。<jupyter_code>w_val = sess.run(w)
print (w_val)
### 下記はPython3.6ではエラーとなるので、上記に変更した。
#print w_val<jupyter_output>[[ 6.1065474 ]
 [-4.0423927 ]
 [ 2.5106251 ]
 [-0.28170168]
 [ 0.00834867]]<jupyter_text>**[LSE-13]** トレーニング後のパラメーターを用いて、予測気温を計算する関数を定義します。<jupyter_code>def predict(x):
    result = 0.0
    for n in range(0, 5):
        result += w_val[n][0] * x**n
    return result<jupyter_output><empty_output><jupyter_text>**[LSE-14]** 予測気温のグラフを描きます。<jupyter_code>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,100)
liney = predict(linex)
subplot.plot(linex, liney)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup