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

Remove gradient calculation during inference #258

Merged
merged 6 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed

- The CLI has been overhauled to use subcommands.
- Upgraded to Lightning >=2.0
- Upgraded to Lightning >=2.0.
- Checkpointing is configured to save the top-k models instead of all.
- Log steps rather than epochs as units of progress during training.
- Validation performance metrics are logged (and added to tensorboard) at the validation epoch, and training loss is logged at the end of training epoch, i.e. training and validation metrics are logged asynchronously.
- Irrelevant warning messages on the console output and in the log file are no longer shown.
- Nicely format logged warnings.
- `every_n_train_steps` has been renamed to `val_check_interval` in accordance to the corresponding Pytorch Lightning parameter.
- Training batches are randomly shuffled.
- Upgraded to Torch >=2.1.

### Removed

Expand All @@ -36,7 +37,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Correctly refer to input peak files by their full file path.
- Specifying custom residues to retrain Casanovo is now possible.
- Upgrade to depthcharge v0.2.3 to fix sinusoidal encoding and for the `PeptideTransformerDecoder` hotfix.
- Enable gradients during prediction and validation to avoid NaNs from occuring as a temporary workaround until a new Pytorch version is available.
- Correctly report amino acid precision and recall during validation.

## [3.3.0] - 2023-04-04
Expand Down
48 changes: 22 additions & 26 deletions casanovo/denovo/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,7 @@ def validation_step(
The loss of the validation step.
"""
# Record the loss.
# FIXME: Temporary workaround to avoid the NaN bug.
with torch.set_grad_enabled(True):
loss = self.training_step(batch, mode="valid")
loss = self.training_step(batch, mode="valid")
if not self.calculate_precision:
return loss

Expand Down Expand Up @@ -804,30 +802,28 @@ def predict_step(
and amino acid-level confidence scores.
"""
predictions = []
# FIXME: Temporary workaround to avoid the NaN bug.
with torch.set_grad_enabled(True):
for (
precursor_charge,
precursor_mz,
spectrum_i,
spectrum_preds,
) in zip(
batch[1][:, 1].cpu().detach().numpy(),
batch[1][:, 2].cpu().detach().numpy(),
batch[2],
self.forward(batch[0], batch[1]),
):
for peptide_score, aa_scores, peptide in spectrum_preds:
predictions.append(
(
spectrum_i,
precursor_charge,
precursor_mz,
peptide,
peptide_score,
aa_scores,
)
for (
precursor_charge,
precursor_mz,
spectrum_i,
spectrum_preds,
) in zip(
batch[1][:, 1].cpu().detach().numpy(),
batch[1][:, 2].cpu().detach().numpy(),
batch[2],
self.forward(batch[0], batch[1]),
):
for peptide_score, aa_scores, peptide in spectrum_preds:
predictions.append(
(
spectrum_i,
precursor_charge,
precursor_mz,
peptide,
peptide_score,
aa_scores,
)
)

return predictions

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies = [
"scikit-learn",
"spectrum_utils",
"tensorboard",
"torch>=2.0",
"torch>=2.1",
"tqdm",
]
dynamic = ["version"]
Expand Down
Loading