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

Created on 19 Sep 2016  Â·  7Comments  Â·  Source: keras-team/keras

Please make sure that the boxes below are checked before you submit your issue. Thank you!

json serialization does not work when using advanced_activations like LeakyReLU:

import keras
from keras.models import Sequential, model_from_json
from keras.layers import Dense, Activation
from keras.optimizers import SGD
from keras.layers.advanced_activations import LeakyReLU

m = Sequential([
                Dense(5 input_dim=5),
                Activation(LeakyReLU()),
                Dense(self.numHidden),
                Activation(LeakyReLU()),
                Dense(5),
                Activation("tanh")
            ], name="test")

m.to_json()
File "/home/xxx/.local/lib/python3.4/site-packages/keras/engine/topology.py", line 2629, in _updated_config
    config = self.get_config()
  File "/home/xxx/.local/lib/python3.4/site-packages/keras/models.py", line 967, in get_config
    'config': layer.get_config()})
  File "/home/xxx/.local/lib/python3.4/site-packages/keras/layers/core.py", line 219, in get_config
    config = {'activation': self.activation.__name__}
AttributeError: 'LeakyReLU' object has no attribute '__name__'
stale

Most helpful comment

@almoehi, try adding LeakyRelu directly as a layer, ie changing Activation(LeakyReLU()) to LeakyReLU().
Take a look at https://github.com/fchollet/keras/issues/2272.

All 7 comments

@almoehi, try adding LeakyRelu directly as a layer, ie changing Activation(LeakyReLU()) to LeakyReLU().
Take a look at https://github.com/fchollet/keras/issues/2272.

FWIW, I ran into a similar issue when using PReLU:
AttributeError: 'PReLU' object has no attribute '__name__'.

So I circumvented it with the following:

from keras.layers.advanced_activations import PReLU
class PRELU(PReLU):
    def __init__(self, **kwargs):
        self.__name__ = "PRELU"
        super(PRELU, self).__init__(**kwargs)

That fixed the problem.

FWIW, I ran into a similar issue when using PReLU:
AttributeError: 'PReLU' object has no attribute '__name__'.

So I circumvented it with the following:

from keras.layers.advanced_activations import PReLU
class PRELU(PReLU):
    def __init__(self, **kwargs):
        self.__name__ = "PRELU"
        super(PRELU, self).__init__(**kwargs)

That fixed the problem.

After I do it this way, the model can run normally. I use 'ModelCheckpoint' method to save the best model to a file. But when I 'load_model' from the file, it cann't distinguish the ‘PRELU’. When I transport the ‘PRELU’ to model using the parameter ‘custom_objects’ , but it still occur an error ‘Type Error: __init__( ) takes 1 positional argument but 2 were given’.
Can you help me to fix this problem. Or do you have a way to get the best model directly when the model is running, instead of saving the best model to a file.

Add LeakyReLU as shown below
model.add(LeakyReLU(alpha = 0.01))
first_model.add(Dense(25))

-> How about this? The reason why type error occur is I think due to keyword arguments (**kwargs)

but it still occur an error ‘Type Error: __init__( ) takes 1 positional argument but 2 were given’.
Can you help me to fix this problem. Or do you have a way to get the best model directly when the model is running, instead of saving the best model to a file.

from keras.layers.advanced_activations import PReLU class PRELU(PReLU): def __init__(self, *args): self.__name__ = "PRELU" super(PRELU, self).__init__(*args)

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)

use this, it will solve your problem

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)

use this, it will solve your problem

YES! this worked for me thanks

Was this page helpful?
0 / 5 - 0 ratings