# What's the usefulness of Flux.testmode!

**URL:** <https://discourse.julialang.org/t/whats-the-usefulness-of-flux-testmode/96871>\
**Category:** General Usage\
**Tags:** flux\
**Created:** [March 31, 2023, 3:23am UTC](https://discourse.julialang.org/t/whats-the-usefulness-of-flux-testmode/96871 "2023-03-31T03:23:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xuekui\_wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xuekui_wang/32/30746_2.png) [@Xuekui\_wang](https://discourse.julialang.org/u/Xuekui_wang)\
**Post date:** [March 31, 2023, 3:23am UTC](https://discourse.julialang.org/t/whats-the-usefulness-of-flux-testmode/96871/1 "2023-03-31T03:23:53Z")

</div>

I cannot understand `Flux.testmode!`and `Flux.trainmode!` well.  
When I use Flux.testmode(), I found trainloss is lower exactly,  
For example as follows:  
if using `Flux.testmode()`  
train\_pro\_loss is lower much than valid\_pro\_loss, or not, train\_pro\_loss is near valid\_pro\_loss when train data number is equal to valid data.

```julia

lossfun = () -> push!(losses, [cal_loss(train.data...)])
losses=[]
train_pro_loss, valid_pro_loss=[],[]
opt, θ = Adam(0.01), Flux.params(model)
for _ = 1:num_epochs
            for (X, y) in train
                gradients = Flux.gradient(θ) do
                    cal_loss(X, y)
                end
                Flux.update!(opt, θ, gradients)
            end
            Flux.testmode!(model)
            lossfun()
            push!(train_pro_loss, cal_loss(train.data...))
            push!(valid_pro_loss, cal_loss(valid.data...))
end

```

[https://fluxml.ai/Flux.jl/v0.12/models/layers/](https://fluxml.ai/Flux.jl/v0.12/models/layers/)

---

<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 31, 2023, 3:56am UTC](https://discourse.julialang.org/t/whats-the-usefulness-of-flux-testmode/96871/2 "2023-03-31T03:56:41Z")

</div>

You’re in luck, [Flux: trainmode!() and testmode!() needed?](https://discourse.julialang.org/t/flux-trainmode-and-testmode-needed/94155) has everything you’ll ever need to know about this and more. Also, make sure you’re on the latest version of the docs. Flux v0.12 is quite old at this point.
