Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

tf.reshape(tensor, [-1]) VS tf.reshape(tensor, -1)

What is the difference between these two?
1- tf.reshape(tensor, [-1])
2- tf.reshape(tensor, -1)

I can not find any difference between these two, but when I use -1 without brackets, an error occurs when trying to map the function to a TensorSliceDataset.
Here is the simplified version of the code:

def reshapeME(tensor):
    reshaped = tf.reshape(tensor,-1)

    return reshaped

new_y_test = y_test.map(reshapeME)

and here is the Error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 ValueError: Shape must be rank 1 but is rank 0 for '{{node Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32](one_hot, Reshape/shape)' with input shapes: [6], [].

If I add the bracket, there is no error. Also, there is no error when the function is used by calling and feeding a tensor.

>Solution :

tf.reshape expects a tensor or tensor-like variable as the shape in Graph mode:

A Tensor. Must be one of the following types: int32, int64. Defines the shape of the output tensor.

So, simple scalars will not work in this case. The map function of a tf.data.Dataset is always executed in Graph mode:

Note that irrespective of the context in which map_func is defined
(eager vs. graph), tf.data traces the function and executes it as a
graph.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading