0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TensorFlow > sine curveの学習 > v0.2: learn rateの違いによる結果の比較 / ネットワーク図

Last updated at Posted at 2016-11-12
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.

v0.1 http://qiita.com/7of9/items/b364d897b95476a30754

AdamOptimizerの学習係数を変更してみた。

使用するinput.csv

Qiita記事

Network

![](http://yuml.me/diagram/class/ [x]-->[a], [x]-->[b], [x]-->[c], [x]-->[d], [x]-->[0], [x]-->[1], [x]-->[2], [a]-->[e], [a]-->[f], [a]-->[g], [a]-->[h], [a]-->[3], [a]-->[4], [a]-->[5], [b]-->[e], [b]-->[f], [b]-->[g], [b]-->[h], [b]-->[3], [b]-->[4], [b]-->[5], [c]-->[e], [c]-->[f], [c]-->[g], [c]-->[h], [c]-->[3], [c]-->[4], [c]-->[5], [d]-->[e], [d]-->[f], [d]-->[g], [d]-->[h], [d]-->[3], [d]-->[4], [d]-->[5], [0]-->[e], [0]-->[f], [0]-->[g], [0]-->[h], [0]-->[3], [0]-->[4], [0]-->[5], [1]-->[e], [1]-->[f], [1]-->[g], [1]-->[h], [1]-->[3], [1]-->[4], [1]-->[5], [2]-->[e], [2]-->[f], [2]-->[g], [2]-->[h], [2]-->[3], [2]-->[4], [2]-->[5], [e]-->[i], [e]-->[j], [e]-->[k], [e]-->[l], [e]-->[6], [e]-->[7], [e]-->[8], [f]-->[i], [f]-->[j], [f]-->[k], [f]-->[l], [f]-->[6], [f]-->[7], [f]-->[8], [g]-->[i], [g]-->[j], [g]-->[k], [g]-->[l], [g]-->[6], [g]-->[7], [g]-->[8], [h]-->[i], [h]-->[j], [h]-->[k], [h]-->[l], [h]-->[6], [h]-->[7], [h]-->[8], [3]-->[i], [3]-->[j], [3]-->[k], [3]-->[l], [3]-->[6], [3]-->[7], [3]-->[8], [4]-->[i], [4]-->[j], [4]-->[k], [4]-->[l], [4]-->[6], [4]-->[7], [4]-->[8], [5]-->[i], [5]-->[j], [5]-->[k], [5]-->[l], [5]-->[6], [5]-->[7], [5]-->[8], [i]-->[z], [j]-->[z], [k]-->[z], [l]-->[z], [6]-->[z], [7]-->[z], [8]-->[z], )

code

linreg2.py
# !/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf
import tensorflow.contrib.slim as slim

# ファイル名の Queue を作成
filename_queue = tf.train.string_input_producer(["input.csv"])

# CSV を parse
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
input1, output = tf.decode_csv(value, record_defaults=[[0.], [0.]])
inputs = tf.pack([input1])
output = tf.pack([output])

inputs_batch, output_batch = tf.train.shuffle_batch([inputs, output], 4, capacity=40, min_after_dequeue=4)

## NN のグラフ生成
hiddens = slim.stack(inputs_batch, slim.fully_connected, [1,7,7,7], 
  activation_fn=tf.nn.sigmoid, scope="hidden")
prediction = slim.fully_connected(hiddens, 1, activation_fn=tf.nn.sigmoid, scope="output")

loss = tf.contrib.losses.mean_squared_error(prediction, output_batch)
# train_op = slim.learning.create_train_op(loss, tf.train.AdamOptimizer(0.01))
train_op = slim.learning.create_train_op(loss, tf.train.AdamOptimizer(0.001))

init_op = tf.initialize_all_variables()

with tf.Session() as sess:
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(sess=sess, coord=coord)

  try:
    sess.run(init_op)
    for i in range(20000):
      _, t_loss = sess.run([train_op, loss])
      if (i+1) % 100 == 0:
        print("%d,%f" % (i+1, t_loss))
#        print("%d,%f,#step, loss" % (i+1, t_loss))
  finally:
    coord.request_stop()

  coord.join(threads)

結果

train_op = slim.learning.create_train_op(loss, tf.train.AdamOptimizer(0.001))
の行の係数を0.01と0.001とでそれぞれ実行し、以下のファイルを作成した。

  • log.learn0p01
  • log.learn0p001

上記のファイルを読むJupyter用コードは以下。

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('log.learn0p01', delimiter=',')
input1 = data[:,0]
output1 = data[:,1]
data = np.loadtxt('log.learn0p001', delimiter=',')
input2 = data[:,0]
output2 = data[:,1]

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

ax.plot(input1, output1, color='black', linestyle='solid', label='rate=0.01')
ax.plot(input2, output2, color='red', linestyle='solid', label='rate=0.001')

ax.set_title('loss')
ax.set_xlabel('step')
ax.set_ylabel('loss')
ax.grid(True)
ax.legend()
fig.show()

qiita.png

学習係数を変更してもstep20000近辺のlossの値は20%程度はある。

自分が必要としているlossの上限はいくらなのか別途調査する必要あり。lossが20%でも係数の初期値として計算が早くなるのであれば、ここで時間を使うこともないかもしれない。

追加調査

  • QMCを用いて入力データを作成してみた
    • loss値の改善なし
  • prep_data.py内のy_data = np.sin(2*np.pi*x_data) + 0.3 * np.random.rand()の0.3を0.03にしてみた
    • loss値の改善なし
  • ネットの形態を適当に変更してみた
    • loss値の改善なし

qiita.png

RNNやその一形態のLSTMなどに進むか、実際の学習対象のデータ処理に進むか。

tf.train.shuffle_batch()を使っている部分はデータセットから順番に取り出すように変更する方がいいかもしれない。そうしないとQMCの効果が期待できない。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?