Skip to content

Do I call Module::forward() or Module::forward_mut()?

Corey Lowman edited this page Aug 20, 2022 · 1 revision

They are both used in different situations for different things:

  1. Module::forward() doesn't allow the module to be mutated (i.e. every sub-module will receive &self).
  2. Module::forward_mut() allows the module to be mutated (i.e. every sub-module will receive &mut self).

In general:

  • For training, use Module::forward_mut(), which will allow Dropout & BatchNorm (and others that have running statistics) to update themselves.
  • For evaluation/testing use Module::forward().