如何在笔记本中绘制keras激活功能 [英] How to plot keras activation functions in a notebook
问题描述
我想绘制所有Keras激活功能,但其中一些无法正常工作.即linear
会引发错误:
I wanted to plot all Keras activation functions but some of them are not working. i.e. linear
throws an error:
AttributeError:系列"对象没有属性"eval"
AttributeError: 'Series' object has no attribute 'eval'
这很奇怪.如何绘制其余的激活函数?
which is weird. How can I plot the rest of my activation functions?
points = 100
zeros = np.zeros((points,1))
df = pd.DataFrame({"activation": np.linspace(-1.2,1.2,points)})
df["softmax"] = K.eval(activations.elu(df["activation"]))
#df["linear"] = K.eval(activations.linear(df["activation"]))
df["tanh"] = K.eval(activations.tanh(df["activation"]))
df["sigmoid"] = K.eval(activations.sigmoid(df["activation"]))
df["relu"] = K.eval(activations.relu(df["activation"]))
#df["hard_sigmoid"] = K.eval(activations.hard_sigmoid(df["activation"]))
#df["exponential"] = K.eval(activations.exponential(df["activation"]))
df["softsign"] = K.eval(activations.softsign(df["activation"]))
df["softplus"] = K.eval(activations.softplus(df["activation"]))
#df["selu"] = K.eval(activations.selu(df["activation"]))
df["elu"] = K.eval(activations.elu(df["activation"]))
df.plot(x="activation", figsize=(15,15))
推荐答案
这是因为linear
激活返回的输入未经任何修改:
That's because the linear
activation returns the input without any modifications:
def linear(x):
"""Linear (i.e. identity) activation function.
"""
return x
由于您将熊猫系列作为输入传递,因此将返回相同的熊猫系列,因此您无需使用K.eval()
:
Since you are passing a Pandas Series as input, the same Pandas Series will be returned and therefore you don't need to use K.eval()
:
df["linear"] = activations.linear(df["activation"])
对于selu
激活,您需要将输入的形状调整为(n_samples, n_output)
:
As for the selu
activation, you need to reshape the input to (n_samples, n_output)
:
df["selu"] = K.eval(activations.selu(df["activation"].values.reshape(-1,1)))
对于hard_sigmoid
激活,其输入应明确为张量,您可以使用K.variable()
创建该张量:
And as for the hard_sigmoid
activation, its input should be explicitly a Tensor which you can create using K.variable()
:
df["hard_sigmoid"] = K.eval(activations.hard_sigmoid(K.variable(df["activation"].values)))
此外,exponential
激活按您编写的方式工作,无需修改.
Further, exponential
activation works as you have written and there is no need for modifications.
这篇关于如何在笔记本中绘制keras激活功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!