# Custom train function in SimpleChains.jl

**URL:** <https://discourse.julialang.org/t/custom-train-function-in-simplechains-jl/103663>\
**Category:** Machine Learning\
**Tags:** question, simplechains\
**Created:** [September 8, 2023, 12:30pm UTC](https://discourse.julialang.org/t/custom-train-function-in-simplechains-jl/103663 "2023-09-08T12:30:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luca\_Pepe\_Sciarria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luca_pepe_sciarria/32/52797_2.png) [@Luca\_Pepe\_Sciarria](https://discourse.julialang.org/u/Luca_Pepe_Sciarria)\
**Post date:** [September 8, 2023, 12:30pm UTC](https://discourse.julialang.org/t/custom-train-function-in-simplechains-jl/103663/1 "2023-09-08T12:30:31Z")

</div>

Hi SimpleChains team!

I’m currently using SimpleChains and I want to implement my own train function based on the available template in Flux:

```julia
for d in training_set
    # Our super logic
    gs = gradient(params(m)) do
        l = loss(d...)
    end
    update!(opt, params(m), gs)
end

```

What I want to do, using SimpleChains, is to incorporate a simple logic that allows me to discard certain examples during training. I want to avoid computing the gradient for these examples. Is there a specific method to do this?

I have already implemented my own logic by modifying the code in the _train\_unbatched_ function, but I would like to achieve the same result using _train\_batched_. However, _train\_batched_ appears to be more complex, and I’m finding it challenging to make the necessary modifications.

Additionally, I’ve observed that the performance (in terms of loss and accuracy) of _train\_batched_ is significantly better than _train\_unbatched_. If it’s not possible to create an explicit train function, is there a way to improve _train\_unbatched_ so that it is comparable to `train_batched`?

Any guidance or suggestions would be greatly appreciated!  
Thank you 🙂
