1

Is it possible to plot an activation function that I define using an already existing activation from Keras? I tried doing it simply like this:

import keras
from keras import backend as K
import numpy as np
import matplotlib.pyplot as plt

# Define swish activation:
def swish(x):
    return K.sigmoid(x) * x

x = np.linspace(-10, 10, 100)

plt.plot(x, swish(x))
plt.show()

but the above code produces an error: AttributeError: 'Tensor' object has no attribute 'ndim'.

I've noticed this similar question but I couldn't adjust it to my need. I also tried playing with the .eval() like suggested here but also without success.

today
  • 32,602
  • 8
  • 95
  • 115
kamilazdybal
  • 303
  • 3
  • 9

2 Answers2

1

You need a session to evaluate:

x = np.linspace(-10, 10, 100)

with tf.Session().as_default():
    y = swish(x).eval()

plt.plot(x, y)
Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
1

I also tried playing with the .eval() like suggested here but also without success.

How did you use it? This should work:

plt.plot(x, K.eval(swish(x)))
today
  • 32,602
  • 8
  • 95
  • 115
  • Thanks, even simpler solution. What I tried was to place `eval()` like this: `K.eval(sigmoid(x)) * x` inside the function definition. I actually don't know why it doesn't work, the error is: `NameError: name 'sigmoid' is not defined`. – kamilazdybal May 22 '19 at 20:23
  • 1
    @camillejr Of course it should not work, because `sigmoid` is defined inside backend module, so you must write: `K.eval(K.sigmoid(x)) * x` – today May 23 '19 at 05:59