Losses

class todd.models.losses.BCELoss[source]

Bases: FunctionalLoss

__init__(*args, **kwargs)[source]
Return type:

None

class todd.models.losses.BCEWithLogitsLoss[source]

Bases: FunctionalLoss

__init__(*args, **kwargs)[source]
Return type:

None

class todd.models.losses.BaseLoss[source]

Bases: BuildPreHookMixin, Module, ABC

__init__(reduction=Reduction.MEAN, weight=1.0, bound=None, **kwargs)[source]
Parameters:
Return type:

None

classmethod build_pre_hook(config, registry, item)[source]
Parameters:
  • config (Config)

  • registry (RegistryMeta)

  • item (Item)

Return type:

Config

abstractmethod forward(*args, **kwargs)[source]
Return type:

Tensor

property reduction: Reduction
step()[source]
Return type:

None

property weight: float
class todd.models.losses.BaseScheduler[source]

Bases: Module

Base class for schedulers.

Under most cases, schedulers are used as a variable loss weight. Schedulers are functions of steps, which could mean iterations or epochs. Users could increment steps by calling step, or directly set the steps property. Call the scheduler to get the value for the current step.

Note

steps starts from 1, so step should be called after the first step.

The value of this scheduler is always the gain:

>>> base_scheduler = BaseScheduler(gain=5)
>>> base_scheduler()
5.0
>>> base_scheduler.step()
>>> base_scheduler()
5.0
__init__(*args, gain=1.0, **kwargs)[source]

Initialize.

Parameters:

gain (float) – multiplier to the scheduler value.

Return type:

None

forward()[source]

Compute the current schedule weight.

Returns:

The scheduler’s value for the current step, before multiplying gain.

Return type:

float

Since gain is handled by this base class, it is usually adequate for forward to return a percentage value in \([0, 1]\).

property gain: float
step()[source]
Return type:

None

property steps: int
class todd.models.losses.ChainedScheduler[source]

Bases: ComposedScheduler

Chained scheduler.

Schedulers are chained in an multiplicative manner:

>>> warmup = WarmupScheduler(end=5, gain=10)
>>> step = StepScheduler(milestones=[3, 4], gamma=0.1)
>>> chained = ChainedScheduler(schedulers=[warmup, step])
>>> for _ in range(5):
...     print(round(chained(), 6))
...     chained.step()
2.0
4.0
0.6
0.08
0.1
forward()[source]
Return type:

float

step()[source]
Return type:

None

class todd.models.losses.ComposedScheduler[source]

Bases: BuildPreHookMixin, BaseScheduler

__init__(*args, schedulers, **kwargs)[source]
Parameters:

schedulers (Iterable[BaseScheduler])

Return type:

None

classmethod build_pre_hook(config, registry, item)[source]
Parameters:
  • config (Config)

  • registry (RegistryMeta)

  • item (Item)

Return type:

Config

class todd.models.losses.CosineAnnealingScheduler[source]

Bases: BaseScheduler

Cosine annealing scheduler.

The value anneals as the cosine function is defined. The first step starts with 1. After duration steps, the value becomes 0. The best practice is to set duration to the total number of steps.

>>> cosine = CosineAnnealingScheduler(duration=5)
>>> for _ in range(6):
...     print(round(cosine(), 6))
...     cosine.step()
1.0
0.904508
0.654508
0.345492
0.095492
0.0
__init__(*args, duration, min_=0.0, **kwargs)[source]
Parameters:
Return type:

None

forward()[source]
Return type:

float

class todd.models.losses.CosineEmbeddingLoss[source]

Bases: FunctionalLoss

__init__(*args, **kwargs)[source]
Return type:

None

class todd.models.losses.CrossEntropyLoss[source]

Bases: FunctionalLoss

__init__(*args, **kwargs)[source]
Return type:

None

class todd.models.losses.DecayScheduler[source]

Bases: WarmupScheduler

Decay scheduler.

Before or at start, the value is 1. After or at end, the value is 0. In between, the value is interpolated.

