mlech26l / ncps

PyTorch and TensorFlow implementation of NCP, LTC, and CfC wired neural models
https://www.nature.com/articles/s42256-020-00237-3
Apache License 2.0
1.93k stars 311 forks source link

module 'tensorflow.keras.layers' has no attribute 'AbstractRNNCell' #65

Open Pasha-Akito opened 4 months ago

Pasha-Akito commented 4 months ago

Hi,

I'm following this guide on using LTC with tensorflow: https://ncps.readthedocs.io/en/latest/examples/tf_first_steps.html

When running the imports as described here, I run into an issue, in particular this import: from ncps.tf import LTC

I get the following error: AttributeError: module 'tensorflow.keras.layers' has no attribute 'AbstractRNNCell'

From looking into it, it seems like tensorflow no longer supports AbstractRNNCell, this is fine as I can downgrade my tensorflow version to one which does support AbstractRNNCell.

My question is whether or not there is any intention of fixing this for newer tensorflow versions?

Thanks

noorchauhan commented 4 months ago

Yes I have faced similar issues, downgrading the Tensorflow works but that's not the good workaround, the ncp package needs to be updated for the same correction.

For start you can try other supporting versions of tensorflow.

@mlech26l , @raminmh Any insights on this bug?

gabewillen commented 4 months ago

This is actually a fairly simple fix. AbstractRNNCell was just an abstract class (Protocol) with methods stubbed out that the RNN class requires for a cell. To fix this simply replace tf.keras.layers.AbstractRNNCell with tf.keras.layers.Layer

chrootlogin commented 4 weeks ago

I also stumbled over this bug. But he already has a Keras 3-compatible version in his repo, it's just not documented. I think they should just update the documentation. Just use ncps.keras instead of ncps.tf and all your problems are solved.

import keras
from ncps import wirings
from ncps.keras import LTC

wiring = wirings.AutoNCP(8,1) # 8 neurons in total, 1 output (motor neuron)
model = keras.models.Sequential(
    [
        keras.layers.InputLayer(shape=(None, 32)),
        LTC(wiring, return_sequences=True)
    ]
)
model.compile(
    optimizer=keras.optimizers.Adam(0.01), loss='mean_squared_error'
)

model.summary()