F.dropout is only used in train, I confused how to use chainer.using_config in it? How does it works and chainer how to know it is in training or predict?
From Chainer v2, function behavior is controlled by the config
variable, according to the official doc,
chainer.config.train
Training mode flag. If it is True, Chainer runs in the training mode. Otherwise, it runs in the testing (evaluation) mode. The default value is True.
You may control this config
by following two ways.
chainer.config.train = False
here, code runs in the test mode, and dropout won't drop any unit.
model(x)
chainer.config.train = True
with using_config(key, value)
notationIf we use above case, you might need to set True
and False
often, chainer provides with using_config(key, value)
notation to ease this setting.
with chainer.using_config('train', False):
# train config is set to False, thus code runs in the test mode.
model(x)
# Here, train config is recovered to original value.
...
Note1: If you are using trainer
moudule, Evaluator
will handle these configuration automatically during validation/evaluation (see document, or source code). It means train
config is set to False
and dropout runs as evaluate mode when calculating validation loss.
Note2: train
config is used to switch "train" mode and "evaluate/validation".
If you need "predict" code, you need to implement separately.