Module neuralops.trainer.losses

Classes

class ContrastiveLoss (margin=1.0, **kwargs)
Expand source code
@LOSS_REGISTRY.register("contrastive")
class ContrastiveLoss(nn.Module):
    def __init__(self, margin=1.0, **kwargs) -> None:
        super().__init__()
        self.margin = margin

    def forward(self, embeddings, labels) -> Any:
        """Forward pass — compute module output from inputs."""
        dist = F.pairwise_distance(embeddings[0], embeddings[1])
        loss = (1 - labels) * 0.5 * dist.pow(2) + labels * 0.5 * torch.clamp(
            self.margin - dist, min=0
        ).pow(2)
        return loss.mean()

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes::

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:to, etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Ancestors

  • torch.nn.modules.module.Module

Methods

def forward(self, embeddings, labels) ‑> Any
Expand source code
def forward(self, embeddings, labels) -> Any:
    """Forward pass — compute module output from inputs."""
    dist = F.pairwise_distance(embeddings[0], embeddings[1])
    loss = (1 - labels) * 0.5 * dist.pow(2) + labels * 0.5 * torch.clamp(
        self.margin - dist, min=0
    ).pow(2)
    return loss.mean()

Forward pass — compute module output from inputs.

class CrossEntropyLoss (weight=None, ignore_index=-100, **kwargs)
Expand source code
@LOSS_REGISTRY.register("cross_entropy")
class CrossEntropyLoss(nn.Module):
    def __init__(self, weight=None, ignore_index=-100, **kwargs) -> None:
        super().__init__()
        self.loss = nn.CrossEntropyLoss(weight=weight, ignore_index=ignore_index)

    def forward(self, logits, labels) -> Any:
        """Forward pass — compute module output from inputs."""
        return self.loss(logits, labels)

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes::

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:to, etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Ancestors

  • torch.nn.modules.module.Module

Methods

def forward(self, logits, labels) ‑> Any
Expand source code
def forward(self, logits, labels) -> Any:
    """Forward pass — compute module output from inputs."""
    return self.loss(logits, labels)

Forward pass — compute module output from inputs.

class DiceLoss (smooth=1.0, **kwargs)
Expand source code
@LOSS_REGISTRY.register("dice")
class DiceLoss(nn.Module):
    def __init__(self, smooth=1.0, **kwargs) -> None:
        super().__init__()
        self.smooth = smooth

    def forward(self, logits, labels) -> Any:
        """Forward pass — compute module output from inputs."""
        probs = F.softmax(logits, dim=-1)
        one_hot = F.one_hot(labels, num_classes=probs.shape[-1]).float()
        intersection = (probs * one_hot).sum(dim=0)
        union = probs.sum(dim=0) + one_hot.sum(dim=0)
        dice = (2.0 * intersection + self.smooth) / (union + self.smooth)
        return 1 - dice.mean()

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes::

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:to, etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Ancestors

  • torch.nn.modules.module.Module

Methods

def forward(self, logits, labels) ‑> Any
Expand source code
def forward(self, logits, labels) -> Any:
    """Forward pass — compute module output from inputs."""
    probs = F.softmax(logits, dim=-1)
    one_hot = F.one_hot(labels, num_classes=probs.shape[-1]).float()
    intersection = (probs * one_hot).sum(dim=0)
    union = probs.sum(dim=0) + one_hot.sum(dim=0)
    dice = (2.0 * intersection + self.smooth) / (union + self.smooth)
    return 1 - dice.mean()

Forward pass — compute module output from inputs.

class FocalLoss (alpha=0.25, gamma=2.0, **kwargs)
Expand source code
@LOSS_REGISTRY.register("focal")
class FocalLoss(nn.Module):
    def __init__(self, alpha=0.25, gamma=2.0, **kwargs) -> None:
        super().__init__()
        self.alpha = alpha
        self.gamma = gamma

    def forward(self, logits, labels) -> Any:
        """Forward pass — compute module output from inputs."""
        ce_loss = F.cross_entropy(logits, labels, reduction="none")
        p_t = torch.exp(-ce_loss)
        return (self.alpha * (1 - p_t) ** self.gamma * ce_loss).mean()

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes::

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:to, etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Ancestors

  • torch.nn.modules.module.Module

Methods

def forward(self, logits, labels) ‑> Any
Expand source code
def forward(self, logits, labels) -> Any:
    """Forward pass — compute module output from inputs."""
    ce_loss = F.cross_entropy(logits, labels, reduction="none")
    p_t = torch.exp(-ce_loss)
    return (self.alpha * (1 - p_t) ** self.gamma * ce_loss).mean()

Forward pass — compute module output from inputs.

class LabelSmoothingLoss (smoothing=0.1, **kwargs)
Expand source code
@LOSS_REGISTRY.register("label_smoothing")
class LabelSmoothingLoss(nn.Module):
    def __init__(self, smoothing=0.1, **kwargs) -> None:
        super().__init__()
        self.smoothing = smoothing
        self.confidence = 1.0 - smoothing

    def forward(self, logits, labels) -> Any:
        """Forward pass — compute module output from inputs."""
        log_probs = F.log_softmax(logits, dim=-1)
        labels = labels.to(log_probs.device)
        nll = -log_probs.gather(dim=-1, index=labels.unsqueeze(1))
        smooth = -log_probs.mean(dim=-1)
        return (self.confidence * nll + self.smoothing * smooth).mean()

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes::

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:to, etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Ancestors

  • torch.nn.modules.module.Module

Methods

def forward(self, logits, labels) ‑> Any
Expand source code
def forward(self, logits, labels) -> Any:
    """Forward pass — compute module output from inputs."""
    log_probs = F.log_softmax(logits, dim=-1)
    labels = labels.to(log_probs.device)
    nll = -log_probs.gather(dim=-1, index=labels.unsqueeze(1))
    smooth = -log_probs.mean(dim=-1)
    return (self.confidence * nll + self.smoothing * smooth).mean()

Forward pass — compute module output from inputs.

class PolyLoss (epsilon=1.0, **kwargs)
Expand source code
@LOSS_REGISTRY.register("poly")
class PolyLoss(nn.Module):
    def __init__(self, epsilon=1.0, **kwargs) -> None:
        super().__init__()
        self.epsilon = epsilon

    def forward(self, logits, labels) -> Any:
        """Forward pass — compute module output from inputs."""
        ce = F.cross_entropy(logits, labels)
        pt = torch.exp(-ce)
        return ce + self.epsilon * (1 - pt)

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes::

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call :meth:to, etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Ancestors

  • torch.nn.modules.module.Module

Methods

def forward(self, logits, labels) ‑> Any
Expand source code
def forward(self, logits, labels) -> Any:
    """Forward pass — compute module output from inputs."""
    ce = F.cross_entropy(logits, labels)
    pt = torch.exp(-ce)
    return ce + self.epsilon * (1 - pt)

Forward pass — compute module output from inputs.