0

I am trying to get the dimensions of a placeholder in Tensorflow. Function tf$shape immediately came to my mind. I had no trouble using tf$shape to get the shape of a placeholder with positive dimensions. However, I want my placeholder to be able to take in inputs of different sizes, so I left one dimension as NULL. Now, according to my understanding, when it comes to dimensions, Tensorflow treats NULL and -1 equivalently. However, when I run my code, I received this error:

W tensorflow/core/framework/op_kernel.cc:1148] Invalid argument: Shape [-1,2] has negative dimensions

Below is a reproducible example of my code:

a = tf$placeholder(tf$float32, shape = shape(NULL, 2L))    
sess = tf$Session()
sess$run(tf$shape(a))

Is the error cited above caused by something in my code or by the fact that tf$shape cannot take in a placeholder with negative dimensions? If the latter is true, is there any way for me to get the shape of a placeholder with negative dimensions without using tf$shape?

nnguyen24
  • 21
  • 1
  • 4

1 Answers1

1

The tf$shape(a) operation returns a tensor that contains the dynamic shape of placeholder tensor a. The dynamic shape of a depends on the value that you feed for a; if you do not feed a value, then the dynamic shape is undefined. Therefore, you must feed a value for a in the sess$run(tf$shape(a)) call.

Unfortunately the particular error you are seeing ("Shape [-1,2] has negative dimensions") is unhelpful, because of a bug in TensorFlow 1.2 regarding how the placeholder op handles partially defined shapes. If you upgrade to TensorFlow 1.3 (currently available as a release candidate) or later, this bug has been fixed, and you would get an error along the lines of:

You must feed a value for placeholder tensor 'Placeholder' with dtype float32 and shape [?,2]

mrry
  • 125,488
  • 26
  • 399
  • 400