'FailedPreconditionError(参见上面的回溯):在运行CNN时尝试使用未初始化的值beta1_power'

时间:2017-07-11 19:49:59

标签: python tensorflow conv-neural-network

我尝试使用Tensorflow构建卷积神经网络,但出了点问题。

以下是代码:

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

x = mpimg.imread('data.jpg')
y = 1

x = (x-np.mean(x))/np.std(x)

epochs = 10

weights = [tf.Variable(tf.random_normal([5,5,3,32])),
           tf.Variable(tf.random_normal([5,5,32,64])),
           tf.Variable(tf.random_normal([5,5,64,128])),
           tf.Variable(tf.random_normal([19*25*128, 1064])),
           tf.Variable(tf.random_normal([1064, 19*25*128])),
           tf.Variable(tf.random_normal([19*25*128, 1064])),
           tf.Variable(tf.random_normal([1064, 1]))]

def CNN(x, weights):
    output = tf.nn.conv2d([x], weights[0], [1,2,2,1], 'SAME')
    output = tf.nn.relu(output)
    output = tf.nn.conv2d(output, weights[1], [1,2,2,1], 'SAME')
    output = tf.nn.relu(output)
    output = tf.nn.conv2d(output, weights[2], [1,2,2,1], 'SAME')
    output = tf.nn.relu(output)
    output = tf.reshape(output, [-1,19*25*128])
    output = tf.matmul(output, weights[3])
    output = tf.nn.relu(output)
    output = tf.matmul(output, weights[4])
    output = tf.nn.relu(output)
    output = tf.matmul(output, weights[5])
    output = tf.nn.relu(output)
    output = tf.matmul(output, weights[6])
    output = tf.reduce_sum(output)
    return output

init = tf.global_variables_initializer()
sess = tf.Session()

prediction = CNN(tf.cast(x,tf.float32), weights)
cost = tf.reduce_mean(tf.square(prediction-y))
train = tf.train.AdamOptimizer().minimize(cost)

sess.run(init)
for e in range(epochs):
    print('epoch:',e+1)
    sess.run([cost, train])
    print(cost.eval())
print('optimization finished!')

CNN(x, weights)
print(output.eval())

这是我收到的错误:

Traceback (most recent call last):
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1021, in _do_call
    return fn(*args)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1003, in _run_fn
    status, run_metadata)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\contextlib.py", line 66, in __exit__
    next(self.gen)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 469, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.FailedPreconditionError: 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/cpu:0"](beta1_power)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "D:\Kay\Tensorflow\Session 3\CNN.py", line 49, in <module>
    sess.run([cost, train])
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 766, in run
    run_metadata_ptr)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 964, in _run
    feed_dict_string, options, run_metadata)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1014, in _do_run
    target_list, options, run_metadata)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1034, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: 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/cpu:0"](beta1_power)]]
Caused by op 'beta1_power/read', defined at:
  File "<string>", line 1, in <module>
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\idlelib\run.py", line 124, in main
    ret = method(*args, **kwargs)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\idlelib\run.py", line 351, in runcode
    exec(code, self.locals)
  File "D:\Kay\Tensorflow\Session 3\CNN.py", line 44, in <module>
    train = tf.train.AdamOptimizer().minimize(cost)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\training\optimizer.py", line 279, in minimize
    name=name)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\training\optimizer.py", line 393, in apply_gradients
    self._create_slots(var_list)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\training\adam.py", line 113, in _create_slots
    trainable=False)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variables.py", line 224, in __init__
    expected_shape=expected_shape)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variables.py", line 370, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1424, in identity
    result = _op_def_lib.apply_op("Identity", input=input, name=name)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 759, in apply_op
    op_def=op_def)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2240, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1128, in __init__
    self._traceback = _extract_stack()
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/cpu:0"](beta1_power)]]

我希望你能帮我修改我的代码。

PS。如果我的代码对您来说效率低下,那是因为我只有14岁。如果我收到反馈,我会很高兴。

0 个答案:

没有答案