Skip to content

Commit 063081f

Browse files
docs: fix docstring parameter name, add missing Args and Raises secti… (#8849)
### Description Fix several docstring issues across loss functions in `monai/losses/`: - `barlow_twins.py`: fix parameter name typo (`lamb` → `lambd`) in Args section - `nacl_loss.py`: add missing `kernel_ops` arg, update `distance_type`/`alpha` with defaults, add `Raises` section - `multi_scale.py`: add missing `reduction` arg, add `Raises` section - `ds_loss.py`: add `forward` docstring with Args and `Raises` section - `perceptual.py`: add `Raises` section to class docstring No functional changes. ### Types of changes - [x] Non-breaking change (fix or new feature that would not break existing functionality). Signed-off-by: chhayankjain <chhayank44@gmail.com> Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent 8b78589 commit 063081f

5 files changed

Lines changed: 35 additions & 6 deletions

File tree

‎monai/losses/barlow_twins.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class BarlowTwinsLoss(_Loss):
3636
def __init__(self, lambd: float = 5e-3) -> None:
3737
"""
3838
Args:
39-
lamb: Can be any float to handle the informativeness and invariance trade-off. Ideally set to 5e-3.
39+
lambd: Can be any float to handle the informativeness and invariance trade-off. Ideally set to 5e-3.
4040
4141
Raises:
4242
ValueError: When an input of dimension length > 2 is passed

‎monai/losses/ds_loss.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ def get_loss(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
6969
return self.loss(input, target) # type: ignore[no-any-return]
7070

7171
def forward(self, input: None | torch.Tensor | list[torch.Tensor], target: torch.Tensor) -> torch.Tensor:
72+
"""
73+
Args:
74+
input: a single tensor or a list of tensors from deeply supervised network outputs.
75+
target: the target tensor.
76+
77+
Returns:
78+
torch.Tensor: the computed deep supervision loss.
79+
80+
Raises:
81+
ValueError: if ``input`` is None.
82+
"""
7283
if isinstance(input, (list, tuple)):
7384
weights = self.get_weights(levels=len(input))
7485
loss = torch.tensor(0, dtype=torch.float, device=target.device)

‎monai/losses/multi_scale.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@ def __init__(
5555
) -> None:
5656
"""
5757
Args:
58-
loss: loss function to be wrapped
58+
loss: loss function to be wrapped.
5959
scales: list of scalars or None, if None, do not apply any scaling.
60-
kernel: gaussian or cauchy.
60+
kernel: type of smoothing kernel, either ``"gaussian"`` or ``"cauchy"``. Defaults to ``"gaussian"``.
61+
reduction: specifies the reduction to apply to the output:
62+
``"none"`` | ``"mean"`` | ``"sum"``. Defaults to ``"mean"``.
63+
64+
Raises:
65+
ValueError: if ``kernel`` is not ``"gaussian"`` or ``"cauchy"``.
66+
ValueError: if ``reduction`` is not ``"mean"``, ``"sum"``, or ``"none"``.
6167
"""
6268
super().__init__(reduction=LossReduction(reduction).value)
6369
if kernel not in kernel_fn_dict:

‎monai/losses/nacl_loss.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ def __init__(
5252
classes: number of classes
5353
dim: dimension of data (supports 2d and 3d)
5454
kernel_size: size of the spatial kernel
55-
distance_type: l1/l2 distance between spatial kernel and predicted logits
56-
alpha: weightage between cross entropy and logit constraint
57-
sigma: sigma of gaussian
55+
kernel_ops: type of spatial kernel, either ``"mean"`` or ``"gaussian"``. Defaults to ``"mean"``.
56+
distance_type: l1/l2 distance between spatial kernel and predicted logits. Defaults to ``"l1"``.
57+
alpha: weightage between cross entropy and logit constraint. Defaults to 0.1.
58+
sigma: sigma of gaussian filter, used when ``kernel_ops="gaussian"``. Defaults to 1.0.
59+
60+
Raises:
61+
ValueError: if ``kernel_ops`` is not ``"mean"`` or ``"gaussian"``.
62+
ValueError: if ``dim`` is not 2 or 3.
63+
ValueError: if ``distance_type`` is not ``"l1"`` or ``"l2"``.
5864
"""
5965

6066
super().__init__()

‎monai/losses/perceptual.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ class PerceptualLoss(nn.Module):
7070
Defaults to `None`.
7171
channel_wise: if True, the loss is returned per channel. Otherwise the loss is averaged over the channels.
7272
Defaults to ``False``.
73+
74+
Raises:
75+
NotImplementedError: if ``spatial_dims`` is not 2 or 3.
76+
ValueError: if a MedicalNet network is used with ``spatial_dims=2`` or ``is_fake_3d=True``.
77+
ValueError: if ``channel_wise=True`` is used with a non-MedicalNet network.
78+
ValueError: if ``network_type`` is not one of the supported options.
7379
"""
7480

7581
def __init__(

0 commit comments

Comments
 (0)