Welcome to pytorch-types’s documentation!

Pytorch typing for pydantic.

class pytorch_types.Tensor[source]
classmethod validate(data) → torch.Tensor[source]
classmethod ndim(ndim) → pytorch_types.tensor.Tensor[source]

Alias for dim()

classmethod dims(dims) → pytorch_types.tensor.Tensor[source]
classmethod shape(*sizes) → pytorch_types.tensor.Tensor[source]
classmethod between(ge, le) → pytorch_types.tensor.Tensor[source]
classmethod ge(other)Tensor[source]

See torch.ge()

classmethod le(other)Tensor[source]

See torch.le()

classmethod gt(other)Tensor[source]

See torch.gt()

classmethod lt(other)Tensor[source]

See torch.lt()

classmethod ne(other)Tensor[source]

See torch.ne()

classmethod device(device) → pytorch_types.tensor.Tensor[source]

Is the torch.device where this Tensor is.

classmethod cpu(memory_format=torch.preserve_format)Tensor[source]

Returns a copy of this object in CPU memory.

If this object is already in CPU memory and on the correct device, then no copy is performed and the original object is returned.

Args:
memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

classmethod cuda(device=None, non_blocking=False, memory_format=torch.preserve_format)Tensor[source]

Returns a copy of this object in CUDA memory.

If this object is already in CUDA memory and on the correct device, then no copy is performed and the original object is returned.

Args:
device (torch.device): The destination GPU device.

Defaults to the current CUDA device.

non_blocking (bool): If True and the source is in pinned memory,

the copy will be asynchronous with respect to the host. Otherwise, the argument has no effect. Default: False.

memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

classmethod dtype(dtype) → pytorch_types.tensor.Tensor[source]
classmethod float(memory_format=torch.preserve_format)Tensor[source]

self.float() is equivalent to self.to(torch.float32). See to().

Args:
memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

classmethod float32() → pytorch_types.tensor.Tensor[source]
classmethod half(memory_format=torch.preserve_format)Tensor[source]

self.half() is equivalent to self.to(torch.float16). See to().

Args:
memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

classmethod float16()[source]
classmethod double(memory_format=torch.preserve_format)Tensor[source]

self.double() is equivalent to self.to(torch.float64). See to().

Args:
memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

classmethod float64() → pytorch_types.tensor.Tensor[source]
classmethod int(memory_format=torch.preserve_format)Tensor[source]

self.int() is equivalent to self.to(torch.int32). See to().

Args:
memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

classmethod int32() → pytorch_types.tensor.Tensor[source]
classmethod long(memory_format=torch.preserve_format)Tensor[source]

self.long() is equivalent to self.to(torch.int64). See to().

Args:
memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

classmethod int64() → pytorch_types.tensor.Tensor[source]
classmethod short(memory_format=torch.preserve_format)Tensor[source]

self.short() is equivalent to self.to(torch.int16). See to().

Args:
memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

classmethod int16() → pytorch_types.tensor.Tensor[source]
classmethod byte(memory_format=torch.preserve_format)Tensor[source]

self.byte() is equivalent to self.to(torch.uint8). See to().

Args:
memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

classmethod uint8() → pytorch_types.tensor.Tensor[source]
classmethod bool(memory_format=torch.preserve_format)Tensor[source]

self.bool() is equivalent to self.to(torch.bool). See to().

Args:
memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

class pytorch_types.Numpy[source]
classmethod validate(data) → numpy.ndarray[source]
classmethod ndim(ndim) → pytorch_types.numpy.Numpy[source]

Number of array dimensions.

>>> x = np.array([1, 2, 3])
>>> x.ndim
1
>>> y = np.zeros((2, 3, 4))
>>> y.ndim
3
classmethod dims(dims) → pytorch_types.numpy.Numpy[source]
classmethod shape(*sizes) → pytorch_types.numpy.Numpy[source]

Tuple of array dimensions.

The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it. As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the array and the remaining dimensions. Reshaping an array in-place will fail if a copy is required.

>>> x = np.array([1, 2, 3, 4])
>>> x.shape
(4,)
>>> y = np.zeros((2, 3, 4))
>>> y.shape
(2, 3, 4)
>>> y.shape = (3, 8)
>>> y
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
>>> y.shape = (3, 6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: total size of new array must be unchanged
>>> np.zeros((4,2))[::2].shape = (-1,)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Incompatible shape for in-place modification. Use
`.reshape()` to make a copy with the desired shape.

numpy.reshape : similar function ndarray.reshape : similar method

classmethod between(ge, le) → pytorch_types.numpy.Numpy[source]
classmethod ge(ge) → pytorch_types.numpy.Numpy[source]
classmethod le(le) → pytorch_types.numpy.Numpy[source]
classmethod gt(gt) → pytorch_types.numpy.Numpy[source]
classmethod lt(lt) → pytorch_types.numpy.Numpy[source]
classmethod ne(ne) → pytorch_types.numpy.Numpy[source]
classmethod dtype(dtype) → pytorch_types.numpy.Numpy[source]

Data-type of the array’s elements.

None

d : numpy dtype object

numpy.dtype

>>> x
array([[0, 1],
       [2, 3]])
>>> x.dtype
dtype('int32')
>>> type(x.dtype)
<type 'numpy.dtype'>
classmethod float() → pytorch_types.numpy.Numpy[source]
classmethod float32() → pytorch_types.numpy.Numpy[source]
classmethod half() → pytorch_types.numpy.Numpy[source]
classmethod float16()[source]
classmethod double() → pytorch_types.numpy.Numpy[source]
classmethod float64() → pytorch_types.numpy.Numpy[source]
classmethod int() → pytorch_types.numpy.Numpy[source]
classmethod int32() → pytorch_types.numpy.Numpy[source]
classmethod long() → pytorch_types.numpy.Numpy[source]
classmethod int64() → pytorch_types.numpy.Numpy[source]
classmethod short() → pytorch_types.numpy.Numpy[source]
classmethod int16() → pytorch_types.numpy.Numpy[source]
classmethod byte() → pytorch_types.numpy.Numpy[source]
classmethod uint8() → pytorch_types.numpy.Numpy[source]
classmethod bool() → pytorch_types.numpy.Numpy[source]