-
Notifications
You must be signed in to change notification settings - Fork 6
Montecarlo dropout uncertainty estimation #258
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?
Changes from all 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 |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| function evaluate_mc_dropout( | ||
| ghm, x, y, y_no_nan, ps, st, loss_types, training_loss, extra_loss, agg; | ||
| n_samples::Int = 100, file_path::Union{String, Nothing} = nothing, train_or_val_name::String = "val" | ||
| ) | ||
|
|
||
| if !has_dropout(ghm) | ||
| @info "MC Dropout skipped: no Dropout layers detected in the model.\nFalling back to standard deterministic evaluation." | ||
| loss_val, sts, ŷ = evaluate_acc(ghm, x, y, y_no_nan, ps, st, loss_types, training_loss, extra_loss, agg) | ||
| return _store_sample(file_path, train_or_val_name, ŷ, loss_val, nothing) | ||
| end | ||
|
|
||
| st_train = Lux.trainmode(st) | ||
|
|
||
| for k in 1:n_samples | ||
| loss_k, _, ŷ_k = compute_loss( | ||
| ghm, ps, st_train, | ||
| (x, (y, y_no_nan)), | ||
| logging = LoggingLoss( | ||
| train_mode = true, | ||
| loss_types = loss_types, | ||
| training_loss = training_loss, | ||
| extra_loss = extra_loss, | ||
| agg = agg | ||
| ) | ||
| ) | ||
| _store_sample(file_path, train_or_val_name, ŷ_k, loss_k, k) | ||
| end | ||
|
Comment on lines
+14
to
+27
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. When |
||
|
|
||
| return nothing | ||
| end | ||
|
|
||
|
|
||
| function _store_sample(file_path::String, name, ŷ, loss, sample) | ||
| return jldopen(file_path, "a+") do file | ||
|
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. |
||
| key = isnothing(sample) ? name : "$(name)/sample_$(sample)" | ||
| file["predictions/$key"] = ŷ | ||
| file["losses/$key"] = loss | ||
| end | ||
| end | ||
|
|
||
| function _store_sample(::Nothing, name, ŷ, loss, sample) | ||
| return (; ŷ, loss) | ||
| end | ||
|
|
||
| function _has_dropout(model) | ||
| return model isa Lux.Dropout || model isa Lux.AlphaDropout || model isa Lux.VariationalHiddenDropout | ||
| end | ||
|
|
||
| function _has_dropout(model::Lux.AbstractLuxContainerLayer) | ||
| return any(_has_dropout, children(model)) | ||
| end | ||
|
|
||
| function has_dropout(model) | ||
| return _has_dropout(model) | ||
| end | ||
|
|
||
| function mc_dropout_statistics(storage::NamedTuple) | ||
| predictions = [s.ŷ for s in storage] | ||
| losses = [s.loss for s in storage] | ||
|
|
||
| pred_stack = stack(predictions, dims = ndims(first(predictions)) + 1) | ||
| mean_pred = mean(pred_stack, dims = ndims(pred_stack)) | ||
| var_pred = var(pred_stack, dims = ndims(pred_stack)) | ||
| mean_loss = mean(losses) | ||
|
|
||
| return (; mean_pred, var_pred, mean_loss) | ||
| end | ||
|
|
||
| function mc_dropout_statistics(file_path::String, train_or_val_name::String) | ||
| return jldopen(file_path, "r") do file | ||
| keys = sort(keys(file["predictions/$train_or_val_name"]), by = k -> parse(Int, split(k, "_")[end])) | ||
| losses = [file["losses/$train_or_val_name/$(k)"] for k in keys] | ||
|
|
||
| # Welford online algorithm to avoid loading all predictions at once | ||
| first_pred = file["predictions/$train_or_val_name/$(keys[1])"] | ||
| mean_pred = copy(first_pred) | ||
| M2 = zero(first_pred) | ||
| mean_loss = first(losses) | ||
|
|
||
| for (k, (key, loss)) in enumerate(zip(keys[2:end], losses[2:end])) | ||
| ŷ_k = file["predictions/$train_or_val_name/$(key)"] | ||
| delta = ŷ_k .- mean_pred | ||
| mean_pred .+= delta ./ k | ||
| delta2 = ŷ_k .- mean_pred | ||
| M2 .+= delta .* delta2 | ||
| mean_loss += (loss - mean_loss) / k | ||
| end | ||
|
|
||
| var_pred = M2 ./ (length(keys) - 1) | ||
|
|
||
| return (; mean_pred, var_pred, mean_loss) | ||
| end | ||
| end | ||
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.
The
compute_lossfunction returns an emptyNamedTuplefor thestatsfield (the third return value) whenlogging.train_modeis set totrue. Since this loop explicitly setstrain_mode = trueto keep dropout active,ŷ_kwill be empty, and the model's predictions will not be captured. This prevents the MC Dropout from collecting the necessary samples for uncertainty estimation. You should consider calling the model directly to obtain predictions while in training mode, or adjustingcompute_lossto return predictions even whentrain_modeis true.