2017-04-05 5 views
0

Ich habe alle Beispiele auf der Seite Tensorflow unter Verwendung der MNIST-Datenbank ausgeführt. Jetzt versuche ich mein eigenes Beispiel zu führen und ich verstehe es einfach nicht.Fehler - Wert der Form X für Tensor kann nicht eingegeben werden

sagen, dass ich diese csv-Tabelle haben:

enter image description here

Es ist wie 5000 Zeilen. und die letzte Spalte ist die Bezeichnung für jede Zeile, diese besteht aus mehreren Funktionen. Nun zu meinem ersten konkreten Beispiel. Ich möchte ein NN auf diese Daten trainieren, denn das ist hier, was ich getan habe:

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(); 

Hier gibt es Fehler, die ich bekomme:

--------------------------------------------------------------------------- 
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, ?)' 

Ich habe den Wert des Index ändern aber es hat es nicht gelöst, also denke ich, dass ich etwas falsch verstehe. Ich werde für jede Erklärung dankbar sein.

Antwort

1

In dieser Reihe y_ = tf.placeholder('float',[25,None]) wird y_ definiert ist ein Platzhalter für Daten zu sein, die 25 Zeilen (und eine beliebige Anzahl von Spalten) aufweisen. Dann in Ihrem Code, aufgrund der Zeile if index%10==0: Ihre batch_ys hat 10 Zeilen, was der Grund ist, dass Sie diesen Fehler erhalten. So

+0

dank Mirian für die Beantwortung. Ich habe meine Antwort gepostet! Danke noch einmal! – Engine

0

Ich habe es, hier ist der Kabeljau für das, es jemand da draußen helfen kann:

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() 
Verwandte Themen