绘制使用 Keras 中现有激活函数定义的新激活函数

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

是否可以绘制我使用 Keras 中已有的激活函数定义的激活函数?我试着像这样简单地做:

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

但是上面的代码产生了一个错误:AttributeError: 'Tensor' object has no attribute 'ndim'.

我注意到了这个 but I couldn't adjust it to my need. I also tried playing with the .eval() like suggested 但也没有成功。

您需要一个会话来评估:

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

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

plt.plot(x, y)

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

你是怎么用的?这应该有效:

plt.plot(x, K.eval(swish(x)))