2017-04-05 103 views
0

我已使用MNIST數據庫在Tensorflow頁面上運行所有示例。現在我試圖運行我自己的例子,我真的不明白。錯誤 - 無法提供張量的形狀X的值

說我有這個CSV表:

enter image description here

它有像5000行。最後一列是每一行的標籤,這個是由多個特徵組成的。 現在爲我的第一個具體的例子。我想在這裏訓練NN這一數據,爲我做了什麼:

import tensorflow as tf 
    import numpy as np 
    import csv 
    import os 
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 
    # read training data 
    Training_file = open('onetest.csv', 'r', newline='') 
    reader = csv.reader(Training_file) 
    row = next(reader) 
    number_of_rows = 2431 
    x = tf.placeholder('float',[None,len(row[:-1])]) 
    w = tf.Variable(tf.zeros([len(row[:-1]),25])) 
    b = tf.Variable(tf.zeros([25])) 
    model = tf.add(tf.matmul(x,w),b) 
    y_ = tf.placeholder('float',[25,None]) 
    y = tf.nn.softmax(model) 

    cross_entropy= -tf.reduce_sum(y_*tf.log(y)) 
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) 
    sess = tf.Session() 
    sess.run(tf.global_variables_initializer()) 
    index =1 
    batch_xs =[] 
    batch_ys= [] 
    for row in reader: 
     batch_xs.append(row[:-1]) 
     batch_ys.append(row[-1]) 
     print(len(batch_xs),len(batch_ys)) 
     index +=1 
     if index%10==0: 
      sess.run(train_step,feed_dict={x:batch_xs, y_:batch_ys}) 
      correct_prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1)) 
      accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float")) 
      batch_xs.clear(); 
      batch_ys.clear(); 

這裏是有錯誤,我得到:

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-11-4dbaa38c4d9c> in <module>() 
    29  index +=1 
    30  if index%10==0: 
---> 31   sess.run(train_step,feed_dict={x:batch_xs, y_:batch_ys}) 
    32   correct_prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1)) 
    33   accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float")) 

c:\users\engine\appdata\local\programs\python\python35\lib\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) 

c:\users\engine\appdata\local\programs\python\python35\lib\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 (9,) for Tensor 'Placeholder_17:0', which has shape '(25, ?)' 

我已經改變指數的值但它沒有解決它,所以我想我誤解了一些東西。將不勝感激任何解釋。

回答

1

在此行y_ = tf.placeholder('float',[25,None])y_被定義爲具有25行(以及任意數量的列)的數據的佔位符。然後在你的代碼中,由於行if index%10==0:你的batch_ys有10行,這就是你得到這個錯誤的原因。

+0

感謝密裏安的答覆。我發佈了我的答案!再次感謝! – Engine

0

所以我得到了它,這裏是將鱈魚,它可以幫助人在那裏:

import tensorflow as tf 
import numpy as np 
import csv 
import os 
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 
# read training data 
Training_file = open('onetest.csv', 'r', newline='') 
reader = csv.reader(Training_file) 
row = next(reader) 

x = tf.placeholder('float',[None,len(row[:-1])]) 
w = tf.Variable(tf.zeros([len(row[:-1]),25])) 
b = tf.Variable(tf.zeros([25])) 
model = tf.add(tf.matmul(x,w),b) 
y_ = tf.placeholder('float',[None,25]) 
y = tf.nn.softmax(model) 
print(y) 

cross_entropy= -tf.reduce_sum(y_*tf.log(y)) 
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) 
sess = tf.Session() 
sess.run(tf.global_variables_initializer()) 
index =0 
batch_xs =[] 
ys= [] 
for row in reader: 
    batch_xs.append(row[:-1]) 
    ys.append(row[-1]) 

    index +=1 
    if index%25==0: 
      batch_ys = np.reshape(ys,(1,25)) 

      sess.run(train_step,feed_dict={x:batch_xs,y_:batch_ys}) 
      correct_prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1)) 
      accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float")) 
      print(sess.run(accuracy,feed_dict={x:batch_xs,y_:batch_ys})) 
      batch_xs.clear() 
      ys.clear() 
相關問題