【问题标题】:what is the difference between using softmax as a sequential layer in tf.keras and softmax as an activation function for a dense layer?在 tf.keras 中使用 softmax 作为顺序层和使用 softmax 作为密集层的激活函数有什么区别?
【发布时间】:2021-01-13 16:59:50
【问题描述】:

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

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

tf.keras.layers.Softmax(10)

【问题讨论】:

    标签: python tensorflow machine-learning keras computer-vision


    【解决方案1】:

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

    # 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.Softmax的时候也要注意,不需要指定单位,很简单的激活方式

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 2018-09-29
      • 2021-05-10
      • 1970-01-01
      相关资源
      最近更新 更多