Help us understand the problem. What is going on with this article?

TensorFlow > TFRecords読込み版sine curveの学習 v0.5 > Error:[Attempting to use uninitialized value hidden/hidden_1/weights] > init_opの定義位置の問題

More than 3 years have passed since last update.
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.1.0
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

TFRecords読込み版sine curveの学習を行う過程で関連技術を学ぶ。

v0.1: http://qiita.com/7of9/items/ab27432caedbfb301650
前回: http://qiita.com/7of9/items/f8c15262c08b9562f2d9

v0.5

以下のコードでエラーが出る。

learn_sineCurve_170708.py
import numpy as np
import tensorflow as tf
import tensorflow.contrib.slim as slim

"""
v0.5 Jul. 09, 2017
  - 
v0.4 Jul. 09, 2017
  - fix bug > stops only for one epoch
    + set [num_epochs=None] for string_input_producer()
  - change parameters for shuffle_batch()
  - implement training
v0.3 Jul. 09, 2017
  - fix warning > use tf.local_variables_initializer() instead of initialize_local_variables()
  - fix warning > use tf.global_variables_initializer() instead of initialize_all_variables()
v0.2 Jul. 08, 2017
  - fix bug > OutOfRangeError (current size 0)
    + use [tf.initialize_local_variables()]
v0.1 Jul. 08, 2017
  - only read [.tfrecords]
    + add inputs_xy()
    + add read_and_decode()
"""

# codingrule: PEP8

IN_FILE = 'input_170708.tfrecords'


def read_and_decode(filename_queue):
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
        serialized_example,
        features={
           'x_raw': tf.FixedLenFeature([], tf.string),
           'y_raw': tf.FixedLenFeature([], tf.string),
        })
    x_raw = tf.decode_raw(features['x_raw'], tf.float32)
    y_raw = tf.decode_raw(features['y_raw'], tf.float32)

    x_org = tf.reshape(x_raw, [1])
    y_org = tf.reshape(y_raw, [1])

    return x_org, y_org


def inputs_xy():
    filename = IN_FILE
    filequeue = tf.train.string_input_producer(
        [filename], num_epochs=None)

    x_org, y_org = read_and_decode(filequeue)
    return x_org, y_org

x_orgs, y_orgs = inputs_xy()
batch_size = 1  # 4
x_batch, y_batch = tf.train.shuffle_batch([x_orgs, y_orgs],
                                          batch_size,
                                          capacity=40,
                                          min_after_dequeue=batch_size)

# init_op = [tf.initialize_all_variables(), tf.initialize_local_variables()]
init_op = [tf.global_variables_initializer(), tf.local_variables_initializer()]

input_ph = tf.placeholder("float", [None,1])
output_ph = tf.placeholder("float",[None,1])

## network
hiddens = slim.stack(input_ph, slim.fully_connected, [7,7,7], 
  activation_fn=tf.nn.sigmoid, scope="hidden")
#prediction = slim.fully_connected(hiddens, 1, activation_fn=tf.nn.sigmoid, scope="output")
prediction = slim.fully_connected(hiddens, 1, activation_fn=None, scope="output")
loss = tf.contrib.losses.mean_squared_error(prediction, output_ph)

train_op = slim.learning.create_train_op(loss, tf.train.AdamOptimizer(0.001))

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

    try:
        for idx in range(2):
            inpbt, outbt = sess.run([x_batch, y_batch])
            print("line87")
            _, _t_loss = sess.run([train_op, loss], 
                                  feed_dict={input_ph:inpbt, output_ph: outbt})
            print("line90")
            if (idx + 1) % 100 == 0:
                print("%d,%f" % (idx+1, t_loss))
    finally:
        coord.request_stop()

問題点

init_op = [tf.global_variables_initializer(), tf.local_variables_initializer()]

の実装をネットワーク構築前に書いていた。

sess.run(init_op)を実施していないので、いいかと思っていたがtrain_op定義より下にする必要があるようだ。

train_op = slim.learning.create_train_op(loss, tf.train.AdamOptimizer(0.001))

# init_op = [tf.initialize_all_variables(), tf.initialize_local_variables()]
init_op = [tf.global_variables_initializer(), tf.local_variables_initializer()]

修正後コード v0.5

learn_sineCurve_170708.py
import numpy as np
import tensorflow as tf
import tensorflow.contrib.slim as slim

"""
v0.5 Jul. 09, 2017
  - fix bug > [Attempting to use uninitialized value hidden/hidden_1/weights]
v0.4 Jul. 09, 2017
  - fix bug > stops only for one epoch
    + set [num_epochs=None] for string_input_producer()
  - change parameters for shuffle_batch()
  - implement training
v0.3 Jul. 09, 2017
  - fix warning > use tf.local_variables_initializer() instead of initialize_local_variables()
  - fix warning > use tf.global_variables_initializer() instead of initialize_all_variables()
v0.2 Jul. 08, 2017
  - fix bug > OutOfRangeError (current size 0)
    + use [tf.initialize_local_variables()]
v0.1 Jul. 08, 2017
  - only read [.tfrecords]
    + add inputs_xy()
    + add read_and_decode()
"""

# codingrule: PEP8

IN_FILE = 'input_170708.tfrecords'


def read_and_decode(filename_queue):
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
        serialized_example,
        features={
           'x_raw': tf.FixedLenFeature([], tf.string),
           'y_raw': tf.FixedLenFeature([], tf.string),
        })
    x_raw = tf.decode_raw(features['x_raw'], tf.float32)
    y_raw = tf.decode_raw(features['y_raw'], tf.float32)

    x_org = tf.reshape(x_raw, [1])
    y_org = tf.reshape(y_raw, [1])

    return x_org, y_org


def inputs_xy():
    filename = IN_FILE
    filequeue = tf.train.string_input_producer(
        [filename], num_epochs=None)

    x_org, y_org = read_and_decode(filequeue)
    return x_org, y_org

x_orgs, y_orgs = inputs_xy()
batch_size = 1  # 4
x_batch, y_batch = tf.train.shuffle_batch([x_orgs, y_orgs],
                                          batch_size,
                                          capacity=40,
                                          min_after_dequeue=batch_size)

input_ph = tf.placeholder("float", [None,1])
output_ph = tf.placeholder("float",[None,1])

## network
hiddens = slim.stack(input_ph, slim.fully_connected, [7,7,7], 
  activation_fn=tf.nn.sigmoid, scope="hidden")
#prediction = slim.fully_connected(hiddens, 1, activation_fn=tf.nn.sigmoid, scope="output")
prediction = slim.fully_connected(hiddens, 1, activation_fn=None, scope="output")
loss = tf.contrib.losses.mean_squared_error(prediction, output_ph)

train_op = slim.learning.create_train_op(loss, tf.train.AdamOptimizer(0.001))

# init_op = [tf.initialize_all_variables(), tf.initialize_local_variables()]
init_op = [tf.global_variables_initializer(), tf.local_variables_initializer()]

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

    try:
        for idx in range(30000):
            inpbt, outbt = sess.run([x_batch, y_batch])
            _, t_loss = sess.run([train_op, loss], 
                                  feed_dict={input_ph:inpbt, output_ph: outbt})
            if (idx + 1) % 100 == 0:
                print("%d,%f" % (idx+1, t_loss))
    finally:
        coord.request_stop()

$ python3 learn_sineCurve_170708.py
...
2017-07-09 09:01:50.796304: I tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0 
2017-07-09 09:01:50.796324: I tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0:   Y 
2017-07-09 09:01:50.796332: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1070, pci bus id: 0000:01:00.0)
100,0.702910
200,0.413344
300,0.707156
400,0.010691
500,0.015716
600,0.030282
700,0.711572
...

動き出したが、きちんと学習しているかは未確認。

7of9
セブンオブナインです。Unimatrix 01の第三付属物 9の7という識別番号です。Star trek Voyagerの好きなキャラクターです。まとめ記事は後日タイトルから内容がわからなくなるため、title検索で見つかるよう個々の記事にしてます。いわゆるBorg集合体の有名なセリフから「お前たち(の知識)を吸収する。抵抗は無意味だ」。Thanks in advance.
qiitadon
Qiitadon(β)から生まれた Qiita ユーザー・コミュニティです。
https://qiitadon.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away