keras-team / keras

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

ValueError: Layer 'talking_head_attention' looks like it has unbuilt state, but Keras is not able to trace the layer `call()` in order to build it automatically. #18699

Closed madhusshivakumar closed 10 months ago

madhusshivakumar commented 11 months ago

while upgrading "examples/vision/cait.py" to keras 3 I run into the following error.


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-6-dde8fac56e77>](https://localhost:8080/#) in <cell line: 752>()
    750 
    751 config = get_config()
--> 752 cait_xxs24_224 = CaiT(**config)
    753 
    754 dummy_inputs = keras.ops.ones((batch_size, image_size, image_size, num_channels))

4 frames
[/usr/local/lib/python3.10/dist-packages/keras/layers/layer.py](https://localhost:8080/#) in _maybe_build(self, call_spec)
   1211                     # Will let the actual eager call do state-building
   1212                     return
-> 1213                 raise ValueError(
   1214                     f"Layer '{self.name}' looks like it has unbuilt state, but "
   1215                     "Keras is not able to trace the layer `call()` in order to "

ValueError: Layer 'talking_head_attention' looks like it has unbuilt state, but Keras is not able to trace the layer `call()` in order to build it automatically. Possible causes:
1. The `call()` method of your layer may be crashing. Try to `__call__()` the layer eagerly on some test input first to see if it works. E.g. `x = np.random.random((3, 4)); y = layer(x)`
2. If the `call()` method is correct, then you may need to implement the `def build(self, input_shape)` method on your layer. It should create all variables used by the layer (e.g. by calling `layer.build()` on all its children layers).

Colab link to recreate the issue:

https://colab.research.google.com/drive/144Ugzq9peIFqHTWE9OuhXZt9-DPFjjCT?usp=sharing

divyashreepathihalli commented 11 months ago

@madhusshivakumar were you able to try the two debug options

  1. The call() method of your layer may be crashing. Try to __call__() the layer eagerly on some test input first to see if it works. E.g. x = np.random.random((3, 4)); y = layer(x)
  2. If the call() method is correct, then you may need to implement the def build(self, input_shape) method on your layer. It should create all variables used by the layer (e.g. by calling layer.build() on all its children layers).

Please let me know if that worked for you.

github-actions[bot] commented 10 months ago

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 10 months ago

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

google-ml-butler[bot] commented 10 months ago

Are you satisfied with the resolution of your issue? Yes No