遇到ValueError:当我预测两次时,Tensor必须与Tensor来自同一张图

时间:2018-10-07 14:47:19

标签: python tensorflow

最近,我正在使用Esmiator API构建seq2seq模型,但是出现的问题困扰了我几天。

当我两次预测时,它将显示ValueError

   ...
   predictions = list(mymodel.predict(
       lambda: ev_func_with_sentence(last_result)))
   predictions = list(mymodel.predict(
       lambda: ev_func_with_sentence(last_result)))
   ...
   ...
ValueError: Tensor("embedding/Cast:0", shape=(?, 3000), dtype=int32) must be from the same graph as Tensor("embedding/embeddings/Read/ReadVariableOp:0", shape=(103727, 200), dtype=float32).

mymodel的定义如下:

mymodel = tf.estimator.Estimator(
    model_fn=model_fn,
    model_dir=model_dir,
    params={
        'features': _get_features(),
        'learning_rate': learning_rate
    },
    config=tf.estimator.RunConfig(
        save_checkpoints_steps=1,
        save_summary_steps=1,
    )
)

这是我的input_func

def ev_data_with_sentence(last_results=None):
    model = Word2Vec.load(configs.WORD2VEC_FILE)
    datas = ev_set()
    if last_results is None:
        # A [batch * 30] array
        last_results = numpy.zeros([len(datas), configs.MAX_TITLE_WORD])

    for data, result in zip(datas, last_results):
        content = data.get('content')
        xs = tokenization(content, is_sentence=True)
        X = index_word_with_sentence(xs, model)
        # X.shape == (?, 50, 60)
        yield X, result


def ev_func_with_sentence(last_results):
    def _warp(data, labels):
        return {'x1': data, 'x2': labels}

    data = tf.data.Dataset().from_generator(
        lambda: ev_data_with_sentence(last_results),
        output_types=((tf.float32, tf.int64)))
    data = data.batch(configs.batch_size)
    data = data.map(_warp)
    data = data.make_one_shot_iterator()
    inputs = data.get_next()
    return inputs

这让我发疯。救命!拜托!

0 个答案:

没有答案