Tensorflow FailedPreconditionError:尝试使用未初始化的值beta1_power

时间:2018-07-16 23:26:38

标签: python tensorflow

我正在尝试在Tensorflow中建立一个简单的卷积网络。我将尝试将代码量保持在最低水平。这是课程:

class ConvNet(object):

def __init__(self, input, labels, dataset):

    self.input = input
    self.true_labels = labels

    #'dataset' is an instance of a class that
    #I am using to read the training images
    self.data = dataset

    self.build()

    self._optimize = None

    self.sess = tf.Session()

    self.sess.run(tf.global_variables_initializer())

def build(self):

    self.conv_layers = []
    # create 3 conv layers using tf.nn.conv2d
    # and append them to self.conv_layers

    # create flattening layer using tf.nn.reshape

    self.fc_layers = []
    # create 2 fully connected layers using tf.matmul
    # and append them to self.fc_layers

@property
def optimize(self):
    """Return the optimize operation."""
    if not self._optimize:

        cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits = self.fc_layers[-1],
                                                                labels = self.true_labels)

        cost = tf.reduce_mean(cross_entropy)

        self._optimize = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)

    return self._optimize

def train(self, num_epochs=1, batch_size=16):

    epochs_done = 0

    while not epochs_done == num_epochs:

        # get next batch from training data
        x_batch, y_true_batch, _, _ = self.data.train.next_batch(batch_size)

        feed_dict_train = {self.input: x_batch, self.true_labels: y_true_batch}

        #even if I initialize variables here, same error occurs
        #self.sess.run(tf.global_variables_initializer())

        self.sess.run(self.optimize, feed_dict=feed_dict_train)

        if self.data.train.epochs_done > epochs_done:

            #print stuff...

            epochs_done += 1

    self.sess.close()

我试图运行optimize操作以在简单的数据集(例如猫与狗的图像)上训练网络。这里是主要的:

if __name__ == "__main__":

    classes = ['dogs', 'cats']
    num_classes = len(classes)

    batch_size = 16

    img_size = 128
    num_channels = 3

    #read training data and resize images to 128 x 128 pixels
    data = dataset.read_train_sets(img_size, classes)

    x = tf.placeholder(tf.float32, shape = [None, img_size, img_size, num_channels], name='x')
    y_true = tf.placeholder(tf.float32, shape = [None, num_classes], name = 'y_true')

    net = ConvNet(x, y_true, data)

    net.train()

但是,我仍然收到以下错误(实际上比这更长的时间,如果需要,我可以发布该错误):

    Caused by op 'beta1_power/read', defined at:
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.5/idlelib/run.py", line 124, in main
    ret = method(*args, **kwargs)
  File "/usr/lib/python3.5/idlelib/run.py", line 351, in runcode
    exec(code, self.locals)
  File "/home/gian/face_recognition/model.py", line 366, in <module>
    net.train()
  File "/home/gian/face_recognition/model.py", line 309, in train
    self.sess.run(self.optimize, feed_dict=feed_dict_train)
  File "/home/gian/face_recognition/model.py", line 269, in optimize
    self._optimize = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 409, in minimize
    name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 552, in apply_gradients
    self._create_slots([_get_variable_for(v) for v in var_list])
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/adam.py", line 124, in _create_slots
    colocate_with=first_var)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/optimizer.py", line 665, in _create_non_slot_variable
    v = variable_scope.variable(initial_value, name=name, trainable=False)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 2157, in variable
    use_resource=use_resource)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 2147, in <lambda>
    previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variable_scope.py", line 2130, in default_variable_creator
    constraint=constraint)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variables.py", line 235, in __init__
    constraint=constraint)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variables.py", line 391, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/array_ops.py", line 142, in identity
    return gen_array_ops.identity(input, name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3053, in identity
    "Identity", input=input, name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3290, in create_op
    op_def=op_def)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1654, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value beta1_power
     [[Node: beta1_power/read = Identity[T=DT_FLOAT, _class=["loc:@Variable"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](beta1_power)]]

有人可以帮我弄清楚问题出在哪里吗?非常感谢

1 个答案:

答案 0 :(得分:0)

未初始化优化器时,通常会发生此错误。 因此,只需在初始化所有全局变量之前添加self.optimize。

您的代码应如下所示。

def __init__(self, input, labels, dataset):

self.input = input
self.true_labels = labels

#'dataset' is an instance of a class that
#I am using to read the training images
self.data = dataset

self.build()

self._optimize = None

self.sess = tf.Session()
self.optimize()
self.sess.run(tf.global_variables_initializer())