keras.models.load_model broken if custom objects present (.keras format)
Hi @optiluca ,

Thanks for reporting this. I have replicated the reported behaviour with TF2.13v and also tf-nightly versions whereas TF2.12v it seems working fine. Attached [gist](https://colab.research.google.com/gist/SuryanarayanaY/85eef0845746d2c89e5b016bf7ea6676/61270.ipynb) for reference.

This seems regression issue for me. Needs to check the root cause. If you are willing to contribute please feel free to raise a PR.

Thanks! Hi @optiluca ,

I have tried a simple code with custom object and it seems working fine and I can able to save and reload model. Please refer the attached [gist](https://colab.research.google.com/gist/SuryanarayanaY/08d7cc3ece073ec8e974d9d6c71b72dd/custom-objects_serialization_and_saving-2.ipynb) here. This seems not a regression issue. 

May be we need to check the code for the Custom class (i.e.class MyCustomLSTM) of your model to check the root cause.There may be chance that it might not have implementation of `from_config()`  and `get_config()` methods.


Please note that Custom class should implement `from_config()` which is a class method and `get_config()` methods to make it able to serialize and deserialize. Please refer the attached documentation [source](https://www.tensorflow.org/guide/keras/serialization_and_saving#custom_objects) for more details on how to implement custom classes.

Thanks! Hi @SuryanarayanaY , thanks for the prompt feedback.  

My example code defines a custom layer that is *exactly* the same as the keras.layers.LSTM layer.  For this reason, it does not need custom from_config and get_config methods.  Clearly my real use case is more complex, and does define a custom get_config, but the minimal (not) working example is as per my original example code.

The example you share looks quite different to mine, apparently to a point that it sidesteps this bug, whatever the root cause.

I modified your example to make it break again.  It seems that the bug is triggered specifically if LSTM layers are involved, new gist [here ](https://colab.research.google.com/gist/optiluca/8b254fba9e7b0cdef59b9f4bbb0e0258/custom-objects_serialization_and_saving-2.ipynb)to demonstrate:

 Hi @optiluca ,

It seems custom layer sub classed from LSTM layer has problem when loading in `.keras` format but with .tf format it is working fine. The problem exists in tf-nightly also. Attached [gist](https://colab.sandbox.google.com/gist/SuryanarayanaY/c90a99527674a192b25cfe61a9192e0e/61270_custom-objects_serialization_and_saving-2.ipynb) for reference.

There seems some bug AFAIK which needs to look into. Will dig more to check or escalate it to concerned team. Thanks!
### Issue type

Bug

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

No

### Source

binary

### TensorFlow version

tf2.13

### Custom code

Yes

### OS platform and distribution

All

### Mobile device

_No response_

### Python version

3.8

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

Models do not load.  Errors vary, depending on the number of training samples being divisible by batch size or not.

### Standalone code to reproduce the issue

```shell
import keras
import numpy as np


class MyCustomLSTM(keras.layers.LSTM):
    """
    Custom LSTM
    """


model = keras.models.Sequential([MyCustomLSTM(32), keras.layers.Dense(1)])

model.compile(keras.optimizers.Adam(), keras.losses.mse)

x = np.zeros((1024, 300, 1))
y = np.zeros((1024, 1))

history = model.fit(x, y, batch_size=32, epochs=1)

model.save('test.keras')

model_loaded = keras.models.load_model('test.keras', custom_objects={'MyCustomLSTM': MyCustomLSTM})
# This throws:
# IndexError: list assignment index out of range

# If the number of samples is not divisible by the batch size (e.g. 1025 samples, instead of 1024), the error becomes:
# ValueError: as_list() is not defined on an unknown TensorShape.
```


### Relevant log output

_No response_