LoginSignup
0
0

More than 5 years have passed since last update.

windowsでTensorFlow その16

Last updated at Posted at 2017-05-08

概要

windowsでTensorFlowやってみた。
sin(x) を返すニューラルネットワークを作ってみた。

プログラムの手順

学習データの用意

data_x = np.linspace(-3, 3, 20)
data_y = np.sin(data_x) / 2.0 + 0.5

モデルの作成

入力、出力にplaceholder

x_in = tf.placeholder("float", [None, 1])
y_out = tf.placeholder("float", [None, 1])

重みとバイアス用にVariable

W1 = tf.Variable(tf.random_uniform([1, 20]))
W2 = tf.Variable(tf.random_uniform([20, 1]))
b1 = tf.Variable(tf.zeros([20]))
b2 = tf.Variable(tf.zeros([1]))

活性化関数を使う

y1 = tf.nn.sigmoid(tf.matmul(x_in, W1) + b1)
y2 = tf.nn.sigmoid(tf.matmul(y1, W2) + b2)

ロス値を定義

loss = tf.nn.l2_loss(y2 - y_out)

最適化の方法定義

train = tf.train.GradientDescentOptimizer(0.01).minimize(loss)

セッションを定義して学習する。

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    for i in range(100000):
        choice = np.random.randint(0, 20, 4)
        batch_x = data_x[choice].reshape(4, 1)
        batch_y = data_y[choice].reshape(4, 1)
        sess.run(train, feed_dict = {
            x_in: batch_x,
            y_out: batch_y
        })

テストデータで取り出す。

    test_x = np.linspace(-3, 3, 20)
    test_y = sess.run(y2, feed_dict = {
        x_in: test_x.reshape(20, 1)
    })

写真

test8.png

0
0
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
0