From 5a838f8efffc477fd2257a7d128af9b9e601f75a Mon Sep 17 00:00:00 2001 From: "Anthony D. Blaom" Date: Tue, 14 Dec 2021 11:28:33 +1300 Subject: [PATCH] resolve #195. Increase learning rate in an image test --- test/classifier.jl | 4 +++- test/core.jl | 15 ++++++++------- test/image.jl | 14 ++++++++++---- test/regressor.jl | 4 +++- test/test_utils.jl | 9 +++++++-- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/test/classifier.jl b/test/classifier.jl index 1ce7b879..135c3020 100644 --- a/test/classifier.jl +++ b/test/classifier.jl @@ -47,11 +47,13 @@ losses = [] # check flux model is an improvement on predicting constant # distribution: + stable_rng = StableRNGs.StableRNG(123) model = MLJFlux.NeuralNetworkClassifier(epochs=50, builder=builder, optimiser=optimiser, acceleration=accel, - batch_size=10) + batch_size=10, + rng=stable_rng) @time mach = fit!(machine(model, X, y), rows=train, verbosity=0) first_last_training_loss = MLJBase.report(mach)[1][[1, end]] push!(losses, first_last_training_loss[2]) diff --git a/test/core.jl b/test/core.jl index c0d035ba..75e03636 100644 --- a/test/core.jl +++ b/test/core.jl @@ -1,4 +1,5 @@ Random.seed!(123) +stable_rng = StableRNGs.StableRNG(123) rowvec(y) = y rowvec(y::Vector) = reshape(y, 1, length(y)) @@ -6,7 +7,7 @@ rowvec(y::Vector) = reshape(y, 1, length(y)) @test MLJFlux.MLJModelInterface.istransparent(Flux.ADAM(0.1)) @testset "nrows" begin - Xmatrix = rand(10, 3) + Xmatrix = rand(stable_rng, 10, 3) X = MLJBase.table(Xmatrix) @test MLJFlux.nrows(X) == 10 @test MLJFlux.nrows(Tables.columntable(X)) == 10 @@ -14,11 +15,11 @@ end @testset "collate" begin # NeuralNetworRegressor: - Xmatrix = broadcast(x->round(x, sigdigits=2), rand(10, 3)) + Xmatrix = broadcast(x->round(x, sigdigits=2), rand(stable_rng, 10, 3)) # convert to a column table: X = MLJBase.table(Xmatrix) - y = rand(10) + y = rand(stable_rng, 10) model = MLJFlux.NeuralNetworkRegressor() model.batch_size= 3 @test MLJFlux.collate(model, X, y) == @@ -37,7 +38,7 @@ end reshape([1; 0], (2,1))])) # MultitargetNeuralNetworRegressor: - ymatrix = rand(10, 2) + ymatrix = rand(stable_rng, 10, 2) y = MLJBase.table(ymatrix) # a rowaccess table model = MLJFlux.NeuralNetworkRegressor() model.batch_size= 3 @@ -53,7 +54,7 @@ end ymatrix'[:,7:9], ymatrix'[:,10:10]])) # ImageClassifier - Xmatrix = coerce(rand(6, 6, 1, 10), GrayImage) + Xmatrix = coerce(rand(stable_rng, 6, 6, 1, 10), GrayImage) y = categorical(['a', 'b', 'a', 'a', 'b', 'a', 'a', 'a', 'b', 'a']) model = MLJFlux.ImageClassifier(batch_size=2) @@ -68,7 +69,7 @@ end end -Xmatrix = rand(100, 5) +Xmatrix = rand(stable_rng, 100, 5) X = MLJBase.table(Xmatrix) y = Xmatrix[:, 1] + Xmatrix[:, 2] + Xmatrix[:, 3] + Xmatrix[:, 4] + Xmatrix[:, 5] @@ -95,7 +96,7 @@ model = MLJFlux.NeuralNetworkRegressor() # any model will do here chain_no_drop = deepcopy(chain_yes_drop) chain_no_drop.layers[2].p = 1.0 -test_input = rand(Float32, 5, 1) +test_input = rand(stable_rng, Float32, 5, 1) # check both chains have same behaviour before training: @test chain_yes_drop(test_input) == chain_no_drop(test_input) diff --git a/test/image.jl b/test/image.jl index f3a1ceb9..7d0ce518 100644 --- a/test/image.jl +++ b/test/image.jl @@ -1,6 +1,7 @@ ## BASIC IMAGE TESTS GREY Random.seed!(123) +stable_rng = StableRNGs.StableRNG(123) mutable struct MyNeuralNetwork <: MLJFlux.Builder kernel1 @@ -14,12 +15,12 @@ function MLJFlux.build(model::MyNeuralNetwork, rng, ip, op, n_channels) Flux.Conv(model.kernel2, 2=>1, init=init), x->reshape(x, :, size(x)[end]), Flux.Dense(16, op, init=init)) -end +end builder = MyNeuralNetwork((2,2), (2,2)) # collection of gray images as a 4D array in WHCN format: -raw_images = rand(Float32, 6, 6, 1, 50); +raw_images = rand(stable_rng, Float32, 6, 6, 1, 50); # as a vector of Matrix{<:AbstractRGB} images = coerce(raw_images, GrayImage); @@ -30,10 +31,12 @@ losses = [] @testset_accelerated "ImageClassifier basic tests" accel begin Random.seed!(123) + stable_rng = StableRNGs.StableRNG(123) model = MLJFlux.ImageClassifier(builder=builder, epochs=10, - acceleration=accel) + acceleration=accel, + rng=stable_rng) fitresult, cache, _report = MLJBase.fit(model, 0, images, labels) @@ -47,6 +50,7 @@ losses = [] # try with batch_size > 1: model = MLJFlux.ImageClassifier(builder=builder, epochs=10, batch_size=2, acceleration=accel) + model.optimiser.eta = 0.005 @time fitresult, cache, _report = MLJBase.fit(model, 0, images, labels); first_last_training_loss = _report[1][[1, end]] push!(losses, first_last_training_loss[2]) @@ -100,10 +104,12 @@ losses = [] @testset_accelerated "Image MNIST" accel begin Random.seed!(123) + stable_rng = StableRNGs.StableRNG(123) model = MLJFlux.ImageClassifier(builder=MyConvBuilder(), acceleration=accel, - batch_size=50) + batch_size=50, + rng=stable_rng) @time fitresult, cache, _report = MLJBase.fit(model, 0, images[1:500], labels[1:500]); diff --git a/test/regressor.jl b/test/regressor.jl index 41d5b48a..0b6c7c7f 100644 --- a/test/regressor.jl +++ b/test/regressor.jl @@ -27,8 +27,10 @@ train, test = MLJBase.partition(1:N, 0.7) accel) # test model is a bit better than constant predictor: + stable_rng = StableRNGs.StableRNG(123) model = MLJFlux.NeuralNetworkRegressor(builder=builder, - acceleration=accel) + acceleration=accel, + rng=stable_rng) @time fitresult, _, rpt = fit(model, 0, MLJBase.selectrows(X, train), y[train]) first_last_training_loss = rpt[1][[1, end]] diff --git a/test/test_utils.jl b/test/test_utils.jl index 942643a9..7a2728b4 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -57,9 +57,12 @@ function basictest(ModelType, X, y, builder, optimiser, threshold, accel) eval(quote + stable_rng = StableRNGs.StableRNG(123) + model = $ModelType_ex(builder=$builder, optimiser=$optimiser, - acceleration=$accel_ex) + acceleration=$accel_ex, + rng=stable_rng) fitresult, cache, _report = MLJBase.fit(model, 0, $X, $y); @@ -89,7 +92,8 @@ function basictest(ModelType, X, y, builder, optimiser, threshold, accel) model = $ModelType_ex(builder=$builder, optimiser=$optimiser, epochs=2, - acceleration=$accel_ex) + acceleration=$accel_ex, + rng=stable_rng) fitresult, cache, _report = MLJBase.fit(model, 0, $X, $y); @@ -127,6 +131,7 @@ function optimisertest(ModelType, X, y, builder, optimiser, accel) optimiser = deepcopy(optimiser) eval(quote + model = $ModelType_ex(builder=$builder, optimiser=$optimiser, acceleration=$accel_ex,