Configuring Chainer¶
Chainer provides some global settings that affect the behavior of some functionalities. Such settings can be configured using the unified configuration system. The system provides a transparent way to manage the configuration for each process and for each thread.
The configuration is managed by two global objects: chainer.global_config
and chainer.config
.
- The
global_config
object maintains the configuration shared in the Python process. This is an instance of theGlobalConfig
class. It can be used just as a plain object, and users can freely set any attributes on it. - The
config
object, on the other hand, maintains the configuration for the current thread. This is an instance of theLocalConfig
class. It behaves like a thread-local object, and any attribute modifications are only visible to the current thread.
If no value is set to config
for a given key, global_config
is transparently referred.
Thanks to this transparent lookup, users can always use config
to read any configuration so that the thread-local configuration is used if available and otherwise the default global setting is used.
The following entries of the configuration are currently provided by Chainer. Some entries support environment variables to set the default values. Note that the default values are set in the global config.
chainer.config.cudnn_deterministic
- Flag to configure deterministic computations in cuDNN APIs.
If it is
True
, convolution functions that use cuDNN use the deterministic mode (i.e, the computation is reproducible). Otherwise, the results of convolution functions using cuDNN may be non-deterministic in exchange for the performance. The default value isFalse
. chainer.config.debug
- Debug mode flag.
If it is
True
, Chainer runs in the debug mode. See Debug mode for more information of the debug mode. The default value is given byCHAINER_DEBUG
environment variable (set to 0 or 1) if available, otherwise usesFalse
. chainer.config.enable_backprop
- Flag to enable backpropagation support.
If it is
True
, computational graphs are created during forward passes byFunctionNode
\ s, allowing backpropagation to start from anyVariable
in the graph. Otherwise, computational graphs are not created but memory consumptions are reduced. So callingbackward()
on the results of a function will not compute any gradients of any input. The default value isTrue
. chainer.config.keep_graph_on_report
- Flag to configure whether or not to let
report()
keep the computational graph. If it isFalse
,report()
does not keep the computational graph when aVariable
object is reported. It means thatreport()
stores a copy of theVariable
object which is purged from the computational graph. If it isTrue
,report()
just stores theVariable
object as is with the computational graph left attached. The default value isFalse
. chainer.config.train
- Training mode flag.
If it is
True
, Chainer runs in training mode. Otherwise, it runs in the testing (evaluation) mode. This configuration alters the behavior of e.g.chainer.functions.dropout()
andchainer.functions.batch_normalization()
. It does not reduce memory consumption or affect the creation of computational graphs required in order to compute gradients. The default value isTrue
. chainer.config.type_check
- Type checking mode flag.
If it is
True
, Chainer checks the types (data types and shapes) of inputs onFunction
applications. Otherwise, it skips type checking. The default value is given byCHAINER_TYPE_CHECK
environment variable (set to 0 or 1) if available, otherwise usesTrue
. chainer.config.use_cudnn
Flag to configure whether or not to use cuDNN. This is a ternary flag with
'always'
,'auto'
, and'never'
as its allowed values. The meaning of each flag is as follows.- If it is
'always'
, Chainer will try to use cuDNN everywhere if possible. - If it is
'auto'
, Chainer will use cuDNN only if it is known that the usage does not degrade the performance. - If it is
'never'
, Chainer will never use cuDNN anywhere.
The default value is
'auto'
.- If it is
chainer.config.autotune
- Autotune for convolutional networks flag.
If it is
True
, Chainer uses the cuDNN autotune feature to find the fastest calculation process forchainer.links.Convolution2D
,ConvolutionND
,Deconvolution2D
, orDeconvolutionND
links. The default value isFalse
.
Users can also define their own configurations. There are two ways:
- Use Chainer’s configuration objects. In this case, it is strongly recommended to prefix the name by “user_” to avoid name conflicts with configurations introduced to Chainer in the future.
- Use your own configuration objects.
Users can define their own configuration objects using
chainer.configuration.GlobalConfig
andchainer.configuration.LocalConfig
. In this case, there is no need to take care of the name conflicts.
Example
If you want to share a setting within the process, set an attribute to the global configuration.
>>> chainer.global_config.user_my_setting = 123
This value is automatically extracted by referring to the local config.
>>> chainer.config.user_my_setting
123
If you set an attribute to the local configuration, the value is only visible to the current thread.
>>> chainer.config.user_my_setting = 123
We often want to temporarily modify the configuration for the current thread.
It can be done by using using_config()
.
For example, if you only want to enable debug mode in a fragment of code, write as follows.
>>> with chainer.using_config('debug', True):
... pass # code running in the debug mode
We often want to switch to the test mode for an evaluation. This is also done in the same way.
>>> with chainer.using_config('train', False):
... pass # code running in the test mode
Note that Evaluator
automatically switches to the test mode, and thus you do not need to manually switch in the loss function for the evaluation.
You can also make your own code behave differently in training and test modes as follows.
if chainer.config.train:
pass # code only running in the training mode
else:
pass # code only running in the test mode
chainer.global_config |
Global configuration of Chainer. |
chainer.config |
Thread-local configuration of Chainer. |
chainer.using_config |
Context manager to temporarily change the thread-local configuration. |
chainer.configuration.GlobalConfig |
The plain object that represents the global configuration of Chainer. |
chainer.configuration.LocalConfig |
Thread-local configuration of Chainer. |