# \[ANN\] FluxTraining.jl

**URL:** <https://discourse.julialang.org/t/ann-fluxtraining-jl/50856>\
**Category:** Package Announcements\
**Tags:** optimization, machine-learning\
**Created:** [November 27, 2020, 10:59am UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856 "2020-11-27T10:59:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![holylorenzo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holylorenzo/32/34169_2.png) [@holylorenzo](https://discourse.julialang.org/u/holylorenzo)\
**Post date:** [November 27, 2020, 10:59am UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/1 "2020-11-27T10:59:53Z")

</div>

I am happy to announce the release of [FluxTraining.jl](https://github.com/lorenzoh/FluxTraining.jl), a deep learning training package for Flux models.

It has an extensible callback system inspired by fastai’s and comes with features like metrics, hyperparameter scheduling, TensorBoard logging and model checkpointing. It also makes it easy to customize the training loop.

You can find the documentation on all those features [here](https://lorenzoh.github.io/FluxTraining.jl/dev/README.html).

Let me know if you find it useful or something is unclear, otherwise, happy training!

---

<div class="post-metadata">

**Author:** ![holylorenzo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holylorenzo/32/34169_2.png) [@holylorenzo](https://discourse.julialang.org/u/holylorenzo)\
**Post date:** [November 29, 2020, 9:52am UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/2 "2020-11-29T09:52:12Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/3/3/33e1cff8aa2c124ba0cae96bbce85efad768dbb2.png)

This is what the TensorBoard integration looks like, by the way!

---

<div class="post-metadata">

**Author:** ![PeterKeffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peterkeffer/32/18944_2.png) [@PeterKeffer](https://discourse.julialang.org/u/PeterKeffer)\
**Post date:** [November 29, 2020, 10:57am UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/3 "2020-11-29T10:57:25Z")

</div>

Can you use this also for Deep Reinforcement Learning? Because of the ongoing generation of training data. So at the beginning there isn’t any data and the data I want to train on changes every n seconds. Haven’t looked into the code, but probably I could find a relatively easy “workaround” for my usage 🙂  
Hopefully, because your package looks very promising!

Ohh - do you. think it’s possible to use a different logger instead of TensorBoard, like Weights&Biases?

> **[Home](https://wandb.ai/site/)**
>
> The Weights & Biases MLOps platform helps AI developers streamline their ML workflow from end-to-end.

Thank you in advance for your help and your whole work on this package! 🙂

---

<div class="post-metadata">

**Author:** ![holylorenzo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holylorenzo/32/34169_2.png) [@holylorenzo](https://discourse.julialang.org/u/holylorenzo)\
**Post date:** [November 29, 2020, 12:32pm UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/4 "2020-11-29T12:32:36Z")

</div>

Hey Peter!

It’s definitely possible to use this with reinforcement learning. You can implement custom training logic by creating a new `Phase` and then implementing `fitepochphase!` and/or `fitbatchphase!` for it. [The default implementation](https://github.com/lorenzoh/FluxTraining.jl/blob/621503c3dbab0698e479ce59c8e68072e38fa087/src/train.jl#L72) should be a good starting point. As you can see, it simply loops over the data iterator, but you can overwrite that. If you only want to change the epoch (i.e. data iteration) logic, you can make your phase be a subtype of `AbstractTrainingPhase`, that way it will use the [regular `fitbatchphase!` definition](https://github.com/lorenzoh/FluxTraining.jl/blob/621503c3dbab0698e479ce59c8e68072e38fa087/src/train.jl#L91). To make it work with the callbacks, you should also throw the necessary events as is done in the default implementation.

Then you would simply call `fit!(learner, ReinforcementPhase())` (or what you called the phase).

Making a tutorial on this for the documentation is on my to-do list.

* * *

Adding Weights&Biases support should be even easier, as there is an interface specifically for creating [new logger “backends”](https://lorenzoh.github.io/FluxTraining.jl/dev/docstrings/FluxTraining.LoggerBackend.html) that can be used with [the logging callbacks](https://lorenzoh.github.io/FluxTraining.jl/dev/docs/features.html#logging). The [implementation of `TensorBoardBackend`](https://github.com/lorenzoh/FluxTraining.jl/blob/master/src/callbacks/logging/tensorboard.jl) should give enough info.

The implementation boils down to implementing `log_to` methods for the various types that can be logged. Since there is no native Julia client, the easiest way to connect this to W&B would be to use PyCall.jl to wrap the Python client.

Let me know if you try yourself on either of these, and feel free to ask for more information 🙂

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [November 29, 2020, 1:35pm UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/5 "2020-11-29T13:35:12Z")

</div>

Great premise. Because I’m selfish I’ll cue in some of the things I really rely on when I use Flux.

I very often end up writing my own optimizers and training loops, would this package be the place to make that less painful? (IE: importing things not exported in Flux, overloading, etc).

Would it be possible to put a “stop” button in? Or bake in some convenience functions for caching models as training progresses(at the users discretion of course)? VSCode lack an “interrupt” button right now, and it costs me a fair deal of time when I use things like Flux, Turing, etc, but maybe there’s a way to write a simple hook via tensorboard? Maybe that’s a bad hack for a specific user :P(me). Just some passing thoughts.

---

<div class="post-metadata">

**Author:** ![holylorenzo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holylorenzo/32/34169_2.png) [@holylorenzo](https://discourse.julialang.org/u/holylorenzo)\
**Post date:** [November 29, 2020, 4:35pm UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/6 "2020-11-29T16:35:22Z")

</div>

Writing custom training loops is possible, see what I wrote above. FluxTraining.jl works with any optimizer that works with Flux.jl if that is what you mean.

Regarding the “stop” button: I assume you’re using Alt+Enter to run things in VSCode, and that can’t be interrupted, but you can just paste the long-running code (e.g. `fit!(learner, 10)`) in the terminal manually and then it is possible to interrupt it using Ctrl+C. Would that work for you?

---

<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:** [November 29, 2020, 5:52pm UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/7 "2020-11-29T17:52:25Z")

</div>

RE optimizers specifically, [https://github.com/FluxML/Optimisers.jl](https://github.com/FluxML/Optimisers.jl) is the WIP to create a nice, Flux-compatible interface.

Support for proper interruption in VS Code is also [merged](https://github.com/julia-vscode/julia-vscode/pull/1690), so it might be in the insiders channel already?

---

<div class="post-metadata">

**Author:** ![dhairyagandhi96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dhairyagandhi96/32/7589_2.png) [@dhairyagandhi96](https://discourse.julialang.org/u/dhairyagandhi96)\
**Post date:** [November 30, 2020, 3:33pm UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/8 "2020-11-30T15:33:07Z")

</div>

There’s always `Flux.stop` and `Flux.skip`

---

<div class="post-metadata">

**Author:** ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)\
**Post date:** [November 30, 2020, 4:02pm UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/9 "2020-11-30T16:02:28Z")

</div>

The training pipeline of reinforcement learning is kind of special here.

You may take a look at [ReinforcementLearningCore.jl](https://github.com/JuliaReinforcementLearning/ReinforcementLearningCore.jl/blob/master/src/core/run.jl). Also [callbacks](https://github.com/JuliaReinforcementLearning/ReinforcementLearningCore.jl/blob/master/src/core/hooks.jl) and [stop\_conditions](https://github.com/JuliaReinforcementLearning/ReinforcementLearningCore.jl/blob/master/src/core/stop_conditions.jl).

I think the core idea behind is very close to `FluxTraining.jl`.

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [February 23, 2021, 7:50am UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/10 "2021-02-23T07:50:20Z")

</div>

First of all, thank you for a nice package.

Is there any example how to use TensorBoard with FluxTrain.

```julia
traindata, valdata = splitobs((data_X, data_Y))
trainiter, valiter = DataLoader(traindata, 128), DataLoader(valdata, 256);

learner = Learner(model, (trainiter, valiter), optim, lossfn, ToGPU(), Metrics(accuracy))

N_epochs = 500

FluxTraining.fit!(learner, N_epochs)

```

Basically, I don’t know where are values of loss functions/metrics logged.

---

<div class="post-metadata">

**Author:** ![holylorenzo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holylorenzo/32/34169_2.png) [@holylorenzo](https://discourse.julialang.org/u/holylorenzo)\
**Post date:** [February 26, 2021, 3:18pm UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/11 "2021-02-26T15:18:51Z")

</div>

You can use the `LogMetrics` callback with a TensorBoard backend:

```julia
logcb = LogMetrics(TensorBoardBackend("tblogs"))
Learner(model, (trainiter, valiter), optim, lossfn, ToGPU(), Metrics(accuracy), logcb)

```

That way, they’ll be stored in the folder “tblogs”.

If you want to access the raw metrics themselves, they are stored in `learner.cbstate.metricsstep` and `learner.cbstate.metricsepoch` as dictionaries of `MVHistory`s (see [GitHub - JuliaML/ValueHistories.jl: Utilities to efficiently track learning curves or other optimization information](https://github.com/JuliaML/ValueHistories.jl)).  
Hope that helps!

---

<div class="post-metadata">

**Author:** ![reemmasoud123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reemmasoud123/32/26181_2.png) [@reemmasoud123](https://discourse.julialang.org/u/reemmasoud123)\
**Post date:** [June 20, 2021, 3:58pm UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/12 "2021-06-20T15:58:56Z")

</div>

> [@LSTM training for a sequence of multiple features using a batch size 30](https://discourse.julialang.org/t/lstm-training-for-a-sequence-of-multiple-features-using-a-batch-size-30/63238):
>
> I am trying to do batch training using LSTM for a time series data with multiple features. Assuming I have 5000 samples and 5 features for each sample. The input uses 14 days into the past and the output is a single value on the 15th day. (My time step is 14). The size of my data is the following: xtrain: (5000,14,5) ytrain: (5000,1,1) My model is below. How do I train my data by using a batch size of 30? I tried using DataLoader and Flux.train but they are both not working with this input s…

Hello,

Is it possible to use FluxTraining to solve the issue mentioned in the post above?

---

<div class="post-metadata">

**Author:** ![holylorenzo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/holylorenzo/32/34169_2.png) [@holylorenzo](https://discourse.julialang.org/u/holylorenzo)\
**Post date:** [July 3, 2021, 4:52pm UTC](https://discourse.julialang.org/t/ann-fluxtraining-jl/50856/13 "2021-07-03T16:52:09Z")

</div>

I’ve just tagged FluxTraining.jl v0.2.0 which includes a **revamped training loop API** , making it easier to write custom training loops (or rather, make your custom training loops work with all the callbacks). See the [CHANGELOG](https://github.com/lorenzoh/FluxTraining.jl/blob/master/CHANGELOG.md) for the full list of changes and the tutorial for the new training loop API: [Custom training loops](https://lorenzoh.github.io/FluxTraining.jl/dev/docs/tutorials/training.md.html).
