Python ValueError:张量必须与 Tensorflow 中具有双向 RNN 的张量来自同一图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42616625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: Tensor must be from the same graph as Tensor with Bidirectinal RNN in Tensorflow
提问by Namang-K
I'm doing text tagger using Bidirectional dynamic RNN in tensorflow. After maching input's dimension, I tried to run a Session. this is blstm setting parts:
我正在张量流中使用双向动态 RNN 进行文本标记。在加工输入的维度后,我尝试运行一个会话。这是 blstm 设置部分:
fw_lstm_cell = BasicLSTMCell(LSTM_DIMS)
bw_lstm_cell = BasicLSTMCell(LSTM_DIMS)
(fw_outputs, bw_outputs), _ = bidirectional_dynamic_rnn(fw_lstm_cell,
bw_lstm_cell,
x_place,
sequence_length=SEQLEN,
dtype='float32')
and this is runing part:
这是运行部分:
with tf.Graph().as_default():
# Placehoder Settings
x_place, y_place = set_placeholder(BATCH_SIZE, EM_DIMS, MAXLEN)
# BLSTM Model Building
hlogits = tf_kcpt.build_blstm(x_place)
# Compute loss
loss = tf_kcpt.get_loss(log_likelihood)
# Training
train_op = tf_kcpt.training(loss)
# load Eval method
eval_correct = tf_kcpt.evaluation(logits, y_place)
# Session Setting & Init
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# tensor summary setting
summary = tf.summary.merge_all()
summary_writer = tf.summary.FileWriter(LOG_DIR, sess.graph)
# Save
saver = tf.train.Saver()
# Run epoch
for step in range(EPOCH):
start_time = time.time()
feed_dict = fill_feed_dict(KCPT_SET['train'], x_place, y_place)
_, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)
But, it give me the error:
但是,它给了我错误:
ValueError: Tensor("Shape:0", shape=(1,), dtype=int32) must be from the same graph as Tensor("bidirectional_rnn/fw/fw/stack_2:0", shape=(1,), dtype=int32).
ValueError: Tensor("Shape:0", shape=(1,), dtype=int32) 必须与 Tensor("bidirectional_rnn/fw/fw/stack_2:0", shape=(1,), dtype 来自同一图= int32)。
Help me, please
请帮帮我
回答by Max Weinzierl
TensorFlow stores all operations on an operational graph. This graph defines what functions output to where, and it links it all together so that it can follow the steps you have set up in the graph to produce your final output. If you try to input a Tensor or operation on one graph into a Tensor or operation on another graph it will fail. Everything must be on the same execution graph.
TensorFlow 将所有操作存储在一个操作图上。该图定义了哪些函数输出到何处,并将它们链接在一起,以便它可以按照您在图中设置的步骤生成最终输出。如果您尝试将一个图上的张量或操作输入到另一个图上的张量或操作中,它将失败。一切都必须在同一个执行图上。
Try removing with tf.Graph().as_default():
尝试删除 with tf.Graph().as_default():
TensorFlow provides you a default graph which is referred to if you do not specify a graph. You are probably using the default graph in one spot and a different graph in your training block.
TensorFlow 为您提供了一个默认图形,如果您未指定图形,则会引用该图形。您可能在一个地方使用默认图表,而在训练块中使用不同的图表。
There does not seem to be a reason you are specifying a graph as default here and most likely you are using separate graphs on accident. If you really want to specify a graph then you probably want to pass it as a variable, not set it like this.
您在此处将图表指定为默认值似乎没有理由,而且很可能您无意中使用了单独的图表。如果您真的想指定一个图形,那么您可能希望将其作为变量传递,而不是像这样设置。
回答by Niranjan Agnihotri
If you are using tf 2.x with Keras - then maybe disable-ling eager execution before building the model graph could help. So to disable eager execution - adding the following line before defining the model.
如果您将 tf 2.x 与 Keras 一起使用 - 那么在构建模型图之前禁用即时执行可能会有所帮助。所以要禁用急切执行 - 在定义模型之前添加以下行。
tf.compat.v1.disable_eager_execution()