调用 TensorFlow Keras 模型时,“training=True"是什么意思? [英] What does `training=True` mean when calling a TensorFlow Keras model?

查看:252
本文介绍了调用 TensorFlow Keras 模型时,“training=True"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In TensorFlow's offcial documentations, they always pass training=True when calling a Keras model in a training loop, for example, 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模式.但是我没有找到关于这两种模式的任何信息.

It says that training is a Boolean or boolean scalar tensor, indicating whether to run the Network in training mode or inference mode. But I didn't find any information about this two modes.

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

In a nutshell, I don't know what is the influence of this argument. And what if I missed this argument when training?

推荐答案

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

Some neural network layers behave differently during training and inference, for example Dropout and BatchNormalization layers. For example

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

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

The training argument lets the layer know which of the two "paths" it should take. If you set this incorrectly, your network might not behave as expected.

这篇关于调用 TensorFlow Keras 模型时,“training=True"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