Home > Software engineering >  Tensorflow: `tf.reshape((), (0))` works fine in eager mode but ValueError in Graph mode
Tensorflow: `tf.reshape((), (0))` works fine in eager mode but ValueError in Graph mode

Time:06-09

As the title, the function tf.reshape((), (0)) works perfectly fine in eager mode. But when I use it in Graph mode, it returns:
ValueError: Shape must be rank 1 but is rank 0 for '{{node Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32](Reshape/tensor, Reshape/shape)' with input shapes: [0], [].

Can anyone help me with the work-around of this function please. You can reproduce this error here. I want to recreate this piece of code (initialization of mems) in Tensorflow's graph mode. Thanks in advance!!

Edit: Since I want to clarify my use case, I want to convert this PyTorch torch.empty(0) function to Tensorflow (work fine in both eager mode and graph mode).

CodePudding user response:

Might be related to this bug. Try something like this:

@tf.function
def test_graph():
    x = tf.reshape((), (0, ))
    return x

b = test_graph()
b
#<tf.Tensor: shape=(0,), dtype=float32, numpy=array([], dtype=float32)>
  • Related