flint.tensor

class flint.Tensor(data: Union[float, list, numpy.ndarray], depends_on: list = [], requires_grad: bool = False)[source]

Bases: object

Tensor is the basic structure in the computation graph. It holds value for forward computation and grad for backward propagation.

Parameters
  • data (Union[float, list, np.ndarray]) – Data for the Tensor

  • depends_on (list, optional, default=[]) – List of dependent tensors (used when building autograd graph)

  • requires_grad (bool, optional, default=False) – Whether the Tensor requires gradient

add_depends_on(depends_on: list = []) None[source]

Add the dependent tensors for building autograd graph.

Parameters

depends_on (list) – List of dependent tensors

argmax(dim: Optional[int] = None) flint.tensor.Tensor[source]

Return the indice of the maximum value of all elements in the tensor.

backward()[source]

Autograd on computation graph.

dim() int[source]

Return the number of dimensions of self tensor.

property dtype
exp() flint.tensor.Tensor[source]
fill_(val: float) None[source]

Fill the tensor with the given scalar value val.

Parameters

val (float) – The value to fill the tensor with

log() flint.tensor.Tensor[source]
log_softmax(dim: int = - 1) flint.tensor.Tensor[source]
max(dim: Optional[int] = None, keepdims: bool = False) flint.tensor.Tensor[source]

Return the maximum value of all elements in the tensor.

property ndim: int

Return the number of dimensions of self tensor, alias for dim().

normal_(mean: float = 0.0, std: float = 1.0) None[source]

Fill the tensor with values drawn from the normal distribution.

Parameters
  • mean (float, optional, default=0.) – The mean of the normal distribution

  • std (float, optional, default=1.) – The standard deviation of the normal distribution

property numel: int

Return the total number of elements in the self tensor, i.e., the product of the tensor’s dimensions.

one_() None[source]

Fill the tensor with the scalar value 1.

one_grad() None[source]

Fill the gradient with ones.

classmethod ones(*shape, **kwargs)[source]

Create a tensor filled with the scalar value 1.

permute(*dims) flint.tensor.Tensor[source]

Return a view of the original tensor with its dimensions permuted.

Parameters

*dims – The desired ordering of dimensions

classmethod randn(*shape, **kwargs)[source]

Create a tensor filled with random scalar values.

property shape: Tuple[int]

Return the size of the self tensor, alias for size().

size(dim: Optional[int] = None) Union[int, Tuple[int]][source]

Return the size of the self tensor.

softmax(dim: int = - 1) flint.tensor.Tensor[source]
squeeze(dim: Optional[int] = None) flint.tensor.Tensor[source]

Remove the dimensions of input of size 1.

Parameters

dim (int, optional) – If given, the input will be squeezed only in this dimension. Or all the dimensions of size 1 will be removed.

sum(dim: Optional[int] = None, keepdims: bool = False) flint.tensor.Tensor[source]
transpose(dim0: int, dim1: int) flint.tensor.Tensor[source]

Swap the dimension dim0 and dim1 of the tensor.

Parameters
  • dim0 (int) – The first dimension to be transposed

  • dim1 (int) – The second dimension to be transposed

uniform_(low: float = 0.0, high: float = 1.0) None[source]

Fill the tensor with values drawn from the uniform distribution.

Parameters
  • low (float, optional, default=0.) – The lower bound of the uniform distribution

  • high (float, optional, default=1.) – The upper bound of the uniform distribution

unsqueeze(dim: int) flint.tensor.Tensor[source]

Insert a dimension of size one at the specified position.

Parameters

dim (int) – The index at which to insert the singleton dimension

view(*shape) flint.tensor.Tensor[source]

Return a new tensor with the same data as the self tensor but of a different shape.

Parameters

*shape – The desired size

zero_() None[source]

Fill the tensor with the scalar value 0.

zero_grad() None[source]

Fill the gradient with zeros.

classmethod zeros(*shape, **kwargs)[source]

Create a tensor filled with the scalar value 0.