Reputation: 162
Tensorflow 1.14 gives me issue "Cannot feed value of shape (100,) for Tensor 'Placeholder_1:0', which has shape '(?, 1)'" as mentioned in the question. This error occurs in the session segment of the code [last cell of the code]. How do I solve it? Im not able to understand where the bug is lying. The code is in iPython format. Thanks in advance.
Code is as follows:
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import tensorflow as tf
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import random
import math
import time
import h5py
# In[2]:
if len(tf.config.experimental.list_physical_devices('GPU')) > 0:
tf.debugging.set_log_device_placement(True)
# In[3]:
df = pd.read_hdf('../Dataset/dataset.h5',key='df')
# In[4]:
df.head()
# In[5]:
df.pop('App')
df_y = df.pop('Label')
df_x = df
# In[6]:
trn_x, val_x, trn_y, val_y = train_test_split(df_x, df_y, test_size=0.3)
# In[7]:
trn_x.head()
# In[8]:
trn_y.head()
# In[9]:
val_x.head()
# In[10]:
val_y.head()
# In[11]:
train_size,num_features = trn_x.shape
print(train_size)
# In[12]:
x = tf.placeholder("float", shape=[None, num_features])
y = tf.placeholder("float", shape=[None,1])
# In[13]:
w = tf.Variable(np.random.rand(num_features,1), dtype = np.float32)
b = tf.Variable(np.random.rand(1,1), dtype = np.float32)
# In[14]:
C = 1
batch = 100
epochs = 100
# In[15]:
y_cap = tf.matmul(x,w)+b
# In[16]:
reg_loss = 0.5*tf.math.sqrt(tf.reduce_sum(tf.square(w)))
hinge_loss = C*tf.reduce_sum(tf.square(tf.maximum(tf.zeros([batch,1]),1-y*y_cap)))
loss = reg_loss + hinge_loss
# In[17]:
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
# In[18]:
label = tf.sign(y_cap)
# In[19]:
c_pred = tf.equal(y,label)
# In[20]:
accuracy = tf.reduce_sum(tf.cast(c_pred, "float"))
# In[23]:
with tf.Session() as s:
tf.initialize_all_variables().run()
total_steps = epochs*train_size//batch
for step in range(total_steps):
print("step "+str(step)+"/"+str(total_steps)+":",end="\t")
offset = (step*batch)%train_size
b_data = trn_x[offset: (offset+batch)]
b_label = trn_y[offset: (offset+batch)]
train_step.run(feed_dict={x:b_data, y:b_label})
print("loss:"+loss.eval(feed_dict={x:b_data, y:b_label})
Detail error log:
ValueError Traceback (most recent call last)
<ipython-input-23-57d6ca3d1893> in <module>
7 b_data = trn_x[offset: (offset+batch)]
8 b_label = trn_y[offset: (offset+batch)]
----> 9 train_step.run(feed_dict={x:b_data, y:b_label})
10 print("loss:"+loss.eval(feed_dict={x:b_data, y:b_label}))
/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in run(self, feed_dict, session)
2677 none, the default session will be used.
2678 """
-> 2679 _run_using_default_session(self, feed_dict, self.graph, session)
2680
2681 _gradient_registry = registry.Registry("gradient")
/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _run_using_default_session(operation, feed_dict, graph, session)
5612 "the operation's graph is different from the session's "
5613 "graph.")
-> 5614 session.run(operation, feed_dict)
5615
5616
/usr/local/lib/python3.7/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
948 try:
949 result = self._run(None, fetches, feed_dict, options_ptr,
--> 950 run_metadata_ptr)
951 if run_metadata:
952 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python3.7/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1147 'which has shape %r' %
1148 (np_val.shape, subfeed_t.name,
-> 1149 str(subfeed_t.get_shape())))
1150 if not self.graph.is_feedable(subfeed_t):
1151 raise ValueError('Tensor %s may not be fed.' % subfeed_t)
ValueError: Cannot feed value of shape (100,) for Tensor 'Placeholder_1:0', which has shape '(?, 1)'
Upvotes: 0
Views: 3117
Reputation: 162
y = tf.placeholder("float", shape=[None])
This line fixed my issue.
Upvotes: 0
Reputation: 342
This is because you are trying to feed a placeholder with data of shape (batch,)
instead of the expected shape (None, 1)
So try reshaping trn_y
to a shape of (batch, 1) like below,
# In[23]:
with tf.Session() as s:
tf.initialize_all_variables().run()
total_steps = epochs*train_size//batch
for step in range(total_steps):
print("step "+str(step)+"/"+str(total_steps)+":",end="\t")
offset = (step*batch)%train_size
b_data = trn_x[offset: (offset+batch)]
b_label = trn_y[offset: (offset+batch)].values.reshape(batch, 1)
train_step.run(feed_dict={x:b_data, y:b_label})
print("loss:"+loss.eval(feed_dict={x:b_data, y:b_label})
Upvotes: 0
Reputation: 181
As error saying, you should match your placeholder and input data. I think your second placeholder "y" has [?,1] shape, but "b_label" has just [100] shape. you should add this code before run your network.
b_label = np.expand_dims(b_label,axis=-1)
This code makes your b_label's shape as [100,1] and this is the match size with your placeholder.
Upvotes: 1