首页 > 解决方案 > 获取 ValueError:将 gensim.word2vec 传递给 feed_dict 时设置具有序列的数组元素

问题描述

我正在使用 tensorflow(不是 Keras)创建一个 seq2seq 模型,并且输入/输出是句子。诸如聊天机器人或翻译器之类的东西。

但是当我跑步时

for epoch in range(total_epoch):
    _, loss = sess.run([optimizer, cost],
                       feed_dict={enc_input: input_batch,
                                  dec_input: output_batch,
                                  targets: target_batch})

我明白了

ValueError: setting an array element with a sequence.

input_batch/是句子output_batch的数组。gensim.word2vec.wv.vectors我也尝试了其他东西作为输入,但我仍然得到同样的错误。对于目标,它是一个数组数组(每个内部数组都是映射到句子单词的数字列表)。

收到错误的 target_batch 如下所示: [[297, 242, 430, 451, 507, 507, 505, 506, 506, 506, 506, 506], [297, 242, 430, 451, 507, 507, 505, 506, 506, 506, 506, 506], ...]

input_batch/output_batch我已经尝试了一切。

我使用gensim word2vec和 forinputbatch.append(input_data) input_data为每个句子使用gensim word2vec如下:

model=Word2Vec(input_sentence.split(), size=5, window=10, min_count=1, workers=4, sg=1)

我已经完成了所有工作,从将其保存到 bin 并检索到使用model.wv.vectors. 我得到了所有 3 个错误enc_inputdec_input并且 targets

enc_input = tf.placeholder(tf.float32, [None, None, n_input])

谢谢。

标签: tensorflowword2vecseq2seq

解决方案


看来您必须将向量张量目标批次的形状与提要字典中的键匹配,enc_input, dec_input, targets. 从tensorflow Session.py 的文档中:

可选的 feed_dict 参数允许调用者覆盖图中张量的值。feed_dict 中的每个键都可以是以下类型之一:如果键是 tf.Tensor,则值可以是 Python 标量、字符串、列表或 numpy ndarray,可以转换为与该张量相同的 dtype。此外,如果键是 tf.placeholder,则将检查值的形状是否与占位符兼容。

占位符必须具有与输入张量兼容的形状。为目标批次使用 numpy 数组可能更容易,并使用它来设置占位符的尺寸,如下所示:

import numpy as np

target_batch = np.array([[297, 242, 430, 451, 507, 507, 505, 506, 506, 506, 506, 506], [297, 242, 430, 451, 507, 507, 505, 506, 506, 506, 506, 506], ...]) #full list of lists

enc_input = tf.placeholder(dtype = tf.float32, shape=target_batch.shape, name=None)

如果没有更多信息,我无法对其进行测试,但希望对您有所帮助。


推荐阅读