Skip to content

Fix SSIMLoss docstring examples: remove redundant 1- prefix#8828

Open
haoyu-haoyu wants to merge 1 commit intoProject-MONAI:devfrom
haoyu-haoyu:fix/ssim-loss-docstring
Open

Fix SSIMLoss docstring examples: remove redundant 1- prefix#8828
haoyu-haoyu wants to merge 1 commit intoProject-MONAI:devfrom
haoyu-haoyu:fix/ssim-loss-docstring

Conversation

@haoyu-haoyu
Copy link
Copy Markdown
Contributor

Fixes #8822.

Description

`SSIMLoss.forward()` already returns `1 - ssim_value` (see `monai/losses/ssim_loss.py` line 127):

```python
ssim_value = self.ssim_metric._compute_tensor(input, target).view(-1, 1)
loss: torch.Tensor = 1 - ssim_value
```

…yet the three docstring examples call `print(1-SSIMLoss(...)(x,y))`, which computes `1 - (1 - ssim) = ssim` — the structural similarity value, not the loss. A user copying the example would build a "loss" that increases as images grow more similar and train in the wrong direction.

Removing the `1-` prefix in the three `print(...)` calls makes the examples print the actual loss, consistent with the `Returns` docstring that already says "1 minus the ssim index (recall this is meant to be a loss function)".

Verified locally:

```

SSIMLoss(spatial_dims=2)(x, x) # identical inputs
tensor(0.) # correct loss behavior
```

Before the fix the docstring would have printed `1.0` (the similarity), after the fix it prints `0.0` (the loss).

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • In-line docstrings updated.

Checks

Fixes Project-MONAI#8822.

`SSIMLoss.forward()` already returns `1 - ssim_value`, so the docstring
examples `print(1-SSIMLoss(...)(x,y))` compute `1 - (1 - ssim) = ssim`
— the structural similarity, not the loss. A user following the
example would build a "loss" that *increases* as images grow more
similar and train in the wrong direction.

The three examples in the `forward` docstring are corrected to
`print(SSIMLoss(...)(x,y))`, which prints the actual loss value as
intended by the function's return-type docstring ("1 minus the ssim
index").

### Types of changes
- [x] Documentation update.

### Checks
- [x] I've read the [contribution guidelines](https://github.com/Project-MONAI/MONAI/blob/dev/CONTRIBUTING.md).

Signed-off-by: SexyERIC0723 <haoyuwang144@gmail.com>
Assisted-by: Claude Code
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 14, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 0c0a8169-3332-435e-bf08-272ee0cee673

📥 Commits

Reviewing files that changed from the base of the PR and between 089f02d and ee14040.

📒 Files selected for processing (1)
  • monai/losses/ssim_loss.py

📝 Walkthrough

Walkthrough

This PR corrects three docstring examples in SSIMLoss.forward(). Previously, examples showed print(1-SSIMLoss(...)(x,y)), but since the loss function already computes 1 - ssim_value internally, this would negate the result to the similarity value instead of the loss. The fix removes the redundant 1- prefix from examples for 2D, pseudo-3D, and 3D cases.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Title accurately describes the core fix: removing the redundant 1- prefix from SSIMLoss docstring examples.
Description check ✅ Passed Description covers the issue, root cause, fix, and verification. Follows template with non-breaking change and docstring update checkboxes marked.
Linked Issues check ✅ Passed Changes directly address issue #8822: all three docstring examples have the redundant 1- prefix removed, fixing the negation error that would invert the loss.
Out of Scope Changes check ✅ Passed Only docstring example lines modified; no changes to API, logic, or unrelated code. All changes are in-scope to the issue.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ericspod
Copy link
Copy Markdown
Member

Thanks for the update @haoyu-haoyu but this is being addressed in #8821 which will be merged soon.

@haoyu-haoyu
Copy link
Copy Markdown
Contributor Author

Hi @ericspod — thanks for the heads-up! Just to double-check, I think #8821 might be the wrong number — that PR is @Zeesejo's fix for the variable swap in JukeboxLoss.forward (issue #8820), and its diff only touches monai/losses/spectral_loss.py, not ssim_loss.py.

I scanned all PRs that touch ssim_loss.py and only #8828 (this PR) is open. Did you mean a different PR, or were you grouping this in with the broader monai/losses/ audit cluster (#8818, #8821, #8828) and planning to take all of them in together?

Happy to close this if there's an SSIMLoss fix landing elsewhere — just want to make sure we don't drop the docstring fix on the floor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] SSIMLoss docstring examples are incorrect: 1-SSIMLoss()(x,y) negates the loss

2 participants