-
Notifications
You must be signed in to change notification settings - Fork 6
Ac/gpu support #256
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
base: main
Are you sure you want to change the base?
Ac/gpu support #256
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,12 @@ | ||
| export prepare_data | ||
|
|
||
| function prepare_data(hm, data::KeyedArray; kwargs...) | ||
| predictors_forcing, targets = get_prediction_target_names(hm) | ||
| function prepare_data(hm, data::KeyedArray; cfg=DataConfig(), kwargs...) | ||
| predictors, forcings, targets = get_prediction_target_names(hm) | ||
| # KeyedArray: use () syntax for views that are differentiable | ||
| return (data(predictors_forcing), data(targets)) | ||
| dev = cfg.gdev | ||
| targets_nt = NamedTuple([target => dev(Array(data(target))) for target in targets]) | ||
| forcings_nt = NamedTuple([forcing => dev(Array(data(forcing))) for forcing in forcings]) | ||
| return ((dev(Array(data(predictors))), forcings_nt), targets_nt) | ||
| end | ||
|
|
||
| function prepare_data(hm, data::AbstractDimArray; kwargs...) | ||
|
|
@@ -13,10 +16,10 @@ function prepare_data(hm, data::AbstractDimArray; kwargs...) | |
| end | ||
|
|
||
| function prepare_data(hm, data::DataFrame; array_type = :KeyedArray, drop_missing_rows = true) | ||
| predictors_forcing, targets = get_prediction_target_names(hm) | ||
| predictors, forcings, targets = get_prediction_target_names(hm) | ||
|
|
||
| all_predictor_cols = unique(vcat(values(predictors_forcing)...)) | ||
| col_to_select = unique([all_predictor_cols; targets]) | ||
| # all_predictor_cols = unique(vcat(values(predictors_forcing)...)) | ||
| col_to_select = unique([predictors; forcings; targets]) | ||
|
|
||
| # subset to only the cols we care about | ||
| sdf = data[!, col_to_select] | ||
|
|
@@ -84,33 +87,36 @@ Returns a tuple of (predictors_forcing, targets) names. | |
| function get_prediction_target_names(hm) | ||
| targets = hm.targets | ||
| predictors_forcing = Symbol[] | ||
| predictors = Symbol[] | ||
| forcings = Symbol[] | ||
| for prop in propertynames(hm) | ||
| if occursin("predictors", string(prop)) | ||
| val = getproperty(hm, prop) | ||
| if isa(val, AbstractVector) | ||
| append!(predictors_forcing, val) | ||
| append!(predictors, val) | ||
| elseif isa(val, Union{NamedTuple, Tuple}) | ||
| append!(predictors_forcing, unique(vcat(values(val)...))) | ||
| append!(predictors, unique(vcat(values(val)...))) | ||
| end | ||
| end | ||
| end | ||
| for prop in propertynames(hm) | ||
| if occursin("forcing", string(prop)) | ||
| val = getproperty(hm, prop) | ||
| if isa(val, AbstractVector) | ||
| append!(predictors_forcing, val) | ||
| append!(forcings, val) | ||
| elseif isa(val, Union{Tuple, NamedTuple}) | ||
| append!(predictors_forcing, unique(vcat(values(val)...))) | ||
| append!(forcings, unique(vcat(values(val)...))) | ||
| end | ||
| end | ||
| end | ||
| predictors_forcing = unique(predictors_forcing) | ||
| # predicto | ||
| # predictors_forcing = unique(predictors_forcing) | ||
|
Comment on lines
+112
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| if isempty(predictors_forcing) | ||
| @warn "Note that you don't have predictors or forcing variables." | ||
| end | ||
| if isempty(targets) | ||
| @warn "Note that you don't have target names." | ||
| end | ||
| return predictors_forcing, targets | ||
| return predictors, forcings, targets | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function now returns |
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,14 +18,14 @@ Main loss function for hybrid models that handles both training and evaluation m | |
| - `(loss_values, st, ŷ)`: NamedTuple of losses, state and predictions | ||
| """ | ||
| function compute_loss( | ||
| HM::LuxCore.AbstractLuxContainerLayer, ps, st, (x, (y_t, y_nan)); | ||
| HM::LuxCore.AbstractLuxContainerLayer, ps, st, ((x, forcings), (y_t, y_nan)); | ||
| logging::LoggingLoss | ||
| ) | ||
|
|
||
| targets = HM.targets | ||
| ext_loss = extra_loss(logging) | ||
| if logging.train_mode | ||
| ŷ, st = HM(x, ps, st) | ||
| ŷ, st = HM((x, forcings), ps, st) | ||
| loss_value = _compute_loss(ŷ, y_t, y_nan, targets, training_loss(logging), logging.agg) | ||
| # Add extra_loss if provided | ||
| if ext_loss !== nothing | ||
|
|
@@ -34,7 +34,7 @@ function compute_loss( | |
| end | ||
| stats = NamedTuple() | ||
| else | ||
| ŷ, _ = HM(x, ps, LuxCore.testmode(st)) | ||
| ŷ, _ = HM((x, forcings), ps, LuxCore.testmode(st)) | ||
| loss_value = _compute_loss(ŷ, y_t, y_nan, targets, loss_types(logging), logging.agg) | ||
| # Add extra_loss entries if provided | ||
| if ext_loss !== nothing | ||
|
|
@@ -105,9 +105,10 @@ _get_target_ŷ(ŷ, y_t, target) = | |
| function assemble_loss(ŷ, y, y_nan, targets, loss_spec) | ||
| return [ | ||
| begin | ||
| y_t = _get_target_y(y, target) | ||
| ŷ_t = _get_target_ŷ(ŷ, y_t, target) | ||
| _apply_loss(ŷ_t, y_t, _get_target_nan(y_nan, target), loss_spec) | ||
| y_t = y[target]# _get_target_y(y, target) | ||
| ŷ_t = ŷ[target]#_get_target_ŷ(ŷ, y_t, target) | ||
|
Comment on lines
+108
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo in line 109: y_t = y[target]
ŷ_t = ŷ[target] |
||
| _apply_loss(ŷ_t, y_t, y_nan, loss_spec) | ||
| # _apply_loss(ŷ_t, y_t, _get_target_nan(y_nan, target), loss_spec) | ||
| end | ||
| for target in targets | ||
| ] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,8 +69,7 @@ function loss_fn(ŷ, y, y_nan, ::Val{:pearson}) | |
| return cor(ŷ[y_nan], y[y_nan]) | ||
| end | ||
| function loss_fn(ŷ, y, y_nan, ::Val{:r2}) | ||
| r = cor(ŷ[y_nan], y[y_nan]) | ||
| return r * r | ||
| return 1 - sum((y[y_nan] .- ŷ[y_nan]).^2) / sum((y[y_nan] .- mean(ŷ[y_nan])).^2) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The R-squared calculation is incorrect. The denominator should use the mean of the observed values ( return 1 - sum((y[y_nan] .- ŷ[y_nan]).^2) / sum((y[y_nan] .- mean(y[y_nan])).^2) |
||
| end | ||
|
|
||
| function loss_fn(ŷ, y, y_nan, ::Val{:pearsonLoss}) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,18 +8,18 @@ mutable struct EarlyStopping | |
| done::Bool | ||
| end | ||
|
|
||
| function EarlyStopping(init_loss, ps, st, patience::Int) | ||
| function EarlyStopping(init_loss, ps, st, cfg) | ||
| best_loss = extract_agg_loss(init_loss) | ||
| return EarlyStopping(best_loss, deepcopy(ps), deepcopy(st), 0, 0, patience, false) | ||
| return EarlyStopping(best_loss, deepcopy(cfg.cdev(ps)), deepcopy(cfg.cdev(st)), 0, 0, cfg.patience, false) | ||
| end | ||
|
|
||
| function update!(es::EarlyStopping, snapshot::EpochSnapshot, ps, st, epoch, cfg::TrainConfig) | ||
| current_loss = extract_agg_loss(snapshot.l_val) | ||
|
|
||
| if isbetter(current_loss, es.best_loss, first(cfg.loss_types)) | ||
| es.best_loss = current_loss | ||
| es.best_ps = deepcopy(ps) | ||
| es.best_st = deepcopy(st) | ||
| es.best_ps = deepcopy(cfg.cdev(ps)) | ||
| es.best_st = deepcopy(cfg.cdev(st)) | ||
| es.best_epoch = epoch | ||
| es.counter = 0 | ||
| else | ||
|
|
@@ -62,16 +62,16 @@ function best_or_final(stopper::EarlyStopping, ps, st, cfg::TrainConfig) | |
| end | ||
| end | ||
|
|
||
| function build_results(model, history::TrainingHistory, stopper::EarlyStopping, ps, st, x_train, y_train, x_val, y_val) | ||
| function build_results(model, history::TrainingHistory, stopper::EarlyStopping, ps, st, x_train, forcings_train, y_train, x_val, forcings_val, y_val, cfg::TrainConfig) | ||
| target_names = model.targets | ||
|
|
||
| # final predictions in test mode | ||
| ŷ_train, _ = model(x_train, ps, LuxCore.testmode(st)) | ||
| ŷ_val, _ = model(x_val, ps, LuxCore.testmode(st)) | ||
| ŷ_train, _ = model((cfg.cdev(x_train), cfg.cdev(forcings_train)), cfg.cdev(ps), LuxCore.testmode(st)) | ||
| ŷ_val, _ = model((cfg.cdev(x_val), cfg.cdev(forcings_val)), cfg.cdev(ps), LuxCore.testmode(st)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can evaluate this still on the GPU side and just pipe the result of into the |
||
|
|
||
| # observed vs predicted DataFrames | ||
| train_obs_pred = hcat(toDataFrame(y_train), toDataFrame(ŷ_train, target_names)) | ||
| val_obs_pred = hcat(toDataFrame(y_val), toDataFrame(ŷ_val, target_names)) | ||
| train_obs_pred = hcat(DataFrame(y_train), toDataFrame(ŷ_train, target_names)) | ||
| val_obs_pred = hcat(DataFrame(y_val), toDataFrame(ŷ_val, target_names)) | ||
|
|
||
| # extra predictions without observational counterparts | ||
| train_diffs, val_diffs = extract_diffs(ŷ_train, ŷ_val, target_names) | ||
|
|
@@ -84,8 +84,8 @@ function build_results(model, history::TrainingHistory, stopper::EarlyStopping, | |
| val_obs_pred, | ||
| train_diffs, | ||
| val_diffs, | ||
| ps, | ||
| st, | ||
| cfg.cdev(ps), | ||
| cfg.cdev(st), | ||
| stopper.best_epoch, | ||
| stopper.best_loss, | ||
| ) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should do
dev/Arrayat the batch loader level. Up to this point data could still be lazy.