keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.85k stars 19.44k forks source link

RNN layer: len is not well defined for a symbolic Tensor. Please call `x.shape` rather than `len(x)` for shape information. #19754

Open ThibaultDECO opened 4 months ago

ThibaultDECO commented 4 months ago

I get the following error with keras: len is not well defined for a symbolic Tensor. Please call 'x.shape' rather than 'len(x)' for shape information.

The issue seems to originate from this line.

Here is a gist where the error is reproduced.

ThibaultDECO commented 4 months ago

Here are other places where a similar error message has been mentioned:

SuryanarayanaY commented 4 months ago

Hi @ThibaultDECO , Thanks for report. I have reproduced the issue with nightly and attached gist for reference.

grasskin commented 4 months ago

Hi @ThibaultDECO - thank you for flagging this. Would you be interested in contributing the fix? I believe just replacing len(x) with len(ops.shape(x)) should work but you would have to confirm.

Otherwise we'll take this up.

ThibaultDECO commented 3 months ago

Hi @grasskin I unfortunately do not have time to look at the issue atm

evz commented 2 weeks ago

Just ran into something similar with the bidirectional layer type. Would this line here trigger this bug? The traceback I'm getting isn't super useful in this case so I'm just trying to figure out if I need to keep digging.

ghsanti commented 2 weeks ago

Did you disable traceback filtering to get the full traceback error? @evz

evz commented 2 weeks ago

Yeah so, I turned off the traceback filtering and it is, indeed, related.

    File "/home/eric/code/coursera/.venv/lib/python3.10/site-packages/tensorflow/python/framework/tenso
r.py", line 633, in __len__
        raise TypeError(f"len is not well defined for a symbolic Tensor "

    TypeError: Exception encountered when calling Bidirectional.call().

    len is not well defined for a symbolic Tensor (states:0). Please call `x.shape` rather than `len(x)
` for shape information.

    Arguments received by Bidirectional.call():
      • sequences=tf.Tensor(shape=(1, None, 256), dtype=float32)
      • initial_state=tf.Tensor(shape=(1, 2048), dtype=float32)
      • mask=None
      • training=None

The backdrop here is that I'm working through the Shakespeare RNN example from the Tensorflow docs and toying around with adding a bidirectional layer to the model. I'm not even really sure it makes sense as a choice for that particular problem. I'm mostly just interested in figuring out how these things work.

ghsanti commented 2 weeks ago

I wonder whether x.shape is best or ops.shape(...) and then len(result).

I'm not sure about the details but I think the ops namespace deals with symbolic tensors as it seems to be the case.

ti55987 commented 4 days ago

I also encountered this issue with bi directional RNN. Is the issue fixed?