Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `training=True` mean when calling a TensorFlow Keras model?

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).

I tried help(tf.keras.Model.call) and it shows that

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.

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?

like image 914
Guo Shuai Avatar asked Aug 02 '19 05:08

Guo Shuai


People also ask

How do you call a Keras model?

keras. Model . To call a model on an input, always use the __call__() method, i.e. model(inputs) , which relies on the underlying call() method. Input tensor, or dict/list/tuple of input tensors.

Why is layer trainable false?

trainable to False moves all the layer's weights from trainable to non-trainable. This is called "freezing" the layer: the state of a frozen layer won't be updated during training (either when training with fit() or when training with any custom loop that relies on trainable_weights to apply gradient updates).

How to call a keras model from a TensorFlow model?

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 on function call in module tensorflow.python.keras.engine.network: call (self, inputs, training=None, mask=None) Calls the model on new inputs.

What are the default training and evaluation loops in keras?

Description: Complete guide to writing low-level training & evaluation loops. Keras provides default training and evaluation loops, fit () and evaluate () . Their usage is covered in the guide Training & evaluation with the built-in methods.

How do I use tensorboard with Keras?

(optionally) 3D visualizations of the embedding spaces learned by your Embedding layers If you have installed TensorFlow with pip, you should be able to launch TensorBoard from the command line: The easiest way to use TensorBoard with a Keras model and the fit () method is the TensorBoard callback.

What is metrics in keras?

metrics: List of metrics to be evaluated by the model during training and testing. Each of this can be a string (name of a built-in function), function or a tf.keras.metrics.Metric instance. See tf.keras.metrics.


2 Answers

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

  • During training, dropout will randomly drop out units and correspondingly scale up activations of the remaining units.
  • During inference, it does nothing (since you usually don't want the randomness of dropping out units here).

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.

like image 145
xdurch0 Avatar answered Oct 25 '22 06:10

xdurch0


Training indicating whether the layer should behave in training mode or in inference mode.

  • training=True: The layer will normalize its inputs using the mean and variance of the current batch of inputs.

  • training=False: The layer will normalize its inputs using the mean and variance of its moving statistics, learned during training.

Usually in inference mode training=False, but in some networks such as pix2pix_cGAN‍‍‍‍‍‍ At both times of inference and training, training=True.

like image 30
NahidEbrahimian Avatar answered Oct 25 '22 06:10

NahidEbrahimian