showing ValueError: Cannot feed value of shape (50, 41) for Tensor u'Placeholder:0', which has shape

1,135 views
Skip to first unread message

Vinayakumar R

unread,
Apr 5, 2016, 5:30:29 PM4/5/16
to Discuss
data set has 42 attributes and program is given below and please have a look on attached image for error

import csv
import tensorflow as tf
import random
import pandas as pd
import numpy as np

ipd = pd.read_csv("cla2/train.csv")

species = list(ipd['result'].unique())
ipd['One-hot'] = ipd['result'].map(lambda x: np.eye(len(species))[species.index(x)] )


shuffled = ipd.sample(frac=1)
trainingSet = shuffled[0:len(shuffled)-50]
testSet = shuffled[len(shuffled)-50:]


inp = tf.placeholder(tf.float32, [None, 4])
weights = tf.Variable(tf.zeros([4, 3]))
bias = tf.Variable(tf.zeros([3]))

y = tf.nn.softmax(tf.matmul(inp, weights) + bias)

y_ = tf.placeholder(tf.float32, [None, 3])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))

train_step = tf.train.AdamOptimizer(0.01).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))


init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)



keys = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11','12','13','14','15','16','17','18','19','20','21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31','32','33','34','35','36','37','38','39','40','41']

for i in range(1000):
    train = trainingSet.sample(50)
    sess.run(train_step, feed_dict={inp: [x for x in train[keys].values],
                                    y_: [x for x in train['One-hot'].as_matrix()]})

print sess.run(accuracy, feed_dict={inp: [x for x in testSet[keys].values],
                                    y_: [x for x in testSet['One-hot'].values]})

Untitled.png

vaibhav jain

unread,
Aug 3, 2017, 5:32:03 AM8/3/17
to Discuss
Hi,

Are you able to solve your issue??

I'm trying to solve a simple ML problem with TF learn, But getting an error. Below I have added my code.

#!/usr/bin/env python
import os

os
.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import tflearn

net
= tflearn.input_data(shape=[None, 2]) # No of features is two
net
= tflearn.fully_connected(net, 2) # No idea what the value should be
# net = tflearn.fully_connected(net, 2, activation='softmax')
net
= tflearn.regression(net)

# Define model
model
= tflearn.DNN(net)
data
= [
   
# Apple
   
[105, 0],
   
[110, 0],
   
[100, 0],
   
[140, 0],
   
[130, 0],
   
# Orange
   
[150, 1],
   
[155, 1],
   
[160, 1],
   
[170, 1],
   
[190, 1],
]
labels
= [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]

# Start training (apply gradient descent algorithm)
model
.fit(data, labels, n_epoch=3, batch_size=2, show_metric=True)

pred
= model.predict([[180, 0], [100, 0]])
print pred

"""
Getting error


Run id: RAATYL
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 10
Validation samples: 0
--

--
Traceback (most recent call last):
  File "
./ml5.py", line 32, in <module>
    model.fit(data, labels, n_epoch=3, batch_size=2, show_metric=True)
  File "
/home/prime/.virtualenvs/mllearn/local/lib/python2.7/site-packages/tflearn/models/dnn.py", line 216, in fit
    callbacks=callbacks)
  File "
/home/prime/.virtualenvs/mllearn/local/lib/python2.7/site-packages/tflearn/helpers/trainer.py", line 338, in fit
    show_metric)
  File "
/home/prime/.virtualenvs/mllearn/local/lib/python2.7/site-packages/tflearn/helpers/trainer.py", line 817, in _train
    feed_batch)
  File "
/home/prime/.virtualenvs/mllearn/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 778, in run
    run_metadata_ptr)
  File "
/home/prime/.virtualenvs/mllearn/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 961, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (2,) for Tensor u'TargetsData/Y:0', which has shape '(?, 2)'
"""
Reply all
Reply to author
Forward
0 new messages