How to plot keras activation functions in a notebook

偶尔善良 提交于 2019-12-11 17:08:47

问题


I wanted to plot all Keras activation functions but some of them are not working. i.e. linear throws an error:

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

回答1:


That's because the linear activation returns the input without any modifications:

def linear(x):
    """Linear (i.e. identity) activation function.
    """
    return x

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"])

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

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

Further, exponential activation works as you have written and there is no need for modifications.



来源:https://stackoverflow.com/questions/53091121/how-to-plot-keras-activation-functions-in-a-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!