Search code examples
pythontensorflowdeep-learningkerashybrid

ValueError: setting an array element with a sequence Keras


I am building a hybrid CNN-RNN architecture to make a predictive model. I have used Keras implementation with TensorFlow. But I keep getting this error -

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: setting an array element with a sequence.

I am attaching my code here

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 

The data variable gives error It is supposed to be a numpy array but at its essence is still word vector sequence


Solution

  • converted the wv word vectors into a numpy matrix that is suitable for insertion into Keras model