API Reference

Enums

class polytorch.enums.BinaryLossType(value)

An enumeration.

class polytorch.enums.CategoricalLossType(value)

An enumeration.

class polytorch.enums.ContinuousLossType(value)

An enumeration.

Data

class polytorch.data.BinaryData(loss_type: polytorch.enums.BinaryLossType = BinaryLossType.CROSS_ENTROPY, labels: List[str] = NOTHING, colors: Optional[List[str]] = None, *, name: str = NOTHING)
class polytorch.data.CategoricalData(category_count: int, loss_type: polytorch.enums.CategoricalLossType = CategoricalLossType.CROSS_ENTROPY, labels: Optional[List[str]] = None, colors: Optional[List[str]] = None, *, name: str = NOTHING)
class polytorch.data.ContinuousData(loss_type: polytorch.enums.ContinuousLossType = ContinuousLossType.SMOOTH_L1_LOSS, color: str = '', *, name: str = NOTHING)
class polytorch.data.OrdinalData(category_count: int, loss_type: polytorch.enums.CategoricalLossType = CategoricalLossType.CROSS_ENTROPY, labels: Optional[List[str]] = None, colors: Optional[List[str]] = None, color: str = '', *, name: str = NOTHING)
class polytorch.data.PolyData(*, name: str = NOTHING)

Embedding

class polytorch.embedding.ContinuousEmbedding(embedding_size: int, bias: bool = True, device=None, dtype=None, **kwargs)
forward(input)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class polytorch.embedding.OrdinalEmbedding(category_count, embedding_size, bias: bool = True, device=None, dtype=None, **kwargs)
forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class polytorch.embedding.PolyEmbedding(input_types: List[polytorch.data.PolyData], embedding_size: int, feature_axis: int = - 1, **kwargs)
forward(*inputs)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Modules

exception polytorch.modules.PolyLayerError
class polytorch.modules.PolyLinear(output_types: List[polytorch.data.PolyData], out_features=None, **kwargs)

Creates a linear layer designed to be the final layer in a neural network model that produces unnormalized scores given to PolyLoss.

The out_features value is set internally from root.layer_size and cannot be given as an argument.

Loss

class polytorch.loss.PolyLoss(data_types: List[polytorch.data.PolyData], feature_axis: int = - 1, **kwargs)
forward(predictions, *targets)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Metrics

polytorch.metrics.calc_generalized_dice_score(predictions, target, power: float = 2.0, smooth: float = 1.0, feature_axis: int = - 1) torch.Tensor

A generalized Dice score for multi-class segmentation.

If power=0.0, this is equivalent to normal Dice score (i.e. volume “implicit” weighting) If power=1.0, this is equivalent to ‘equal’ weighting. If power=2.0, this is equivalent to ‘inverse volume’ weighting.

See:
polytorch.metrics.generalized_dice(predictions, *targets, data_index=None, feature_axis=- 1, perform_softmax: bool = True, power: float = 2.0) torch.Tensor

Calculate the generalized dice score for a single data index. Used for for multi-class segmentation.

See:
Parameters
  • predictions (_type_) – Logits or probabilities. If probabilities, perform_softmax must be False.

  • data_index (_type_, optional) – The index of the target to . Defaults to None.

  • feature_axis (int, optional) – _description_. Defaults to -1.

  • perform_softmax (bool, optional) – Whether or not to normalize the predictions using the softmax function. Defaults to True.

  • power (float, optional) – The power to use for the generalized dice score. Defaults to 2.0 (i.e. inverse volume weighting) If power=0.0, this is equivalent to normal Dice score (i.e. volume “implicit” weighting) If power=1.0, this is equivalent to ‘equal’ weighting. If power=2.0, this is equivalent to ‘inverse volume’ weighting.

Returns

The generalized dice score for the given data index.

Return type

torch.Tensor

Plots

polytorch.plots.format_fig(fig) plotly.graph_objs._figure.Figure

Formats a plotly figure in a nicer way.

polytorch.plots.plot_embedding(embedding: polytorch.embedding.PolyEmbedding, n_components: int = 2, show: bool = False, output_path: Optional[Union[str, pathlib.Path]] = None) plotly.graph_objs._figure.Figure

Plots the embedding in 2D or 3D.

Parameters
  • embedding (PolyEmbedding) – The embedding to plot.

  • n_components (int, optional) – The number of principal components to plot. Can be 2 or 3. Defaults to 2.

  • show (bool, optional) – Whether to show the plot. Defaults to False.

  • output_path (str|Path|None, optional) – The path to save the plot to. Can be in HTML, PNG, JPEG, SVG or PDF. Defaults to None.

Returns

The plotly figure

Return type

go.Figure

Util

polytorch.util.permute_feature_axis(tensor: torch.Tensor, old_axis: int, new_axis: int) torch.Tensor

Changes the shape of a tensor so that the feature axis is in a new axis.

Parameters
  • tensor (torch.Tensor) – The tensor to permute.

  • new_axis (int) – The desired index of the feature axis.

Returns

The predictions tensor with the feature axis at the specified index.

Return type

torch.Tensor

polytorch.util.split_tensor(tensor: torch.Tensor, data_types, feature_axis: int = - 1) Tuple[torch.Tensor, ...]

Splits a tensor into a tuple of tensors, one for each data type.

Parameters
  • tensor (Tensor) – The predictions tensor.

  • data_types (List[PolyData]) – The data types to predict.

  • feature_axis (int, optional) – The axis which has the features to predict. Defaults to last axis.

Returns

A tuple of tensors, one for each data type.

Return type

Tuple[Tensor, …]

polytorch.util.squeeze_prediction(prediction: torch.Tensor, target: torch.Tensor, feature_axis: int)

Squeeze feature axis if necessary

polytorch.util.total_size(data_types) int

Calculates the total number of features required to predict a list of output types.

Parameters

data_types (List[PolyData]) – The data types to predict.

Returns

The number of features required to predict the given data types.

Return type

int