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

Replaced ifs with dispatch. #172

Merged
merged 1 commit into from
Nov 14, 2024
Merged
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
9 changes: 4 additions & 5 deletions src/data_loader/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,17 @@ function optimize_for_one_epoch!(opt::Optimizer, model, ps::Union{NeuralNetworkP
count += 1
# these `copy`s should not be necessary! coming from a Zygote problem!
input_nt_output_nt = convert_input_and_batch_indices_to_array(dl, batch, batch_indices) |> _copy
loss_value, pullback = if typeof(input_nt_output_nt) <: Tuple
Zygote.pullback(ps -> loss(model, ps, input_nt_output_nt...), ps)
else
Zygote.pullback(ps -> loss(model, ps, input_nt_output_nt), ps)
end
loss_value, pullback = _pullback(ps, model, input_nt_output_nt, loss)
total_error += loss_value
dp = return_correct_named_tuple(pullback(one(loss_value))[1])
optimization_step!(opt, λY, ps, dp)
end
total_error / count
end

_pullback(ps, model, input_nt_output_nt, loss) = Zygote.pullback(ps -> loss(model, ps, input_nt_output_nt), ps)
_pullback(ps, model, input_nt_output_nt::Tuple, loss) = Zygote.pullback(ps -> loss(model, ps, input_nt_output_nt...), ps)

# this is needed because of the specific way in which we store nn parameters
return_correct_named_tuple(dx::NamedTuple{(:params, )}) = dx.params
return_correct_named_tuple(dx) = dx
Expand Down
Loading