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

2024-04-25 14:07:39 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有可能用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'。你知道吗

我注意到了这个similar question,但我无法调整它以适应我的需要。我也试过玩.eval()像建议的here一样,但也没有成功。你知道吗


Tags: 函数fromimportnumpybackend定义matplotlibas
2条回答

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

你怎么用的?这应该起作用:

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

您需要一个会话来评估:

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

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

plt.plot(x, y)

相关问题 更多 >