Skip to content

Commit

Permalink
Merge pull request #12 from Evovest/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jeremiedb committed Apr 21, 2024
2 parents fdeda6d + f896998 commit a4f8dc7
Show file tree
Hide file tree
Showing 28 changed files with 1,004 additions and 437 deletions.
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
authors = ["jeremiedb <[email protected]>"]
name = "NeuroTreeModels"
uuid = "1db4e0a5-a364-4b0c-897c-2bd5a4a3a1f2"
version = "1.2.0"
authors = ["jeremiedb <[email protected]>"]
version = "1.3.0"

[deps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

## Installation

```julia-repl
```julia
] add NeuroTreeModels
```

Expand Down
5 changes: 2 additions & 3 deletions benchmarks/Higgs-logloss.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ deval = df_tot[end-1_000_000+1:end-500_000, :];
dtest = df_tot[end-500_000+1:end, :];

config = NeuroTreeRegressor(
device=:gpu,
loss=:logloss,
nrounds=200,
scaler=true,
Expand All @@ -44,7 +43,7 @@ config = NeuroTreeRegressor(
batchsize=8092,
)

@time m, logger = NeuroTreeModels.fit(
@time m = NeuroTreeModels.fit(
config,
dtrain;
deval,
Expand All @@ -53,7 +52,7 @@ config = NeuroTreeRegressor(
print_every_n=1,
early_stopping_rounds=2,
metric=:logloss,
return_logger=true
device=:gpu,
);

dinfer_eval = NeuroTreeModels.get_df_loader_infer(deval; feature_names, batchsize=config.batchsize, device=config.device);
Expand Down
12 changes: 4 additions & 8 deletions benchmarks/MSRank.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ transform!(deval, feature_names .=> percent_rank .=> feature_names)
transform!(dtest, feature_names .=> percent_rank .=> feature_names)

config = NeuroTreeRegressor(
device=:gpu,
loss=:mse,
nrounds=2,
actA=:tanh,
outsize=1,
depth=4,
ntrees=64,
stack_size=2,
Expand All @@ -66,7 +64,7 @@ config = NeuroTreeRegressor(
lr=3e-4,
)

@time m, logger = NeuroTreeModels.fit(
@time m = NeuroTreeModels.fit(
config,
dtrain;
deval,
Expand All @@ -75,15 +73,13 @@ config = NeuroTreeRegressor(
print_every_n=1,
early_stopping_rounds=3,
metric=:mse,
return_logger=true
device=:gpu,
);

dinfer_eval = NeuroTreeModels.get_df_loader_infer(deval; feature_names, batchsize=config.batchsize, device=config.device);
p_eval = m(dinfer_eval);
p_eval = m(deval);
mse_eval = mean((p_eval .- deval.y_raw) .^ 2)
@info "MSE - deval" mse_eval

dinfer_test = NeuroTreeModels.get_df_loader_infer(dtest; feature_names, batchsize=config.batchsize, device=config.device);
p_test = m(dinfer_test);
p_test = m(dtest);
mse_test = mean((p_test .- dtest.y_raw) .^ 2)
@info "MSE - dtest" mse_test
6 changes: 2 additions & 4 deletions benchmarks/YEAR-gaussian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ deval = df_tot[eval_idx, :];
dtest = df_tot[(end-51630+1):end, :];

config = NeuroTreeRegressor(
device=:gpu,
loss=:gaussian_mle,
actA=:identity,
nrounds=200,
outsize=1,
depth=4,
ntrees=32,
hidden_size=8,
Expand All @@ -62,7 +60,7 @@ config = NeuroTreeRegressor(
lr=1e-3,
)

@time m, logger = NeuroTreeModels.fit(
@time m = NeuroTreeModels.fit(
config,
dtrain;
deval,
Expand All @@ -71,7 +69,7 @@ config = NeuroTreeRegressor(
print_every_n=5,
early_stopping_rounds=2,
metric=:gaussian_mle,
return_logger=true
device=:gpu
);

# dinfer_eval = NeuroTrees.get_df_loader_infer(deval; feature_names, batchsize=config.batchsize, device=config.device);
Expand Down
6 changes: 2 additions & 4 deletions benchmarks/YEAR-mse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ deval = df_tot[eval_idx, :];
dtest = df_tot[(end-51630+1):end, :];

config = NeuroTreeRegressor(
device=:gpu,
loss=:mse,
actA=:identity,
init_scale=1.0,
Expand All @@ -51,12 +50,11 @@ config = NeuroTreeRegressor(
ntrees=32,
stack_size=1,
hidden_size=1,
outsize=1,
batchsize=2048,
lr=3e-4,
)

@time m, logger = NeuroTreeModels.fit(
@time m = NeuroTreeModels.fit(
config,
dtrain;
deval,
Expand All @@ -65,7 +63,7 @@ config = NeuroTreeRegressor(
print_every_n=5,
early_stopping_rounds=2,
metric=:mse,
return_logger=true
device=:gpu
);

# nfeats = length(feature_names)
Expand Down
76 changes: 76 additions & 0 deletions benchmarks/YEAR-tweedie.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#####################################################################
# WIP: need to adapt the fit! function to support normal DataFrame (not just GroupedOne)
# Have dataloader adapted to DF vs GDF (both at fit init and callback init)
#####################################################################

using Revise
using Random
using CSV
using DataFrames
using StatsBase
using Statistics: mean, std
using NeuroTreeModels

using AWS: AWSCredentials, AWSConfig, @service
@service S3
aws_creds = AWSCredentials(ENV["AWS_ACCESS_KEY_ID_JDB"], ENV["AWS_SECRET_ACCESS_KEY_JDB"])
aws_config = AWSConfig(; creds=aws_creds, region="ca-central-1")

path = "share/data/year/year.csv"
raw = S3.get_object("jeremiedb", path, Dict("response-content-type" => "application/octet-stream"); aws_config)
df = DataFrame(CSV.File(raw, header=false))
df_tot = copy(df)

path = "share/data/year/year-train-idx.txt"
raw = S3.get_object("jeremiedb", path, Dict("response-content-type" => "application/octet-stream"); aws_config)
train_idx = DataFrame(CSV.File(raw, header=false))[:, 1] .+ 1

path = "share/data/year/year-eval-idx.txt"
raw = S3.get_object("jeremiedb", path, Dict("response-content-type" => "application/octet-stream"); aws_config)
eval_idx = DataFrame(CSV.File(raw, header=false))[:, 1] .+ 1

transform!(df_tot, "Column1" => identity => "y_raw")
transform!(df_tot, "y_raw" => (x -> (x .- minimum(x)) ./ std(x)) => "y_norm")
select!(df_tot, Not("Column1"))
feature_names = setdiff(names(df_tot), ["y_raw", "y_norm"])
target_name = "y_norm"

function percent_rank(x::AbstractVector{T}) where {T}
return tiedrank(x) / (length(x) + 1)
end

transform!(df_tot, feature_names .=> percent_rank .=> feature_names)

dtrain = df_tot[train_idx, :];
deval = df_tot[eval_idx, :];
dtest = df_tot[(end-51630+1):end, :];

config = NeuroTreeRegressor(
loss=:tweedie_deviance,
actA=:identity,
nrounds=200,
depth=4,
ntrees=32,
batchsize=2048,
lr=1e-3,
)

@time m = NeuroTreeModels.fit(
config,
dtrain;
deval,
target_name,
feature_names,
print_every_n=5,
early_stopping_rounds=2,
metric=:tweedie_deviance,
device=:gpu
);

p_eval = m(deval);
mse_eval = mean((p_eval .- deval.y_norm) .^ 2)
@info "MSE raw - deval" mse_eval

p_test = m(dtest);
mse_test = mean((p_test .- dtest.y_norm) .^ 2) * std(df_tot.y_raw)^2
@info "MSE - dtest" mse_test
6 changes: 2 additions & 4 deletions benchmarks/Yahoo-LTRC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,21 @@ target_name = "y"
# training
#####################################
config = NeuroTreeRegressor(
device=:gpu,
loss=:logloss,
nrounds=400,
actA=:identity,
init_scale=1.0,
scaler=true,
depth=4,
ntrees=256,
outsize=1,
hidden_size=1,
stack_size=1,
batchsize=1024,
shuffle=true,
lr=3e-4,
)

@time m, logger = NeuroTreeModels.fit(
@time m = NeuroTreeModels.fit(
config,
dtrain;
deval,
Expand All @@ -119,7 +117,7 @@ config = NeuroTreeRegressor(
print_every_n=5,
early_stopping_rounds=3,
metric=:logloss,
return_logger=true
device=:gpu,
);

dinfer = NeuroTreeModels.get_df_loader_infer(dtest; feature_names, batchsize=config.batchsize, device=config.device);
Expand Down
4 changes: 1 addition & 3 deletions benchmarks/aicrowd-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,12 @@ y_train = Vector{Float32}(df_train[:, target])
y_eval = Vector{Float32}(df_eval[:, target])

config = NeuroTreeRegressor(
device = :gpu,
loss = :logloss,
nrounds = 100,
actA = :tanh,
scaler = false,
outsize = 1,
depth = 4,
ntrees = 32,
masks = nothing,
batchsize = 4096,
rng = 123,
opt = Dict("type" => "nadam", "lr" => 3e-2, "rho" => 0.9),
Expand All @@ -75,6 +72,7 @@ config = NeuroTreeRegressor(
early_stopping_rounds = 5,
print_every_n = 1,
metric = :logloss,
device = :gpu,
);

using CUDA
Expand Down
58 changes: 58 additions & 0 deletions benchmarks/titanic-logloss.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using NeuroTreeModels
using MLDatasets
using DataFrames
using Statistics: mean
using StatsBase: median
using CategoricalArrays
using Random
using CUDA
using CategoricalArrays

Random.seed!(123)

df = MLDatasets.Titanic().dataframe

# convert string feature to Categorical
transform!(df, :Sex => categorical => :Sex)
transform!(df, :Sex => ByRow(levelcode) => :Sex)

# treat string feature and missing values
transform!(df, :Age => ByRow(ismissing) => :Age_ismissing)
transform!(df, :Age => (x -> coalesce.(x, median(skipmissing(x)))) => :Age);

# remove unneeded variables
df = df[:, Not([:PassengerId, :Name, :Embarked, :Cabin, :Ticket])]

train_ratio = 0.8
train_indices = randperm(nrow(df))[1:Int(round(train_ratio * nrow(df)))]

dtrain = df[train_indices, :]
deval = df[setdiff(1:nrow(df), train_indices), :]

target_name = "Survived"
feature_names = setdiff(names(df), ["Survived"])

config = NeuroTreeRegressor(
loss=:logloss,
nrounds=400,
depth=4,
lr=3e-2,
)

m = NeuroTreeModels.fit(
config,
dtrain;
deval,
target_name,
feature_names,
metric=:logloss,
print_every_n=10,
early_stopping_rounds=3,
device=:cpu
)

p_train = m(dtrain)
p_eval = m(deval)

@info mean((p_train .> 0.5) .== (dtrain[!, target_name] .> 0.5))
@info mean((p_eval .> 0.5) .== (deval[!, target_name] .> 0.5))
Loading

2 comments on commit a4f8dc7

@jeremiedb
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/105331

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.3.0 -m "<description of version>" a4f8dc72ac6688a8232a879b4fe47a9061c77de0
git push origin v1.3.0

Please sign in to comment.