<p>有没有可能用Keras已经存在的激活来绘制我定义的激活函数?我试着这么做:</p>
<pre class="lang-py prettyprint-override"><code>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()
</code></pre>
<p>但是上面的代码产生了一个错误:<code>AttributeError: 'Tensor' object has no attribute 'ndim'</code>。你知道吗</p>
<p>我注意到了这个<a href="https://stackoverflow.com/questions/53091121/how-to-plot-keras-activation-functions-in-a-notebook">similar question</a>,但我无法调整它以适应我的需要。我也试过玩<code>.eval()</code>像建议的<a href="https://stackoverflow.com/questions/34097281/how-can-i-convert-a-tensor-into-a-numpy-array-in-tensorflow">here</a>一样,但也没有成功。你知道吗</p>
<blockquote>
<p>I also tried playing with the <code>.eval()</code> like suggested here but also without success.</p>
</blockquote>
<p>你怎么用的?这应该起作用:</p>
<pre><code>plt.plot(x, K.eval(swish(x)))
</code></pre>