MomentumOptimizer错误:尝试使用未初始化的值Variable_2 / Momentum

时间:2017-04-03 15:41:36

标签: tensorflow

我正在学习TensorFlow。我正在尝试tf.train.MomentumOptimizer,但我收到以下错误:

Traceback (most recent call last):
  File "relu.py", line 98, in <module>
    learner.run(stop=0.01, print_epoch=True)
  File "relu.py", line 70, in run
    self.sess.run(train_step, feed_dict={self.x: batch_xs, self.y_: batch_ys})
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 767, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 965, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1015, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1035, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value Variable_2/Momentum
     [[Node: Momentum/update_Variable_2/ApplyMomentum = ApplyMomentum[T=DT_FLOAT, _class=["loc:@Variable_2"], use_locking=false, use_nesterov=false, _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_2, Variable_2/Momentum, Momentum/learning_rate, gradients/add_1_grad/tuple/control_dependency_1, Momentum/momentum)]]

Caused by op u'Momentum/update_Variable_2/ApplyMomentum', defined at:
  File "relu.py", line 98, in <module>
    learner.run(stop=0.01, print_epoch=True)
  File "relu.py", line 55, in run
    train_step = self.optimizer.minimize(self.cross_entropy)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 289, in minimize
    name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 413, in apply_gradients
    update_ops.append(processor.update_op(self, grad))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 61, in update_op
    return optimizer._apply_dense(g, self._v)  # pylint: disable=protected-access
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/momentum.py", line 69, in _apply_dense
    use_nesterov=self._use_nesterov).op
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/gen_training_ops.py", line 348, in apply_momentum
    use_nesterov=use_nesterov, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2327, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1226, in __init__
    self._traceback = _extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable_2/Momentum
     [[Node: Momentum/update_Variable_2/ApplyMomentum = ApplyMomentum[T=DT_FLOAT, _class=["loc:@Variable_2"], use_locking=false, use_nesterov=false, _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_2, Variable_2/Momentum, Momentum/learning_rate, gradients/add_1_grad/tuple/control_dependency_1, Momentum/momentum)]]

以下是我的代码:

import time
import numpy as np
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data

class ReluMnistNet:

    def __init__(self, optimizer=None):
        self.varlist = []
        self.optimizer = optimizer or tf.train.GradientDescentOptimizer(0.01)

        # fetch dataset

        self.mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

        # prepare environment
        layers = [ 100 ]
        input_layer = 784
        output_layer = 10

        self.x = tf.placeholder(tf.float32, [None, input_layer])
        last_layer = input_layer
        y = self.x
        for layer in layers:
            b = tf.Variable(tf.zeros([layer]))
            self.varlist.append(b)
            W = tf.Variable(tf.random_normal([last_layer,layer], stddev=0.01))
            self.varlist.append(W)
            y = tf.nn.relu( tf.matmul(y,W) ) + b
            last_layer = layer
        b = tf.Variable(tf.zeros([output_layer]))
        self.varlist.append(b)
        W = tf.Variable(tf.random_normal([last_layer,output_layer], stddev=0.01))
        self.varlist.append(W)
        self.y = tf.matmul(y,W) + b

        self.y_ = tf.placeholder(tf.float32, [None, 10])

        self.cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=self.y, labels=self.y_) )

    def prepare(self):
        # init = tf.initialize_variables(self.varlist)
        init = tf.initialize_all_variables()

        self.sess = tf.Session()
        self.sess.run(init)

    def run(self, batch_size=100, stop=0.001, print_epoch=False):
        mnist = self.mnist
        data_size = mnist.train.images.shape[0]

        last_accuracy = 0

        accuracy_history = []
        train_step = self.optimizer.minimize(self.cross_entropy)

        time1 = time.time()
        for i in range(10000):
            for j in range(data_size/batch_size):
                # random batch
                batch_idx = np.arange(data_size)
                np.random.shuffle(batch_idx)
                batch_idx = batch_idx[0:batch_size]
                batch_xs = mnist.train.images[batch_idx]
                batch_ys = mnist.train.labels[batch_idx]
                # ordered batch
                # start = j * batch_size
                # end = (j+1) * batch_size
                # batch_xs, batch_ys = mnist.train.images[start:end], mnist.train.labels[start:end]
                self.sess.run(train_step, feed_dict={self.x: batch_xs, self.y_: batch_ys})

            # test the accuracy 
            correct_prediction = tf.equal( tf.argmax(self.y,1), tf.argmax(self.y_,1) )
            accuracy = tf.reduce_mean( tf.cast(correct_prediction, tf.float32) )
            accuracy = self.sess.run(accuracy, feed_dict = {self.x: mnist.test.images, self.y_: mnist.test.labels})
            accuracy_history.append(accuracy)
            if print_epoch:
                print i, accuracy
            if last_accuracy != 0 and abs(last_accuracy-accuracy) < stop:
                break
            last_accuracy = accuracy

        time2 = time.time()

        return accuracy_history, (time2-time1)

    def close(self):
        if not (self.sess is None):
            self.sess.close()
            self.sess = None

if __name__ == '__main__':
    learner = ReluMnistNet()
    # learner.optimizer = tf.train.GradientDescentOptimizer(0.01)
    learner.optimizer = tf.train.MomentumOptimizer(0.01, momentum=0.9)
    for i in range(10):
        learner.prepare()
        learner.run(stop=0.01, print_epoch=True)
        learner.close()

似乎名为Momentum的变量未初始化?但是,通过致电learner.prepare(),我已拨打tf.initialize_all_variables()。更重要的是,我没有名为Momentum的变量。为什么会这样?

1 个答案:

答案 0 :(得分:3)

在您的代码中,您在初始化全局变量后调用minimize

而你必须这样做:

self.cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=self.y, labels=self.y_) )
self.optimize = self.optimizer.minimize(self.cross_entropy)

并在运行函数而不是

train_step = self.optimizer.minimize(self.cross_entropy)

你应该致电

train_step = self.optimize

P.S Momentun是MomentumOptimizer的默认名称