【问题标题】:ValueError: setting an array element with a sequence KerasValueError:使用序列 Keras 设置数组元素
【发布时间】:2017-10-17 15:52:36
【问题描述】:

我正在构建一个混合 CNN-RNN 架构来制作预测模型。我已经在 TensorFlow 中使用了 Keras 实现。但我不断收到此错误 -

File "try.py", line 56, in <module>
model.fit(data, labels, epochs=10, batch_size=32)
File "/share/apps/caffe_software/anaconda4.3.1/lib/python2.7/site-packages/keras/models.py", line 845, in fit
initial_epoch=initial_epoch)
File "/share/apps/caffe_software/anaconda4.3.1/lib/python2.7/site-packages/keras/engine/training.py", line 1485, in fit
initial_epoch=initial_epoch)
File "/share/apps/caffe_software/anaconda4.3.1/lib/python2.7/site-packages/keras/engine/training.py", line 1140, in _fit_loop
outs = f(ins_batch)
File "/share/apps/caffe_software/anaconda4.3.1/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2073, in __call__
feed_dict=feed_dict)
File "/share/apps/caffe_software/anaconda4.3.1/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 778, in run
run_metadata_ptr)
File "/share/apps/caffe_software/anaconda4.3.1/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 954, in _run
np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
File "/share/apps/caffe_software/anaconda4.3.1/lib/python2.7/site-packages/numpy/core/numeric.py", line 531, in asarray
return array(a, dtype, copy=False, order=order)

ValueError: 使用序列设置数组元素。

我在这里附上我的代码

import gensim
from gensim.models import word2vec
documents = ["Human machine interface for lab abc computer applications",
             "A survey of user opinion of computer system response time",
             "The EPS user interface management system",
             "System and human system engineering testing of EPS",
             "Relation of user perceived response time to error measurement"]
sentences = [[word for word in document.lower().split()] for document in documents]
word_model = gensim.models.word2vec.Word2Vec(sentences, size=200, min_count = 1, window = 5)
word_vectors = word_model.wv
data = np.array(word_vectors, ndmin = 2, dtype = object) 
labels = np.array([0.214285714286], ndmin = 2 , dtype = object) #A Normalised Class Label Name 

数据变量给出错误 它应该是一个numpy数组,但本质上仍然是词向量序列

【问题讨论】:

  • 您确定您的datalabels 是包含“仅”数字的numpy 数组吗? (不是其他数组)。
  • data 是 并且 label 是一个包含标准化值的 numpy 数组
  • 你正在喂word2vec.wv 这是一个对象。这会导致问题。请注意,您可能想提供使用 word2vec 模型转换的序列 - 在您的代码中的任何时候都没有这样做。
  • @MarcinMożejko 我正在通过 numpy 数组数据将序列提供给 model.fit,但我仍然收到值错误,如何操作 word2vec.wv 以提供给 model.fit?

标签: python tensorflow deep-learning keras hybrid


【解决方案1】:

将 wv 词向量转换为适合插入 Keras 模型的 numpy 矩阵

【讨论】:

    猜你喜欢
    • 2019-09-03
    • 2011-06-08
    • 2018-08-04
    • 2019-08-04
    相关资源
    最近更新 更多