【问题标题】:What does `training=True` mean when calling a TensorFlow Keras model?调用 TensorFlow Keras 模型时,“training=True”是什么意思?
【发布时间】:2019-12-10 17:18:20
【问题描述】:

在 TensorFlow 的官方文档中,它们在训练循环中调用 Keras 模型时总是传递 training=True,例如 logits = mnist_model(images, training=True)。

我试过help(tf.keras.Model.call),结果显示

Help on function call in module tensorflow.python.keras.engine.network:

call(self, inputs, training=None, mask=None)
    Calls the model on new inputs.

    In this case `call` just reapplies
    all ops in the graph to the new inputs
    (e.g. build a new computational graph from the provided inputs).

    Arguments:
        inputs: A tensor or list of tensors.
        training: Boolean or boolean scalar tensor, indicating whether to run
          the `Network` in training mode or inference mode.
        mask: A mask or list of masks. A mask can be
            either a tensor or None (no mask).

    Returns:
        A tensor if there is a single output, or
        a list of tensors if there are more than one outputs.

它说training 是一个布尔或布尔标量张量,表示是在训练模式还是推理模式运行Network。但是我没有找到关于这两种模式的任何信息。

简而言之,我不知道这个论点的影响是什么。如果我在训练时错过了这个论点怎么办?

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    一些神经网络层在训练和推理过程中表现不同,例如 Dropout 和 BatchNormalization 层。例如

    • 在训练期间,dropout 将随机丢弃单元并相应地扩大剩余单元的激活。
    • 在推理过程中,它什么也不做(因为您通常不希望在这里丢弃单位的随机性)。

    training 参数让层知道它应该采用两条“路径”中的哪一条。如果您设置不正确,您的网络可能无法按预期运行。

    【讨论】:

    • 这有点模棱两可。是模型在训练模式下“评估”,还是模型在“训练”时进行评估?
    • 不确定你的意思;任何地方都没有提到评估。
    • @Dr.PP 我认为人们在调用fit() 时指的是训练模式,而在调用evaluate() 和predict() 时是指推理模式。
    【解决方案2】:

    请参考this答案。消除疑虑可能会有所帮助。此外,如果您需要,我可以提供更多说明。谢谢。

    【讨论】:

      【解决方案3】:

      训练指示层应该在训练模式还是推理模式下运行。

      • training=True:该层将使用当前批次输入的均值和方差对其输入进行归一化。

      • training=False:该层将使用在训练期间学习的移动统计数据的均值和方差对其输入进行归一化。

      通常在推理模式下training=False,但在某些networks如pix2pix_cGAN‍‍‍‍‍‍在推理和训练时,training=True。 p>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-24
        • 2020-01-05
        • 2019-11-05
        • 2020-03-02
        • 2016-04-12
        • 2019-08-12
        • 1970-01-01
        相关资源
        最近更新 更多