Skip to content
Open
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
15 changes: 14 additions & 1 deletion src/cdtools/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,20 @@ def closure():
return total_loss

# This takes the step for this minibatch
loss += optimizer.step(closure).detach().cpu().numpy()
# If an exception occurs at this point, the root cause may be related
# to the presence of large gradients that produce `nan` losses.
# We write a custom error message here to help communicate this point.
try:
loss += optimizer.step(closure).detach().cpu().numpy()
except Exception as e:
print('\n\n')
msg = 'An error has occurred during the parameter update and\n'\
'loss calculation step. This problem may be related to excessively\n'\
'large parameter gradients. Please try lowering the learning\n'\
'rates or disabling grad_required for one or more parameters\n'\

raise Exception(msg) from e



loss /= normalization
Expand Down