F.dropout只用于火车,我混淆了怎么用chainer.using_config呢? 它是如何工作的,如何知道它是在训练或预测?
答案 0 :(得分:0)
根据Chainer v2,函数行为由config
变量控制,根据official doc,
chainer.config.train
训练模式标志。如果为True,则Chainer以训练模式运行。否则,它以测试(评估)模式运行。默认值为True。
您可以通过以下两种方式控制此config
。
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)
符号如果我们使用上述情况,您可能需要经常设置True
和False
,chainer提供with using_config(key, value)
符号来简化此设置。
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.
...
注意1:如果您使用trainer
moudule,Evaluator
将在验证/评估期间自动处理这些配置(请参阅document或source code)。这意味着train
config设置为False
,dropout在计算验证丢失时作为评估模式运行。
注2:train
配置用于切换" train"模式和"评估/验证"。
如果你需要"预测"代码,你需要单独实现。