Keras-LeakyReLU保存模型时没有属性名称错误

时间:2019-03-16 05:49:01

标签: python tensorflow keras

我在模型中使用LeakyReLU激活。我能够训练它。但是当我训练保存模型时,

discriminator_model.save(os.path.join(output_folder_path, 'discriminator_model_{0}.h5'.format(iteration_no)))

我收到以下错误

AttributeError: 'LeakyReLU' object has no attribute '__name__'

我正在使用带有tensorflow-gpu 1.12.0后端的keras-gpu 2.2.4。这是我的模型:

discriminator_model = Sequential()
discriminator_model.add(Conv2D(64, 5, strides=2, input_shape=(28, 28, 1), padding='same', activation=LeakyReLU(alpha=0.2)))
discriminator_model.add(Dropout(0.4))
discriminator_model.add(Conv2D(128, 5, strides=2, padding='same'))
discriminator_model.add(LeakyReLU(alpha=0.2))
discriminator_model.add(Dropout(0.4))
discriminator_model.add(Conv2D(256, 5, strides=2, padding='same'))
discriminator_model.add(LeakyReLU(alpha=0.2))
discriminator_model.add(Dropout(0.4))
discriminator_model.add(Conv2D(512, 5, strides=2, padding='same'))
discriminator_model.add(LeakyReLU(alpha=0.2))
discriminator_model.add(Dropout(0.4))
discriminator_model.add(Flatten())
discriminator_model.add(Dense(1))
discriminator_model.add(Activation('sigmoid'))
discriminator_model.summary()

最初,我在使用

discriminator_model.add(Conv2D(128, 5, strides=2, padding='same', activation=LeakyReLU(alpha=0.2)))

但是建议herehere将LeakyReLU添加为单独的激活层。即使尝试了也没有运气。

完整堆栈跟踪:

Traceback (most recent call last):
  File "/opt/PyCharm/pycharm-community-2018.3.3/helpers/pydev/pydevd.py", line 1741, in <module>
    main()
  File "/opt/PyCharm/pycharm-community-2018.3.3/helpers/pydev/pydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/opt/PyCharm/pycharm-community-2018.3.3/helpers/pydev/pydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/opt/PyCharm/pycharm-community-2018.3.3/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "..../Workspace/src/v01/MnistTrainer.py", line 100, in <module>
    main()
  File "..../Workspace/src/v01/MnistTrainer.py", line 92, in main
    mnist_trainer.train(train_steps=100, log_interval=1, save_interval=1)
  File "..../Workspace/src/v01/MnistTrainer.py", line 56, in train
    self.save_models(output_folder_path, i + 1)
  File "..../Workspace/src/v01/MnistTrainer.py", line 69, in save_models
    os.path.join(output_folder_path, 'discriminator_model_{0}.h5'.format(iteration_no)))
  File "..../.conda/envs/e0_270_ml_3.6/lib/python3.6/site-packages/keras/engine/network.py", line 1090, in save
    save_model(self, filepath, overwrite, include_optimizer)
  File "..../.conda/envs/e0_270_ml_3.6/lib/python3.6/site-packages/keras/engine/saving.py", line 382, in save_model
    _serialize_model(model, f, include_optimizer)
  File "..../.conda/envs/e0_270_ml_3.6/lib/python3.6/site-packages/keras/engine/saving.py", line 83, in _serialize_model
    model_config['config'] = model.get_config()
  File "..../.conda/envs/e0_270_ml_3.6/lib/python3.6/site-packages/keras/engine/sequential.py", line 278, in get_config
    'config': layer.get_config()
  File "..../.conda/envs/e0_270_ml_3.6/lib/python3.6/site-packages/keras/layers/convolutional.py", line 493, in get_config
    config = super(Conv2D, self).get_config()
  File "..../.conda/envs/e0_270_ml_3.6/lib/python3.6/site-packages/keras/layers/convolutional.py", line 226, in get_config
    'activation': activations.serialize(self.activation),
  File "..../.conda/envs/e0_270_ml_3.6/lib/python3.6/site-packages/keras/activations.py", line 176, in serialize
    return activation.__name__
AttributeError: 'LeakyReLU' object has no attribute '__name__'

2 个答案:

答案 0 :(得分:1)

编辑部分(感谢@NagabhushanSN提及其余问题)

仍有一行代码discriminator_model.add(Conv2D(64, 5, strides=2, input_shape=(28, 28, 1), padding='same', activation=LeakyReLU(alpha=0.2))),这是代码的第二行。

如果我们修改该行,则最终更正的代码应如下所示:

discriminator_model = Sequential()
discriminator_model.add(Conv2D(64, 5, strides=2, input_shape=(28, 28, 1), padding='same'))
discriminator_model.add(LeakyReLU(alpha=0.2))
discriminator_model.add(Dropout(0.4))
discriminator_model.add(Conv2D(128, 5, strides=2, padding='same'))
discriminator_model.add(LeakyReLU(alpha=0.2))
discriminator_model.add(Dropout(0.4))
discriminator_model.add(Conv2D(256, 5, strides=2, padding='same'))
discriminator_model.add(LeakyReLU(alpha=0.2))
discriminator_model.add(Dropout(0.4))
discriminator_model.add(Conv2D(512, 5, strides=2, padding='same'))
discriminator_model.add(LeakyReLU(alpha=0.2))
discriminator_model.add(Dropout(0.4))
discriminator_model.add(Flatten())
discriminator_model.add(Dense(1))
discriminator_model.add(Activation('sigmoid'))
discriminator_model.summary()

并且这个版本应该在最新版本的tensroflow上运行良好,我在1.8.0上进行了测试,效果很好。 但是,如果使用tesnorflow1.1.0等较旧的版本检查代码,则会收到相同的错误。

对于这种情况,我建议将tensorflow更新为更高的版本

  • 要检查python正在使用的当前tensorflow版本,请执行here
  • 要更新张量流,this post似乎足以说明如何做到这一点。

答案 1 :(得分:-1)

leakyrelu_alpha = 0.2    

gen5 = Conv2D(filters=256, kernel_size=3, strides=1, padding='same')(gen5)
gen5 = LeakyReLU(alpha=leakyrelu_alpha)(gen5)#Activation('relu')'or #LeakyReLU(alpha=0.3)

使用它,它将解决您的问题