Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
almoehi opened this issue Sep 19, 2016 · 7 comments
Closed
2 tasks done

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

almoehi opened this issue Sep 19, 2016 · 7 comments

Comments

@almoehi
Copy link

almoehi commented Sep 19, 2016

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

  • Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps
  • If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
    pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps

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__'
@grovina
Copy link

grovina commented Dec 20, 2016

@almoehi, try adding LeakyRelu directly as a layer, ie changing Activation(LeakyReLU()) to LeakyReLU().
Take a look at #2272.

@stale stale bot added the stale label May 23, 2017
@stale stale bot closed this as completed Jun 22, 2017
decaelus added a commit to BoiseStatePlanetary/Exoplanet-Artificial-Intelligence that referenced this issue Apr 19, 2018
Generated data

There is an issue with keras described here - keras-team/keras#3816. I modified model_fit_history.py to circumvent it.

I also saved some figures.
@decaelus
Copy link

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.

@xiaoxing139
Copy link

xiaoxing139 commented Feb 28, 2019

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.

@Manoj-Virigineni
Copy link

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

@4pygmalion
Copy link

4pygmalion commented Jan 29, 2020

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

@AnasHXH
Copy link

AnasHXH commented Sep 16, 2020

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

@NerminSiphocly
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants