Pav*_*tty 5 python machine-learning computer-vision keras tensorflow
使用 softmax 作为 tf.keras 中的连续层和使用 softmax 作为密集层的激活函数有什么区别?
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
Run Code Online (Sandbox Code Playgroud)
和
tf.keras.layers.Softmax(10)
Run Code Online (Sandbox Code Playgroud)
是一样的,你可以自己测试一下
# 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>
Run Code Online (Sandbox Code Playgroud)
使用时也要注意tf.keras.layers.Softmax,不需要指定单位,只是简单的激活
默认情况下,softmax 在 -1 轴上计算,如果您的张量输出 > 2D 并且想要在其他维度上操作 softmax,则可以更改此设置。您可以在第二个选项中轻松更改此设置
| 归档时间: |
|
| 查看次数: |
500 次 |
| 最近记录: |