简体   繁体   English

绘制使用Keras中现有功能定义的新激活功能

[英]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? 是否可以使用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' . 但是上面的代码产生错误: 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. 我也尝试像这里建议的那样使用.eval()游戏,但也没有成功。

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)

I also tried playing with the .eval() like suggested here but also without success. 我也尝试像这里建议的那样使用.eval()游戏,但也没有成功。

How did you use it? 您是如何使用它的? This should work: 这应该工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM