mygrad.tensor#

mygrad.tensor(arr_like: ArrayLike, dtype: DTypeLikeReals | None = None, *, constant: bool | None = None, copy: bool = True, ndmin: int = 0) Tensor[source]#

Create a tensor

This documentation was adapted from that of ``numpy.array`

Parameters:
arr_likearray_like

A tensor, any object exposing the array interface, an object whose __array__ method returns an tensor, a real number, any (nested) sequence.

dtypedata-type, optional

The desired data-type for the tensor. Restricted to integer and float type. If not specified, then the type will be determined as the minimum type required to hold the objects in the sequence.

constantOptional[bool]

If True, this tensor is treated as a constant, and thus does not facilitate back propagation (i.e. constant_tensor.grad will always return None).

If a new tensor is returned:
  • Defaults to False for float-type data.

  • Defaults to True for integer-type data.

copybool, optional

If true (default), or if a copy is needed to satisfy any of the other requirements (dtype, constant, etc.) then a new tensor is created from copied data. Otherwise the tensor will be returned unchanged.

ndminint, optional

Specifies the minimum number of dimensions that the resulting tensor should have. Ones will be prepended to the shape as needed to meet this requirement.

Returns:
outTensor

A tensor satisfying the specified requirements.

See also

empty_like

Return an empty tensor with shape and type of input.

ones_like

Return an tensor of ones with shape and type of input.

zeros_like

Return an tensor of zeros with shape and type of input.

full_like

Return a new tensor with shape of input filled with value.

empty

Return a new uninitialized tensor.

ones

Return a new tensor setting values to one.

zeros

Return a new tensor setting values to zero.

full

Return a new tensor of given shape filled with value.

Examples

>>> import mygrad as mg
>>> mg.tensor([1, 2, 3])
Tensor([1, 2, 3])

Upcasting:

>>> mg.tensor([1, 2, 3.0])
Tensor([ 1.,  2.,  3.])

More than one dimension:

>>> mg.tensor([[1, 2], [3, 4]])
Tensor([[1, 2],
        [3, 4]])

Minimum dimensions 2:

>>> mg.tensor([1, 2, 3], ndmin=2)
Tensor([[1, 2, 3]])

Type provided:

>>> mg.tensor([1, 2, 3], dtype="float32")
Tensor([1., 2., 3.], dtype=float32)