# Flux: trainmode!() and testmode!() needed?

**URL:** https://discourse.julialang.org/t/flux-trainmode-and-testmode-needed/94155
**Category:** High Energy Physics
**Tags:** flux
**Created:** [February 6, 2023, 6:17pm UTC](https://discourse.julialang.org/t/flux-trainmode-and-testmode-needed/94155 "2023-02-06T18:17:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [February 6, 2023, 6:17pm UTC](https://discourse.julialang.org/t/flux-trainmode-and-testmode-needed/94155/1 "2023-02-06T18:17:47Z")

</div>

I started using Flux recently, and I am wondering when `Flux.trainmode!()` and `Flux.testmode!()` need to be called.

Am I correct that these functions do not need to be called explicitly during normal training and testing, no matter how complex the model is? I find that a simple example shown in [this Flux documentation](https://fluxml.ai/Flux.jl/stable/tutorials/2021-02-07-convnet/#Model) does not use these functions at all.

Also, if `Flux.trainmode!()` and `Flux.testmode!()` do not need to be called explicitly during normal Flux usage, then when do these functions need to be called?

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [February 7, 2023, 1:03am UTC](https://discourse.julialang.org/t/flux-trainmode-and-testmode-needed/94155/2 "2023-02-07T01:03:56Z")

</div>

Have you seen [Built-in Layers · Flux](https://fluxml.ai/Flux.jl/stable/models/layers/#Test-vs.-Train)? The idea is that sometimes you want to override the automatic behaviour. For example, some training regimes may freeze batchnorm stat updates temporarily.

---

<div class="post-metadata">

### Author: ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)
#### Post date: [March 20, 2023, 6:52pm UTC](https://discourse.julialang.org/t/flux-trainmode-and-testmode-needed/94155/3 "2023-03-20T18:52:33Z")

</div>

Thanks, and sorry that I haven’t followed up for a while.

Then is it correct to say that it doesn’t hurt to use `trainmode!()` during training and `testmode!()` during testing explicitly?

More generally, how can I write code that uses the automatic behavior and code that doesn’t? Could you show two sets of code that achieve the same result, one using the automatic behavior and the other that doesn’t?

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [March 20, 2023, 9:16pm UTC](https://discourse.julialang.org/t/flux-trainmode-and-testmode-needed/94155/4 "2023-03-20T21:16:52Z")

</div>

It doesn’t hurt, but you’d have to remember to switch back afterwards if you want to replicate the automatic behaviour. Remember that if you explicitly disable the automatic behaviour by using true/false, you can always re-enable it by passing `mode = :auto` or `mode = nothing`.

> [@wsshin](#):
>
> More generally, how can I write code that uses the automatic behavior and code that doesn’t? Could you show two sets of code that achieve the same result, one using the automatic behavior and the other that doesn’t?

There’s no real difference. Just call `trainmode!(model)` before you want to enable `BatchNorm` stats updating/`Dropout` dropout, and call `testmode!(model)` when you want to stop. Most likely these would be before and after the `gradient` call in your training loop, respectively.
