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

In Keras, how to apply softmax function on each row of the weight matrix?

from keras.models import Model
from keras.models import Input
from keras.layers import Dense

a = 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 的输入,那么输出将是:

但我想将 softmax 函数应用于 Dense 层的每一行,以便输出为:

我该怎么做?

一种实现您正在寻找的方法是通过子类化 Dense 层并覆盖其 call 方法来定义自定义层:

from keras import backend as K

class 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 np

w = 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]]