我在下面运行了玩具代码。我不明白为什么会发生“ FailedPreconditionError:”并指示“ 1_batches”未初始化。
尚未宣布1个批次。
请有人解释并解决此问题。
我尝试在jupyternotebook的服务器部分中使用中断代码,但这没有帮助。
import tensorflow as tf
import numpy as np
from tensorflow.contrib import rnn
import pprint
pp = pprint.PrettyPrinter(indent=4)
sess = tf.InteractiveSession()
h = [1, 0, 0, 0]
e = [0, 1, 0, 0]
l = [0, 0, 1, 0]
o = [0, 0, 0, 1]
with tf.variable_scope('one_cell') as scope:
# One cell RNN input_dim (4) -> output_dim (2)
hidden_size = 2
cell = tf.contrib.rnn.BasicRNNCell(num_units=hidden_size)
print(cell.output_size, cell.state_size)
x_data = np.array([[h]], dtype=np.float32) # x_data = [[[1,0,0,0]]]
pp.pprint(x_data)
outputs, _states = tf.nn.dynamic_rnn(cell, x_data, dtype=tf.float32)
sess.run(tf.global_variables_initializer())
pp.pprint(outputs.eval())
with tf.variable_scope('1_batches') as scope:
# One cell RNN input_dim (4) -> output_dim (2). sequence: 5, batch 3
# 3 batches 'hello', 'eolll', 'lleel'
sess.run(tf.global_variables_initializer())
x_data = np.array([[h, e, l, l, o]], dtype=np.float32)
pp.pprint(x_data)
hidden_size = 2
cell = tf.nn.rnn_cell.LSTMCell(num_units=hidden_size, state_is_tuple=True)
outputs, _states = tf.nn.dynamic_rnn(cell, x_data, dtype=tf.float32)
pp.pprint(outputs.eval())
FailedPreconditionError:尝试使用未初始化的值1_batches / rnn / lstm_cell / bias
[[node 1_batches/rnn/lstm_cell/bias/read (defined at <ipython-input-1-ee510d6a752e>:36) = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](1_batches/rnn/lstm_cell/bias)]]