调用 TensorFlow Keras 模型时,`training=True` 的意思是什么?

22
在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是一个布尔或布尔标量张量,表示是否在 训练模式推理模式 下运行网络。但我没有找到关于这两种模式的任何信息。
总之,我不知道这个参数的影响是什么。如果我在训练时错过了这个参数会怎样?
2个回答

34

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

  • 在训练期间,Dropout将随机丢弃部分神经元,并相应地放大其余神经元的激活值。
  • 在推理(inference)期间,它不做任何事情(因为通常不希望在此处丢弃单元的随机性)。

training参数可以告诉该层应采取哪种“路径”。如果设置不正确,则您的网络可能无法按预期工作。


@Dr.PP 我认为人们在调用 fit() 时指的是训练模式,在调用 evaluate()predict() 时指的是推理模式。 - Li-Pin Juan

4

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

  • training=True: 该层将使用当前输入批次的均值和方差来规范化其输入。

  • training=False: 该层将使用其在训练期间学习到的移动统计量的均值和方差来规范化其输入。

通常在推理模式下 training=False,但在某些网络(如 pix2pix_cGAN)中,无论在推理还是训练时都使用 training=True


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接