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

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

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 14
  • Comments: 16

Most upvoted comments

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

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.

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

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

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