Trying to train word2vec model, I got stuck during the part of loading values into the feed_dict. The error message is :
ValueError Traceback (most recent call last)
<ipython-input-31-eba8f8f5ab96> in <module>()
----> 1 model.train_word2vec()
<ipython-input-28-d20feabd3b23> in train_word2vec(self)
47 target_word = batch[:,0]
48 loss_get,_ = sess.run([loss,optimizer],feed_dict={center_words:center_word,
---> 49 target_words:target_word})
50 average_loss+=loss_get
51
/Users/mac/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
765 try:
766 result = self._run(None, fetches, feed_dict, options_ptr,
--> 767 run_metadata_ptr)
768 if run_metadata:
769 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/Users/mac/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
936 ' to a larger type (e.g. int64).')
937
--> 938 np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
939
940 if not subfeed_t.get_shape().is_compatible_with(np_val.shape):
/Users/mac/anaconda3/lib/python3.6/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
529
530 """
--> 531 return array(a, dtype, copy=False, order=order)
532
533
ValueError: setting an array element with a sequence.
Here's my model code:
center_words = tf.placeholder(dtype=tf.int32,shape=[self.batch_size],name="center_words")
target_words = tf.placeholder(dtype=tf.int32,shape=[self.batch_size,1],name="target_words")
...
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(self.training_steps):
batch = next(batch_gen)
center_word = batch[:,1]
target_word = batch[:,0]
loss_get,_ = sess.run([loss,optimizer],feed_dict={center_words:center_word,
target_words:target_word})
average_loss+=loss_get
And here is my generated batch of size 8 just for demonstration purpose:
gen=gen_batch(batchesX,batchesY,batch_size=8)
batch=next(gen)
batch[:,0]
#target words
array([array([-1, -1, -1, 1, 2, 3], dtype=int32),
array([-1, -1, -1, 2, 3, 4], dtype=int32),
array([-1, -1, -1, 3, 4, 5], dtype=int32),
array([0, 1, 2, 4, 5, 6], dtype=int32),
array([1, 2, 3, 5, 6, 7], dtype=int32),
array([2, 3, 4, 6, 7, 0], dtype=int32),
array([3, 4, 5, 7, 0, 8], dtype=int32),
array([4, 5, 6, 0, 8, 9], dtype=int32)], dtype=object)
batch[:,1]
#center words:
array([0, 1, 2, 3, 4, 5, 6, 7], dtype=object)
From what i gathered shape of array are consistent both center_words and target_words have shape of (batch_size,).My guessing is that it has to do something with dtype=object part , but I'm not sure. Would be grateful for any suggestions.
Code of gen_batch :
def gen_batch(batchesX,batchesY,batch_size=256):
'''Batch generator in order to save some computation time'''
batches=generate_empty_2D_batch_array()
for batch in zip(batchesX,batchesY):
for i in range(len(batch[0])):
X_sample = batch[0][i]
Y_sample = batch[1][i]
one_batch = np.array([[X_sample,Y_sample]])
batches=np.append(batches,one_batch,axis=0)
if len(batches)==batch_size:
yield batches
batches=generate_empty_2D_batch_array()
Code of generate_empty_2D_batch_array :
def generate_empty_2D_batch_array():
''' Name of function is self-explanatory'''
arr = np.array([],dtype=np.int32)
arr = arr.reshape(-1,2)
return arr
Anyway, I realized that i should follow different pattern of batches, so I changed it to (input,output) pairs ,and they all were 1D arrays. That's how it worked for me.