首页 文章

ValueError:Tensor必须来自与Tensorflow中具有Bidirectinal RNN的Tensor相同的图形

提问于
浏览
12

我正在使用张量流中的双向动态RNN进行文本标记 . 在加工输入维度后,我尝试运行一个Session . 这是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)必须与Tensor在同一个图形中(“bidirectional_rnn / fw / fw / stack_2:0”,shape =(1,), D型= INT32) .

请帮帮我

1 回答

  • 20

    TensorFlow将所有操作存储在操作图上 . 此图定义了输出到哪里的函数,并将它们全部链接在一起,以便它可以按照您在图中设置的步骤生成最终输出 . 如果您尝试在一个图表上输入Tensor或操作到Tensor或在另一个图表上操作它将失败 . 一切都必须在同一个执行图上 .

    尝试删除 with tf.Graph().as_default():

    TensorFlow为您提供了一个默认图形,如果您未指定图形,则会引用该图形 . 您可能正在使用训练块中的一个位置和不同图形中的默认图形 .

    似乎没有理由在此处将图表指定为默认值,并且很可能您在事故中使用单独的图表 . 如果你真的想要指定一个图形,那么你可能希望将它作为变量传递,而不是像这样设置它 .

相关问题