在Keras中,如何对权重矩阵的每一行应用Softmax函数?

from keras.models import Modelfrom keras.models import Inputfrom keras.layers import Densea = Input(shape=(3,))b = Dense(2, use_bias=False)(a)model = Model(inputs=a, outputs=b)

假设上述代码中Dense层的权重为[[2, 3], [3, 1], [-1, 1]]。如果我们将[[2, 1, 3]]作为输入传递给model,那么输出将是:

no softmax

但我想对Dense层的每一行应用Softmax函数,这样输出将是:

with softmax

我该如何实现这一点?


回答:

实现你所需功能的一种方法是通过子类化Dense层并重写其call方法来定义一个自定义层:

from keras import backend as Kclass CustomDense(Dense):    def __init__(self, units, **kwargs):        super(CustomDense, self).__init__(units, **kwargs)    def call(self, inputs):        output = K.dot(inputs, K.softmax(self.kernel, axis=-1))        if self.use_bias:            output = K.bias_add(output, self.bias, data_format='channels_last')        if self.activation is not None:            output = self.activation(output)        return output

测试以确保它工作正常:

model = Sequential()model.add(CustomDense(2, use_bias=False, input_shape=(3,)))model.compile(loss='mse', optimizer='adam')import numpy as npw = np.array([[2,3], [3,1], [1,-1]])inp = np.array([[2,1,3]])model.layers[0].set_weights([w])print(model.predict(inp))# output[[4.0610714 1.9389288]]

使用numpy验证结果:

soft_w = np.exp(w) / np.sum(np.exp(w), axis=-1, keepdims=True)print(np.dot(inp, soft_w))[[4.06107115 1.93892885]]

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中创建了一个多类分类项目。该项目可以对…

发表回复

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