【问题标题】:Why Tensorflow classification example is not using an activation function?为什么 TensorFlow 分类示例不使用激活函数?
【发布时间】:2020-10-11 17:56:40
【问题描述】:

我正在尝试按照here 提供的说明来训练二元分类器并将其用于对新图像进行预测。据我所知,二元分类器模型的末尾通常需要一个 Sigmoid 激活函数来将输出限制在 0 和 1 之间的范围内,但是这个模型没有任何 Softmax 或 Sigmoid 函数:

model = Sequential([
    Conv2D(16, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
    MaxPooling2D(),
    Conv2D(32, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Conv2D(64, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Flatten(),
    Dense(512, activation='relu'),
    Dense(1)
])

当我使用 model.predict() 命令对新图像进行预测时,模型会返回不限于任何范围的正值和负值,我不知道如何解释它们。

我还尝试在最后一个 Dense 层中添加一个 sigmoid 激活函数,Dense(1, activation='sigmoid',但是这个动作大大降低了准确性。

谁能帮我理解模型的输出?

【问题讨论】:

    标签: python tensorflow keras conv-neural-network


    【解决方案1】:

    Dense 层的默认激活函数是线性函数。如果您按照教程进行操作,您将观察到他们使用带有from_logits = True 参数的CrossEntropy 损失来编译模型。这样,在计算损失时,来自Dense(1) 层的原始预测将根据logits 转换为类别概率。

    如果您将激活切换到sigmoid,您应该使用from_logits=False 相应地修改您的损失函数,以便损失函数期望值在 [0,1] 范围内

    教程中之所以使用from_logits=True,是因为它可以产生更多的numerically stable results(根据TF)

    【讨论】:

      猜你喜欢
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 2018-01-27
      相关资源
      最近更新 更多