Skip to content

Commit

Permalink
resolve #195. Increase learning rate in an image test
Browse files Browse the repository at this point in the history
  • Loading branch information
ablaom committed Dec 13, 2021
1 parent 84fbe0a commit 5a838f8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
4 changes: 3 additions & 1 deletion test/classifier.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
15 changes: 8 additions & 7 deletions test/core.jl
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
Random.seed!(123)
stable_rng = StableRNGs.StableRNG(123)

rowvec(y) = y
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
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) ==
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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]
Expand All @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions test/image.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## BASIC IMAGE TESTS GREY

Random.seed!(123)
stable_rng = StableRNGs.StableRNG(123)

mutable struct MyNeuralNetwork <: MLJFlux.Builder
kernel1
Expand All @@ -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);
Expand All @@ -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)

Expand All @@ -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])
Expand Down Expand Up @@ -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]);
Expand Down
4 changes: 3 additions & 1 deletion test/regressor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
9 changes: 7 additions & 2 deletions test/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5a838f8

Please sign in to comment.