I'm trying to re-use a neural network for sound classification but keras give an error: AttributeError: module 'keras.src.backend' has no attribute 'Variable'. May it be a compatibility problem? I'm using keras v3.0.5. This is my code:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.optimizers import Adam
# from keras.utils import np_utils
from sklearn import metrics
from tensorflow.keras import layers
import keras
from keras import backend
num_labels = yy.shape[1]
filter_size = 2
# Construct model
model = Sequential()
model.add(Dense(256, input_shape=(40,)))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_labels))
model.add(Activation('softmax'))
model.summary()
According to Keras documentation, if I use a dropout layer:
layers.Dropout(0.5, noise_shape=None, seed=None),
it gives the same error. Can someone help me? Thanks.
You need to have a backend configured before you import keras. If not, it will never import the Variable class which might have something to do with your error. Put your setting of the backend type before importing ANY of the keras modules.
Info on how to configure the backend can be found on this page: https://keras.io/getting_started/
Copied from that page:
You can export the environment variable KERAS_BACKEND or you can edit your local config file at ~/.keras/keras.json to configure your backend. Available backend options are: "jax", "tensorflow", "torch". Example:
export KERAS_BACKEND="jax"
In Colab, you can do:
import os
os.environ["KERAS_BACKEND"] = "jax"
import keras
Note: The backend must be configured before importing Keras, and the backend cannot be changed after the package has been imported.