'tensorflow import error: cannot import keras.layers

I am trying to import keras using jupyternotebook but i get an error.

in general, using tensorflow.keras.XX instead of keras.XX solves the problem but that is not the case for keras.layers . is there any other way to solve the problem? below is the code i wrote

import tensorflow as tf
import tensorflow.keras
from tensorflow.keras import backend as k
from tensorflow.keras.models import Model, load_model, save_model
from tensorflow.keras.layers import Input,Dropout,BatchNormalization,Activation,Add
from keras.layers.core import Lambda
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.pooling import MaxPooling2D
from tensorflow.keras.layers.merge import concatenate
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from tensorflow.keras import backend as K
from tensorflow.keras import optimizers

and the below is the error i get

from tensorflow.keras.preprocessing.image import array_to_img, img_to_array, load_img#,save_img

import time
t_start = time.time()

<ipython-input-51-e901beac4908> in <module>
      4 from tensorflow.keras.models import Model, load_model, save_model
      5 from tensorflow.keras.layers import Input,Dropout,BatchNormalization,Activation,Add
----> 6 from keras.layers.core import Lambda
      7 from keras.layers.convolutional import Conv2D, Conv2DTranspose
      8 from keras.layers.pooling import MaxPooling2D

/usr/local/lib/python3.5/dist-packages/keras/__init__.py in <module>
      1 from __future__ import absolute_import
      2 
----> 3 from . import utils
      4 from . import activations
      5 from . import applications

/usr/local/lib/python3.5/dist-packages/keras/utils/__init__.py in <module>
      4 from . import data_utils
      5 from . import io_utils
----> 6 from . import conv_utils
      7 from . import losses_utils
      8 from . import metrics_utils

/usr/local/lib/python3.5/dist-packages/keras/utils/conv_utils.py in <module>
      7 from six.moves import range
      8 import numpy as np
----> 9 from .. import backend as K
     10 
     11 

/usr/local/lib/python3.5/dist-packages/keras/backend/__init__.py in <module>
----> 1 from .load_backend import epsilon
      2 from .load_backend import set_epsilon
      3 from .load_backend import floatx
      4 from .load_backend import set_floatx
      5 from .load_backend import cast_to_floatx

/usr/local/lib/python3.5/dist-packages/keras/backend/load_backend.py in <module>
     88 elif _BACKEND == 'tensorflow':
     89     sys.stderr.write('Using TensorFlow backend.\n')
---> 90     from .tensorflow_backend import *
     91 else:
     92     # Try and load external backend.

/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py in <module>
     52 
     53 # Private TF Keras utils
---> 54 get_graph = tf_keras_backend.get_graph
     55 # learning_phase_scope = tf_keras_backend.learning_phase_scope  # TODO
     56 name_scope = tf.name_scope

AttributeError: module 'tensorflow.python.keras.backend' has no attribute 'get_graph'


Solution 1:[1]

dont import keras as :

import tensorflow.keras

try:

!pip install keras

then

from keras.layers import Lambda

for more details visit: https://keras.io/layers/core/

Solution 2:[2]

I think the problem is with

from keras.layers.core import Lambda

Lambda is not part of core, but layers itself! So you should use

from tf.keras.layers import Lambda

Alternatively, you can directly call Lambda as part of your model with having to explicitly import.

A quick example,

    def linear_transform(x):
       v1 = tf.Variable(1., name='multiplier')
       v2 = tf.Variable(0., name='bias')
       return x*v1 + v2

   linear_layer = tf.keras.layers.Lambda(linear_transform)
   model.add(linear_layer)
   model.add(tf.keras.layers.Dense(10, activation='relu'))
   model.add(linear_layer)  # Reuses existing Variables

Solution 3:[3]

Just use: from keras.layers import Layer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 maddy23
Solution 2 Anigasan
Solution 3 Video Trends