module 'keras.backend' has no attribute 'optimizers'


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 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


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
---> 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:




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

Answered By – Kishore

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published