Fixing Numpy and TensorFlow ValueError: Cannot feed value of shape (x, y, z) for Tensor (…), which has shape ‘(?, x, y, z)’

TensorFlow ValueError: Cannot feed value of shape (x, y, z) for Tensor (…), which has shape ‘(?, x, y, z)’ is an error which occurs when you feed the values of a tensor which has a shape with values of a different shape.

In this article I am going to show you why this error is happening and how you can solve this error. I am also going to share with you other possible solutions which have worked for many developers who suffered from this error.

Exploring the Numpy and TensorFlow ValueError: Cannot feed value of shape (x, y, z) for Tensor (…), which has shape ‘(?, x, y, z)’

This is an error which occurs when you feed the values of a tensor which has a shape with values of a different shape.

Please double check so you can avoid mixing between different errors. The error message should look like the error message bellow.

                                                                       #
ValueError: Cannot feed value of shape (x, y, z) for Tensor (...), which has shape '(?, x, y, z)' 
                                                                       #

Bellow is a number of tested solutions that I have tried and that have worked for me.

How to get rid of the issue : using .reshape(1,x,y,z) or np.expand_dims()

We have already explained how the error occurs after feeding the values of a tensor which has a certain shape with values of a different shape.

For example the error will certainly occur after the code bellow

                                                                       #
naram = np.zeros((x, y, z))
nar= Image.open('./image.jpg')
nar= nar.resize((x, y))
naram = array(nar).reshape(x,y,z)
                                                                       #

The solution is to reshape the value to a batch with size one. the code above will become.

                                                                       #
naram = np.zeros((x, y, z))
nar= Image.open('./image.jpg')
nar= nar.resize((x, y))
image = array(img).reshape(1,x,y,z)
                                                                       #

Notice how we replaced .reshape(x,y,z) with .reshape(1,x,y,z)

A second option is to replace .reshape(x,y,z) or .reshape(1,x,y,z) with np.expand_dims() exactly like in the line bellow.

                                                                       #
np.expand_dims(nar, axis=0)
                                                                       #

This allows us to add the batch dimension directly with no hassle.

The solution above should be enough to solve the problem, if you like our effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up : 

This is the end of our article, I hope the solution we offered solved your issue, If you like our effort and explanations and want to support our team, please consider donating to our Kofi account above, keep coding and learning, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/