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
层的每一行,以便输出为:
我该怎么做?
答案 0 :(得分:1)
一种实现所需内容的方法是通过对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]]