2017-08-29 72 views
0

試圖訓練word2vec模型,在加載值到Feed_dict的過程中,我卡住了。該錯誤信息是:Tensorflow,設置序列的數組元素

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. 

這裏是我的模型代碼:

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 

這裏是我的生成尺寸爲8批次只是爲了演示目的:

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) 

從我收集的數組的形狀是一致的,center_words和target_words的形狀都是(batch_size,)。我的猜測是它必須對dtype = object部分做些什麼,但我不確定。將不勝感激任何建議。

代碼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() 

代碼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 
+0

很奇怪你的批處理是如何形成的,而不是象你有一個包含'list's的'object'類型的數組一樣。最好讓'gen_batch'返回一個包含兩個「int32」類型數組的元組來代替。 – jdehesa

+0

你可以分享'gen_batch'的代碼嗎?還有'batchesX.shape'和'batchesY.shape'?看起來你正在某個地方創建一個破舊的數組。 –

+0

@DanielF在帖子末尾添加了 – Oksana

回答

0

無論如何,我意識到,我應該遵循批次的不同圖案,所以將其改爲(輸入,輸出)對,他們都是一維數組。這就是它爲我工作的方式。

相關問題