Home > Software design >  module 'keras.backend' has no attribute 'optimizers'
module 'keras.backend' has no attribute 'optimizers'

Time:09-17

I am trying to get a code to work on google coolab, it is an old code so probably there is something wrong with imports and versions:

# IMPORT
import tensorflow as tf
from keras.models import Model
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.pooling import AveragePooling2D
from keras.layers.pooling import GlobalAveragePooling2D
from keras.layers import Input, Concatenate
#from keras.layers.normalization import BatchNormalization
from tensorflow.keras.layers import BatchNormalization
from keras.regularizers import l2

import keras.backend as K
from keras_layer_normalization import LayerNormalization
import numpy as np
import matplotlib.pyplot as plt
from keras.models import *
from keras.layers import *
from keras.optimizers import *
from keras.callbacks import ModelCheckpoint, LearningRateScheduler, ModelCheckpoint
from keras import backend as keras
#from keras.utils import plot_model
from keras.utils.vis_utils import plot_model
from keras.callbacks import EarlyStopping, TensorBoard
from keras.callbacks import ModelCheckpoint
import zipfile 
from google.colab import drive
from keras.callbacks import CSVLogger

drive.mount('/content/gdrive')


model = model_standard

opt = keras.optimizers.Adam(learning_rate=0.01, amsgrad=True)
model.compile(opt, loss = custom_loss, metrics=['mean_absolute_error', SSIM])

This is the problem that occurs when I run it:

AttributeError                            Traceback (most recent call last)
<ipython-input-91-4cf6655c0fc5> in <module>()
     32 model = model_standard
     33 
---> 34 opt = keras.optimizers.Adam(learning_rate=0.01, amsgrad=True)
     35 model.compile(opt, loss = custom_loss, metrics=['mean_absolute_error', SSIM])
     AttributeError: module 'keras.backend' has no attribute 'optimizers'

I'm using theese versions of tensorflow and keras:

print(tf.__version__)
print(tf.keras.__version__)

2.6.0
2.6.0

CodePudding user response:

You are running into the issue because of the import from keras import backend as keras.

Simply import keras as import keras and remove from keras import backend as keras

  • Related