This commit is contained in:
FilippoOlivo
2025-03-12 12:03:47 +01:00
committed by Nicola Demo
parent 59e6ee595c
commit ae796ce34c
6 changed files with 128 additions and 97 deletions

View File

@@ -14,13 +14,15 @@ class LabelTensor(torch.Tensor):
@staticmethod
def __new__(cls, x, labels, *args, **kwargs):
"""
Create a new instance of the :class:`LabelTensor` class.
Create a new instance of the :class:`pina.label_tensor.LabelTensor`
class.
:param torch.Tensor x: :class:`torch.tensor` instance to be casted as a
:class:`LabelTensor`.
:class:`pina.label_tensor.LabelTensor`.
:param labels: Labels to assign to the tensor.
:type labels: str | list[str] | dict
:return: The instance of the :class:`LabelTensor` class.
:return: The instance of the :class:`pina.label_tensor.LabelTensor`
class.
:rtype: LabelTensor
"""
@@ -31,9 +33,10 @@ class LabelTensor(torch.Tensor):
@property
def tensor(self):
"""
Give the tensor part of the :class:`LabelTensor` object.
Give the tensor part of the :class:`pina.label_tensor.LabelTensor`
object.
:return: tensor part of the :class:`LabelTensor`.
:return: tensor part of the :class:`pina.label_tensor.LabelTensor`.
:rtype: torch.Tensor
"""
@@ -41,9 +44,10 @@ class LabelTensor(torch.Tensor):
def __init__(self, x, labels):
"""
Construct a :class:`LabelTensor` by passing a dict of the labels and a
:class:`torch.Tensor`. Internally, the initialization method will store
check the compatibility of the labels with the tensor shape.
Construct a :class:`pina.label_tensor.LabelTensor` by passing a dict of
the labels and a :class:`torch.Tensor`. Internally, the initialization
method will store check the compatibility of the labels with the tensor
shape.
:Example:
>>> from pina import LabelTensor
@@ -271,9 +275,10 @@ class LabelTensor(torch.Tensor):
def __str__(self):
"""
The string representation of the :class:`LabelTensor`.
The string representation of the :class:`pina.label_tensor.LabelTensor`.
:return: String representation of the :class:`LabelTensor` instance.
:return: String representation of the
:class:`pina.label_tensor.LabelTensor` instance.
:rtype: str
"""
@@ -290,8 +295,8 @@ class LabelTensor(torch.Tensor):
Concatenate a list of tensors along a specified dimension. For more
details, see :meth:`torch.cat`.
:param list[LabelTensor] tensors: :class:`LabelTensor` instances to
concatenate
:param list[LabelTensor] tensors: :class:`pina.label_tensor.LabelTensor`
instances to concatenate
:param int dim: dimensions on which you want to perform the operation
(default is 0)
:return: A new :class:`LabelTensor' instance obtained by concatenating
@@ -346,8 +351,8 @@ class LabelTensor(torch.Tensor):
:param list[LabelTensor] tensors: A list of tensors to stack.
All tensors must have the same shape.
:return: A new :class:`LabelTensor` instance obtained by stacking the
input tensors, with the updated labels.
:return: A new :class:`pina.label_tensor.LabelTensor` instance obtained
by stacking the input tensors, with the updated labels.
:rtype: LabelTensor
"""
@@ -368,8 +373,8 @@ class LabelTensor(torch.Tensor):
:param bool mode: A boolean value indicating whether the tensor should
track gradients.If `True`, the tensor will track gradients;
if `False`, it will not.
:return: The :class:`LabelTensor` itself with the updated
`requires_grad` state and retained labels.
:return: The :class:`pina.label_tensor.LabelTensor` itself with the
updated `requires_grad` state and retained labels.
:rtype: LabelTensor
"""
@@ -394,8 +399,8 @@ class LabelTensor(torch.Tensor):
Performs Tensor dtype and/or device conversion. For more details, see
:meth:`torch.Tensor.to`.
:return: A new :class:`LabelTensor` instance with the updated dtype
and/or device and retained labels.
:return: A new :class:`pina.label_tensor.LabelTensor` instance with the
updated dtype and/or device and retained labels.
:rtype: LabelTensor
"""
@@ -405,11 +410,11 @@ class LabelTensor(torch.Tensor):
def clone(self, *args, **kwargs):
"""
Clone the :class:`LabelTensor`. For more details, see
Clone the :class:`pina.label_tensor.LabelTensor`. For more details, see
:meth:`torch.Tensor.clone`.
:return: A new :class:`LabelTensor` instance with the same data and
labels but allocated in a different memory location.
:return: A new :class:`pina.label_tensor.LabelTensor` instance with the
same data and labels but allocated in a different memory location.
:rtype: LabelTensor
"""
@@ -466,10 +471,11 @@ class LabelTensor(torch.Tensor):
"""
Stack tensors vertically. For more details, see :meth:`torch.vstack`.
:param list of LabelTensor label_tensors: The :class:`LabelTensor`
instances to stack. They need to have equal labels.
:return: A new :class:`LabelTensor` instance obtained by stacking the
input tensors vertically.
:param list of LabelTensor label_tensors: The
:class:`pina.label_tensor.LabelTensor` instances to stack. They need
to have equal labels.
:return: A new :class:`pina.label_tensor.LabelTensor` instance obtained
by stacking the input tensors vertically.
:rtype: LabelTensor
"""
@@ -524,15 +530,15 @@ class LabelTensor(torch.Tensor):
def __getitem__(self, index):
""" "
Override the __getitem__ method to handle the labels of the
:class:`LabelTensor` instance. It first performs __getitem__ operation
on the :class:`torch.Tensor` part of the instance, then updates the
labels based on the index.
:class:`pina.label_tensor.LabelTensor` instance. It first performs
__getitem__ operation on the :class:`torch.Tensor` part of the instance,
then updates the labels based on the index.
:param index: The index used to access the item
:type index: int | str | tuple of int | list ot int | torch.Tensor
:return: A new :class:`LabelTensor` instance obtained __getitem__
operation on :class:`torch.Tensor` part of the instance, with the
updated labels.
:return: A new :class:`pina.label_tensor.LabelTensor` instance obtained
`__getitem__` operation on :class:`torch.Tensor` part of the
instance, with the updated labels.
:rtype: LabelTensor
:raises KeyError: If an invalid label index is provided.
@@ -665,7 +671,8 @@ class LabelTensor(torch.Tensor):
@staticmethod
def summation(tensors):
"""
Computes the summation of a list of :class:`LabelTensor` instances.
Computes the summation of a list of
:class:`pina.label_tensor.LabelTensor` instances.
:param list[LabelTensor] tensors: A list of tensors to sum. All
@@ -712,8 +719,8 @@ class LabelTensor(torch.Tensor):
For more details, see :meth:`torch.Tensor.reshape`.
:param tuple of int shape: The new shape of the tensor.
:return: A new :class:`LabelTensor` instance with the updated shape and
labels.
:return: A new :class:`pina.label_tensor.LabelTensor` instance with the
updated shape and labels.
:rtype: LabelTensor
"""