繁体   English   中英

使用 softmax 作为 tf.keras 中的连续层和使用 softmax 作为密集层的激活函数有什么区别?

[英]what is the difference between using softmax as a sequential layer in tf.keras and softmax as an activation function for a dense layer?

使用 softmax 作为 tf.keras 中的连续层和使用 softmax 作为密集层的激活函数有什么区别?

tf.keras.layers.Dense(10, activation=tf.nn.softmax)

tf.keras.layers.Softmax(10)

他们是一样的,你可以自己测试

# generate data
x = np.random.uniform(0,1, (5,20)).astype('float32')

# 1st option
X = Dense(10, activation=tf.nn.softmax)
A = X(x)

# 2nd option
w,b = X.get_weights()
B = Softmax()(tf.matmul(x,w) + b)

tf.reduce_all(A == B)
# <tf.Tensor: shape=(), dtype=bool, numpy=True>

使用tf.keras.layers.Softmaxtf.keras.layers.Softmax注意,它不需要指定单位,这是一个简单的激活

默认情况下,softmax 在 -1 轴上计算,如果张量输出 > 2D 并且想要在其他维度上操作 softmax,则可以更改此设置。 您可以在第二个选项中轻松更改此设置

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM