SpectralNormalization layer is not trainable. Please help (OperatorNotAllowedInGraphError: Exception encountered when calling layer 'spectral_normalization' (type SpectralNormalization).)
Hi @kavjayawardana ,

By looking into the error, it appears that the problem might be due to the reason that `SpectralNormalization` layer seems not supported with Graph execution.

I have set `run_eagerly= True` in `model.compile(run_eagerly= True)` then the error is gone but shape incompatibility error araised. This was corrected by changing the `y` in your code to `y = np.random.rand(batch, height-2, width-2, filters)`.

With all these modification it executes fine and attached [gist](https://colab.sandbox.google.com/gist/SuryanarayanaY/df980d43493ceeddf0d9b9f4f1242a1d/61953_spectralnormalization.ipynb) for reference.

Please cross check and confirm if still have any queries.

Thanks!

 Thank you very much @SuryanarayanaY. Setting run_eagerly= True made it trainable. Thank you again for being prompt with your reply and incredibly helpful with your solution

best

kav Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61953">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61953">No</a>

### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

No

### Source

source

### TensorFlow version

2.13.0

### Custom code

No

### OS platform and distribution

Ubuntu 22.04.3 LTS and Google Colab

### Mobile device

_No response_

### Python version

3.9

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

cudatoolkit=11.8.0, nvidia-cudnn-cu11==8.6.0.163

### GPU model and memory

_No response_

### Current behavior?

SpectralNormalization layer is not trainable. Whenever I try to use the "model.fit" method, TF outputs the error "Using a symbolic `tf.Tensor` as a Python `bool` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature."

Please help

Best

Kav

### Standalone code to reproduce the issue

```shell
LINK TO COLAB NOTEBOOK:
https://colab.research.google.com/drive/1TYoNIrrpk-bLBqpzNJXq5VOI6Mpln5Ty?usp=sharing


STANDALONE CODE:
batch = 1
height = 10
width = 10
channels = 1
filters = 4
kernel_size = 3

x_input = Input(shape=(height, width, channels))
conv2d = SpectralNormalization(Conv2D(filters, kernel_size))
x_output = conv2d(x_input)
model = Model(x_input, x_output)
model.compile(loss='mse')

x = np.random.rand(batch, height, width, channels)
y = np.random.rand(batch, height, width, filters)

model.fit(x, y)
```


### Relevant log output

```shell
---------------------------------------------------------------------------

OperatorNotAllowedInGraphError            Traceback (most recent call last)

<ipython-input-6-d3dc977168f5> in <cell line: 1>()
----> 1 model.fit(x, y)

1 frames

/usr/local/lib/python3.10/dist-packages/tensorflow/python/eager/polymorphic_function/autograph_util.py in autograph_handler(*args, **kwargs)
     50     except Exception as e:  # pylint:disable=broad-except
     51       if hasattr(e, "ag_error_metadata"):
---> 52         raise e.ag_error_metadata.to_exception(e)
     53       else:
     54         raise

OperatorNotAllowedInGraphError: in user code:

    File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1338, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1322, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1303, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1080, in train_step
        y_pred = self(x, training=True)
    File "/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
        raise e.with_traceback(filtered_tb) from None

    OperatorNotAllowedInGraphError: Exception encountered when calling layer 'spectral_normalization' (type SpectralNormalization).
    
    Using a symbolic `tf.Tensor` as a Python `bool` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.
    
    Call arguments received by layer 'spectral_normalization' (type SpectralNormalization):
      • inputs=tf.Tensor(shape=(None, 10, 10, 1), dtype=float32)
      • training=True
```
