kamilazdybal
kamilazdybal

Reputation: 323

Plotting a new activation function defined using an existing one from Keras

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.

Upvotes: 1

Views: 293

Answers (2)

today
today

Reputation: 33460

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

Upvotes: 1

Dr. Snoopy
Dr. Snoopy

Reputation: 56397

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)

Upvotes: 1

Related Questions