keras.activations.softmax
和 keras.layers.Softmax
有什么区别?为什么同一个激活函数有两个定义?
keras.activations.softmax
: https://keras.io/activations/
keras.layers.Softmax
: https://keras.io/layers/advanced-activations/
回答:
它们在功能上是等价的。实际上,Softmax
层在内部会调用 activations.softmax
函数:
def call(self, inputs): return activations.softmax(inputs, axis=self.axis)
然而,它们的区别在于,Softmax
层可以直接作为一个层使用:
from keras.layers import Softmaxsoft_out = Softmax()(input_tensor)
而 activations.softmax
不能直接作为一个层使用。相反,你可以通过 activation
参数将其传递给其他层的激活函数:
from keras import activationsdense_out = Dense(n_units, activation=activations.softmax)
此外,使用 Softmax
层的优点是它接受一个 axis
参数,你可以计算输入的另一个轴上的 softmax,而不是默认的最后一个轴:
soft_out = Softmax(axis=desired_axis)(input_tensor)