Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi-target MLJFlux example #183

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/Manifest.toml
/docs/Manifest.toml
/test/Manifest.toml
.ipynb_checkpoints
*~
#*
*.bu
.DS_Store
sandbox/
docs/build
docs/build
.vscode/
2 changes: 1 addition & 1 deletion src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function plotting_report(fields, scales, history)
n_parameters = length(fields)

A = Array{Any}(undef, (n_models, n_parameters))
measurements = Vector{Float64}(undef, n_models)
measurements = Vector{eltype(first(history).measurement)}(undef, n_models)

for j in eachindex(history)
entry = history[j]
Expand Down
4 changes: 4 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ DecisionTree = "7806a523-6efd-50cb-b5f6-3fa6f1930dbb"
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
LatinHypercubeSampling = "a5e1c1ea-c99a-51d3-a14d-a9a37257b02d"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MLJBase = "a7f614a8-145f-11e9-1d2a-a57a1082229d"
MLJFlux = "094fc8d1-fd35-5302-93ea-dabda2abf845"
MLJModelInterface = "e80e1ace-859a-464e-9ed9-23947d8ae3ea"
MultivariateStats = "6f286f6a-111f-5878-ab1e-185364afe411"
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
Expand All @@ -26,7 +28,9 @@ ComputationalResources = "0.3"
DecisionTree = "0.10"
Distances = "0.10"
Distributions = "0.25"
Flux = "0.13"
MLJBase = "0.20"
MLJFlux = "0.2"
MLJModelInterface = "1.3"
MultivariateStats = "0.9"
NearestNeighbors = "0.4"
Expand Down
1 change: 1 addition & 0 deletions test/models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import MLJModelInterface
include("models/Constant.jl")
include("models/DecisionTree.jl")
include("models/NearestNeighbors.jl")
include("models/Flux.jl")
include("models/MultivariateStats.jl")
include("models/Transformers.jl")
include("models/foobarmodel.jl")
Expand Down
35 changes: 35 additions & 0 deletions test/models/Flux.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using MLJFlux, MLJ, Flux

X = randn(100, 2)
Y = X * rand(2, 2) .+ 0.1 * randn.()
XT = MLJ.table(X, names = [:x1, :x2])
YT = MLJ.table(Y, names = [:y1, :y2])
act = tanh
nn = Chain(
Dense(2, 5, act),
Dense(5, 5, act),
Dense(5, 5, act),
Dense(5, 2, identity),
)
builder = MLJFlux.@builder nn

function multi_target(loss)
(x1, x2) -> sum(map(x1, x2) do _x1, _x2
loss(_x1, _x2)
end)
end
loss = multi_target(l2)
model = MLJFlux.MultitargetNeuralNetworkRegressor(builder = builder; epochs = 10, loss)
r = (MLJ.range(model, :lambda, lower=1e-6, upper=1.0, scale=:linear), 10)
tuning = MLJ.Grid(shuffle = true)
tuned_model = MLJ.TunedModel(
model;
tuning,
resampling = MLJ.CV(nfolds = 5),
range = [r],
measure = loss,
n = 10,
check_measure = false,
)
mach = MLJ.machine(tuned_model, XT, YT)
MLJ.fit!(mach, verbosity=1)