Reputation: 5432
I've run all the examples on the Tensorflow
page, using the MNIST database. Now I'm trying to run my own example and I really just don't get it.
Say I have this csv table:
It has like 5000 rows. and the last column is the label for each row, this one is composed of multiple features. Now for my first concrete example. I want to train a NN on this data, for that here what I've done:
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();
Here is there error that I get :
---------------------------------------------------------------------------
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, ?)'
I've change the value of the index but it didn't solve it so I guess I'm misunderstanding something. will be grateful for any explanation.
Upvotes: 0
Views: 561
Reputation: 19634
In this row y_ = tf.placeholder('float',[25,None])
, y_
is defined to be a placeholder for data that has 25 rows (and any number of columns). Then in your code, due to the row if index%10==0:
your batch_ys
has 10 rows, which is the reason that you get this error.
Upvotes: 1
Reputation: 5432
So I got it, here is the cod for that, it may help somebody out there :
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()
Upvotes: 0