tensorflow导入错误:无法导入keras.layers

时间:2019-10-05 09:31:18

标签: keras-layer

我正在尝试使用jupyternotebook导入keras,但出现错误。

通常,使用tensorflow.keras.XX代替keras.XX可以解决问题,但keras.layers并非如此。还有其他解决方法吗? 下面是我写的代码

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

以下是我得到的错误

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'

2 个答案:

答案 0 :(得分:0)

不要将keras导入为:

导入tensorflow.keras

尝试:

!pip安装keras

然后

从keras.layers导入Lambda

有关更多详细信息,请访问: https://keras.io/layers/core/

答案 1 :(得分:0)

我认为问题出在

from keras.layers.core import Lambda

Lambda不是核心的一部分,而是分层本身!所以你应该使用

from tf.keras.layers import Lambda

或者,您可以直接调用Lambda作为模型的一部分,而不必显式导入。

一个简单的示例

    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