Live Training with MLJFlux

This demonstration is available as a Jupyter notebook or julia script here.

Julia version is assumed to be 1.10.*

Basic Imports

using MLJ
using Flux
import RDatasets
import Optimisers
using Plots

Loading and Splitting the Data

iris = RDatasets.dataset("datasets", "iris");
y, X = unpack(iris, ==(:Species), rng=123);
X = Float32.(X);      # To be compatible with type of network network parameters

Instantiating the model

Now let's construct our model. This follows a similar setup to the one followed in the Quick Start.

NeuralNetworkClassifier = @load NeuralNetworkClassifier pkg=MLJFlux

clf = NeuralNetworkClassifier(
    builder=MLJFlux.MLP(; hidden=(5,4), σ=Flux.relu),
    optimiser=Optimisers.Adam(0.01),
    batch_size=8,
    epochs=50,
    rng=42,
)
NeuralNetworkClassifier(
  builder = MLP(
        hidden = (5, 4), 
        σ = NNlib.relu), 
  finaliser = NNlib.softmax, 
  optimiser = Adam(0.01, (0.9, 0.999), 1.0e-8), 
  loss = Flux.Losses.crossentropy, 
  epochs = 50, 
  batch_size = 8, 
  lambda = 0.0, 
  alpha = 0.0, 
  rng = 42, 
  optimiser_changes_trigger_retraining = false, 
  acceleration = CPU1{Nothing}(nothing), 
  embedding_dims = Dict{Symbol, Real}())

Now let's wrap this in an iterated model. We will use a callback that makes a plot for validation losses each iteration.

stop_conditions = [
    Step(1),            # Repeatedly train for one iteration
    NumberLimit(100),   # Don't train for more than 100 iterations
]

validation_losses =  []
gr(reuse=true)                  # use the same window for plots
function plot_loss(loss)
    push!(validation_losses, loss)
    display(plot(validation_losses, label="validation loss", xlim=(1, 100)))
    sleep(.01)  # to catch up with the plots while they are being generated
end

callbacks = [ WithLossDo(plot_loss),]

iterated_model = IteratedModel(
    model=clf,
    resampling=Holdout(),
    measures=log_loss,
    iteration_parameter=:(epochs),
    controls=vcat(stop_conditions, callbacks),
    retrain=true,
)
ProbabilisticIteratedModel(
  model = NeuralNetworkClassifier(
        builder = MLP(hidden = (5, 4), …), 
        finaliser = NNlib.softmax, 
        optimiser = Adam(0.01, (0.9, 0.999), 1.0e-8), 
        loss = Flux.Losses.crossentropy, 
        epochs = 50, 
        batch_size = 8, 
        lambda = 0.0, 
        alpha = 0.0, 
        rng = 42, 
        optimiser_changes_trigger_retraining = false, 
        acceleration = CPU1{Nothing}(nothing), 
        embedding_dims = Dict{Symbol, Real}()), 
  controls = Any[IterationControl.Step(1), EarlyStopping.NumberLimit(100), IterationControl.WithLossDo{typeof(Main.plot_loss)}(Main.plot_loss, false, nothing)], 
  resampling = Holdout(
        fraction_train = 0.7, 
        shuffle = false, 
        rng = Random._GLOBAL_RNG()), 
  measure = LogLoss(tol = 2.22045e-16), 
  weights = nothing, 
  class_weights = nothing, 
  operation = nothing, 
  retrain = true, 
  check_measure = true, 
  iteration_parameter = :epochs, 
  cache = true)

Live Training

Simply fitting the model is all we need

mach = machine(iterated_model, X, y)
fit!(mach, force=true)
trained Machine; does not cache data
  model: ProbabilisticIteratedModel(model = NeuralNetworkClassifier(builder = MLP(hidden = (5, 4), …), …), …)
  args: 
    1:	Source @598 ⏎ ScientificTypesBase.Table{AbstractVector{ScientificTypesBase.Continuous}}
    2:	Source @656 ⏎ AbstractVector{ScientificTypesBase.Multiclass{3}}

This page was generated using Literate.jl.