【问题标题】:Getting ValueError: setting an array element with a sequence when passing gensim.word2vec to feed_dict获取 ValueError:将 gensim.word2vec 传递给 feed_dict 时设置具有序列的数组元素
【发布时间】:2019-04-28 03:19:12
【问题描述】:

我正在使用 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_batchgensim.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 word2vecinputbatch.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.vectorsenc_inputdec_inputtargets 中的所有 3 个都出现了该错误

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

谢谢。

【问题讨论】:

    标签: tensorflow word2vec seq2seq


    【解决方案1】:

    看来您必须将向量张量目标批次的形状与您的提要字典 enc_input, dec_input, targets 中的键相匹配。来自documentation for tensorflowSession.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)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 2020-06-04
      • 2017-10-20
      • 2023-03-16
      • 2020-08-21
      • 2019-05-02
      • 2019-11-28
      相关资源
      最近更新 更多