0

這是代碼值錯誤:,其具有形狀 '(5,15,2)'

num_epochs = 100 
total_series_length = 50000 
truncated_backprop_length = 15 
state_size = 4 
num_classes = 2 
echo_step = 3 
batch_size = 5 
num_batches = total_series_length//batch_size//truncated_backprop_length 

genreating數據{:無法張量 '0 one_hot' 喂形狀(5,15)的值...}

batchX_placeholder = tf.placeholder(tf.int32, [batch_size, truncated_backprop_length]) 
batchY_placeholder = tf.placeholder(tf.int32, [batch_size, truncated_backprop_length]) 

#and one for the RNN state, 5,4 
init_state = tf.placeholder(tf.float32, [batch_size, state_size]) 

batchX_placeholder = tf.one_hot(batchX_placeholder, num_classes) 
inputs_series = tf.unstack(batchX_placeholder, axis=1) 

cell = tf.contrib.rnn.BasicRNNCell(state_size) 
rnn_outputs, final_state = tf.contrib.rnn.static_rnn(cell, inputs_series, initial_state=init_state) 

一些優化代碼{....} ,然後創建一個圖

#Step 3 Training the network 
with tf.Session() as sess: 
    #we stupidly have to do this everytime, it should just know 
    #that we initialized these vars. v2 guys, v2.. 
    sess.run(tf.initialize_all_variables()) 
    #interactive mode 
    plt.ion() 
    #initialize the figure 
    plt.figure() 
    #show the graph 
    plt.show() 
    #to show the loss decrease 
    loss_list = [] 

    for epoch_idx in range(num_epochs): 
     #generate data at eveery epoch, batches run in epochs 
     x,y = generateData() 
     #initialize an empty hidden state 
     _current_state = np.zeros((batch_size, state_size)) 

     print("New data, epoch", epoch_idx) 
     #each batch 
     for batch_idx in range(num_batches): 
      #starting and ending point per batch 
      #since weights reoccuer at every layer through time 
      #These layers will not be unrolled to the beginning of time, 
      #that would be too computationally expensive, and are therefore truncated 
      #at a limited number of time-steps 
      start_idx = batch_idx * truncated_backprop_length 
      end_idx = start_idx + truncated_backprop_length 

      batchX = x[:,start_idx:end_idx] 
      batchY = y[:,start_idx:end_idx] 

      #run the computation graph, give it the values 
      #we calculated earlier 
      _total_loss, _train_step, _final_state, _predictions_series = sess.run(
       [total_loss, train_step, final_state, predictions], 
       feed_dict={ 
        batchX_placeholder:batchX, 
        batchY_placeholder:batchY, 
        init_state:_current_state 
       }) 

      loss_list.append(_total_loss) 

      if batch_idx%100 == 0: 
       print("Step",batch_idx, "Loss", _total_loss) 
       plot(loss_list, _predictions_series, batchX, batchY) 

plt.ioff() 
plt.show() 

這個錯誤:

ValueError        Traceback (most recent call last) 
<ipython-input-9-7c3d1289d16b> in <module>() 
    40      batchX_placeholder:batchX, 
    41      batchY_placeholder:batchY, 
---> 42      init_state:_current_state 
    43     }) 
    44 

/home/pranshu_44/anaconda3/lib/python3.5/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) 

/home/pranshu_44/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 
    942     'Cannot feed value of shape %r for Tensor %r, ' 
    943     'which has shape %r' 
--> 944     % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape()))) 
    945   if not self.graph.is_feedable(subfeed_t): 
    946    raise ValueError('Tensor %s may not be fed.' % subfeed_t) 

ValueError: Cannot feed value of shape (5, 15) for Tensor 'one_hot:0', which has shape '(5, 15, 2)' 

我看了文檔,但該做的所有 沒有幫助,如果有任何其他簡便的方法,也將是有益的

回答

0

你是你的佔位符變量轉換到一熱表示,但不能轉換您在訓練期間實際提供給網絡的數據。在餵食之前,嘗試將batchX轉換爲單熱表示。此段代碼將矩陣轉換爲其一隻熱表示:

# Assuming that y contains values from 0 to N-1 where N is number of classes 
batchX = (np.arange(max(batchX.flatten())+1) = batchX[:,:,None]).astype(int) 
+0

感謝的人..它的工作!這對我來說是如此的嫺熟。 –

相關問題