>>> decay = DecayScheduler(start=2, end=7)
>>> for _ in range(8):
...     print(round(decay(), 1))
...     decay.step()
1.0
1.0
0.8
0.6
0.4
0.2
0.0
0.0
forward()[source]
Return type:

float

class todd.models.losses.DeferScheduler[source]

Bases: JumpMixin, WarmupScheduler

__init__(*args, to, **kwargs)[source]
Parameters:

to (int)

Return type:

None

class todd.models.losses.EarlyStopScheduler[source]

Bases: JumpMixin, DecayScheduler

Early stop.

At some point, the value drops to 0 from 1.

>>> early_stop = EarlyStopScheduler(at=3)
>>> for _ in range(5):
...     print(early_stop())
...     early_stop.step()
1.0
1.0
0.0
0.0
0.0
class todd.models.losses.FunctionalLoss[source]

Bases: BaseLoss

__init__(*args, func, **kwargs)[source]
Parameters:

func (Callable[[...], Tensor])

Return type:

None

classmethod build_pre_hook(config, registry, item)[source]
Parameters:
  • config (Config)

  • registry (RegistryMeta)

  • item (Item)

Return type:

Config

forward(pred, target, *args, mask=None, **kwargs)[source]
Parameters:
Return type:

Tensor

class todd.models.losses.L1Loss[source]

Bases: NormMixin, FunctionalLoss

__init__(*args, **kwargs)[source]
Return type:

None

class todd.models.losses.MSELoss[source]

Bases: NormMixin, FunctionalLoss

__init__(*args, **kwargs)[source]
Return type:

None

class todd.models.losses.NormMixin[source]

Bases: FunctionalLoss, ABC

__init__(*args, norm=False, **kwargs)[source]
Parameters:

norm (bool)

Return type:

None

forward(pred, target, *args, mask=None, **kwargs)[source]
Parameters:
Return type:

Tensor

class todd.models.losses.SchedulerRegistry[source]

Bases: LossRegistry

data = {'BaseScheduler': <class 'todd.models.losses.schedulers.BaseScheduler'>, 'ChainedScheduler': <class 'todd.models.losses.schedulers.ChainedScheduler'>, 'CosineAnnealingScheduler': <class 'todd.models.losses.schedulers.CosineAnnealingScheduler'>, 'DecayScheduler': <class 'todd.models.losses.schedulers.DecayScheduler'>, 'DeferScheduler': <class 'todd.models.losses.schedulers.DeferScheduler'>, 'EarlyStopScheduler': <class 'todd.models.losses.schedulers.EarlyStopScheduler'>, 'SequentialScheduler': <class 'todd.models.losses.schedulers.SequentialScheduler'>, 'StepScheduler': <class 'todd.models.losses.schedulers.StepScheduler'>, 'WarmupScheduler': <class 'todd.models.losses.schedulers.WarmupScheduler'>}
class todd.models.losses.SequentialScheduler[source]

Bases: ComposedScheduler

__init__(*args, milestones, **kwargs)[source]
Parameters:

milestones (Iterable[int])

Return type:

None

forward()[source]
Return type:

float

property scheduler: BaseScheduler
step()[source]
Return type:

None

class todd.models.losses.StepScheduler[source]

Bases: BaseScheduler

Step scheduler.

The value is multiplied by \(gamma\) at every milestone:

>>> step = StepScheduler(milestones=[3, 4], gamma=0.1)
>>> for _ in range(5):
...     print(round(step(), 2))
...     step.step()
1.0
1.0
0.1
0.01
0.01
__init__(*args, milestones, gamma, **kwargs)[source]
Parameters:
Return type:

None

forward()[source]
Return type:

float

class todd.models.losses.WarmupScheduler[source]

Bases: BaseScheduler

Warmup scheduler.

The value will linearly increase from 0 to 1. At step end, the value is 1.

>>> warmup = WarmupScheduler(end=5)
>>> for _ in range(7):
...     print(warmup())
...     warmup.step()
0.2
0.4
0.6
0.8
1.0
1.0
1.0
__init__(*args, start=0, end, **kwargs)[source]
Parameters:
Return type:

None

forward()[source]
Return type:

float