如何在笔记本中绘制Keras激活函数

我想绘制所有Keras激活函数,但其中一些无法正常工作。例如,linear会抛出错误:

AttributeError: ‘Series’ object has no attribute ‘eval’

这很奇怪。我怎样才能绘制剩余的激活函数呢?

points = 100zeros = 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激活函数返回的是未经修改的输入:

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

由于您传递的是一个Pandas Series作为输入,因此返回的也是相同的Pandas Series,因此您不需要使用K.eval()

df["linear"] = activations.linear(df["activation"])

至于selu激活函数,您需要将输入重塑为(n_samples, n_output)的形状:

df["selu"] = K.eval(activations.selu(df["activation"].values.reshape(-1,1)))

而对于hard_sigmoid激活函数,其输入必须明确是一个张量,您可以使用K.variable()来创建:

df["hard_sigmoid"] = K.eval(activations.hard_sigmoid(K.variable(df["activation"].values)))

此外,exponential激活函数按您所写的方式运行,不需要修改。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注