Keras: AttributeError: module 'tensorflow.python.ops.nn' has no attribute 'leaky_relu'

Created on 10 Feb 2018  路  16Comments  路  Source: keras-team/keras

Would you please help me how to add leaky_relu or get rid of this error?

return K.relu(inputs, alpha=self.alpha)
File "C:Users\fi\Anaconda30\envs\tensorflow\lib\site-packages\keras\backend\tensorflow_backend.py", line 2918, in relu
x = tf.nn.leaky_relu(x, alpha)
AttributeError: module 'tensorflow.python.ops.nn' has no attribute 'leaky_relu'

and definition of relu in keras version (latest that I downloaded is:)
def relu(x, alpha=0., max_value=None):
"""Rectified linear unit.

With default values, it returns element-wise `max(x, 0)`.

# Arguments
    x: A tensor or variable.
    alpha: A scalar, slope of negative section (default=`0.`).
    max_value: Saturation threshold.

# Returns
    A tensor.
"""
if alpha != 0.:
    x = tf.nn.leaky_relu(x, alpha)
else:
    x = tf.nn.relu(x)

if max_value is not None:
    max_value = _to_tensor(max_value, x.dtype.base_dtype)
    x = tf.minimum(x, max_value)
return x

Most helpful comment

It works on 2.1.2, for the time being I rolled back to that version.

All 16 comments

I have the same error

I do have, too (pip-installed TF 1.3.0, Keras 2.1.3 installed via MacPorts), and I saw other people have the same problem with fresh conda installations as well.

'''
Written by Hans Meine to reproduce
AttributeError: module 'tensorflow.python.ops.nn' has no attribute 'leaky_relu'
'''

import tensorflow, keras
print(tensorflow.__version__, keras.__version__)

from keras.models import Sequential
from keras.layers import LeakyReLU
from keras.layers import InputLayer

m = Sequential()
m.add(InputLayer(input_shape=(1000,)))
m.add(LeakyReLU(alpha=0.2))

It works on 2.1.2, for the time being I rolled back to that version.

On the other hand, I just tried the latest master (f6958abb6566b1374d029c9dcbd361c37baebc3e) and still got the same problem.

Ok, I successfully used git bisect to identify f699346386e2de5a76fbed1f1c2d16c6fa1d9000 as the culprit! Which is not surprising, because before that commit, tf.nn.leaky_relu() was not used.

Maybe Keras needs a TF > 1.3.0 now?

Works after upgrading to TF 1.5.0. What could be done about this? Better error message in Keras indicating too old TF version?

I have faced the same problem. switched back to 2.1.2.

For me tf was 1.3 and the error was removed after upgrading to 1.5 using conda forge.

upgrading to 1.5 does not solve the error...still i get "module 'tensorflow.python.ops.nn' has no attribute 'leaky_relu'"

I have also upgraded to 1.5 too but the error is still there.

Worked when I switched to keras 2.1.2 irrespective of tensorflow version (1.3->1.5 all worked after regressing to keras 2.1.2).

save problem. solved after regressing to keras 2.1.2

How do you regress or upgrade keras versions? I'm getting this error and have no idea what version keras I'm using either which I'd better find out how to check.

AttributeError: module 'tensorflow.python.ops.nn' has no attribute 'leaky_relu'

@nyck33 I downgraded my keras version using :
conda install -c conda keras==2.1.2

Closing as this is resolved

Was this page helpful?
0 / 5 - 0 ratings