ValueError: Tensor must be from the same graph as Tensor with Bidirectinal RNN in Tensorflow
ValueError: Tensor must be from the same graph as Tensor with Bidirectinal RNN in Tensorflow
我在 tensorflow 中使用双向动态 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')
这是 运行 部分:
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)
但是,它给我错误:
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).
请帮帮我
TensorFlow 将所有操作存储在一个操作图上。该图定义了哪些函数输出到哪里,并将它们链接在一起,以便它可以按照您在图中设置的步骤生成最终输出。如果您尝试将一个张量或一个图上的操作输入另一个图上的张量或操作,它将失败。一切都必须在同一个执行图上。
尝试删除 with tf.Graph().as_default():
TensorFlow 为您提供了一个默认图,如果您不指定图,则可以参考该图。您可能在一个地方使用默认图表,而在训练块中使用不同的图表。
您似乎没有理由在此处将图表指定为默认图表,很可能您不小心使用了单独的图表。如果你真的想指定一个图表,那么你可能想把它作为一个变量传递,而不是像这样设置它。
如果您将 tf 2.x 与 Keras 一起使用 - 那么在构建模型图之前禁用急切执行可能会有所帮助。因此,要禁用急切执行 - 在定义模型之前添加以下行。
tf.compat.v1.disable_eager_execution()
或者您可以转到 'Kernel' 和 select“重新启动并清除输出”。 :D
我在 tensorflow 中使用双向动态 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')
这是 运行 部分:
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)
但是,它给我错误:
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).
请帮帮我
TensorFlow 将所有操作存储在一个操作图上。该图定义了哪些函数输出到哪里,并将它们链接在一起,以便它可以按照您在图中设置的步骤生成最终输出。如果您尝试将一个张量或一个图上的操作输入另一个图上的张量或操作,它将失败。一切都必须在同一个执行图上。
尝试删除 with tf.Graph().as_default():
TensorFlow 为您提供了一个默认图,如果您不指定图,则可以参考该图。您可能在一个地方使用默认图表,而在训练块中使用不同的图表。
您似乎没有理由在此处将图表指定为默认图表,很可能您不小心使用了单独的图表。如果你真的想指定一个图表,那么你可能想把它作为一个变量传递,而不是像这样设置它。
如果您将 tf 2.x 与 Keras 一起使用 - 那么在构建模型图之前禁用急切执行可能会有所帮助。因此,要禁用急切执行 - 在定义模型之前添加以下行。
tf.compat.v1.disable_eager_execution()
或者您可以转到 'Kernel' 和 select“重新启动并清除输出”。 :D