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]]
关于python - 在Keras中,如何在权重矩阵的每一行上应用softmax函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53273456/