# Flux: Custom Training + Logging

**URL:** <https://discourse.julialang.org/t/flux-custom-training-logging/41688>\
**Category:** General Usage\
**Tags:** flux\
**Created:** [June 18, 2020, 6:53pm UTC](https://discourse.julialang.org/t/flux-custom-training-logging/41688 "2020-06-18T18:53:33Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [June 19, 2020, 4:48pm UTC](https://discourse.julialang.org/t/flux-custom-training-logging/41688/4 "2020-06-19T16:48:44Z")

</div>

I just saw an even better answer. [DiffEqFlux.jl](https://github.com/SciML/DiffEqFlux.jl/blob/master/src/train.jl#L70) uses a similar pattern in its custom training loop, but moves the logging call outside the gradient loop by declaring the loss to be `local`. This works and is a bit easier to understand if you ask me:

```julia
function my_custom_train!(loss, ps, data, opt)
  # declare training loss local so we can use it outside gradient calculation
  local training_loss                                                            
  ps = Params(ps)                                                                   
  for d in data                                                                     
    gs = gradient(ps) do                                                            
      training_loss = loss(d...)
    end                                                                             
    # Insert what ever code you want here that needs Training loss, e.g. logging
    evalcb(training_loss) # eventually want to pass out training_loss...            
                                                                                    
    # insert what ever code you want here that needs gradient                       
    # E.g. logging with TensorBoardLogger.jl as histogram so you can see if it is becoming huge
    Flux.update!(opt, ps, gs)                                                       
    # Here you might like to check validation set accuracy, and break out to do early stopping
  end                                                                               
end                                                                                 

```

---

_[View the full topic](https://discourse.julialang.org/t/flux-custom-training-logging/41688)._
