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

Create a tensor

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


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.


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.


A tensor satisfying the specified requirements.

See also


Return an empty tensor with shape and type of input.


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


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


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


Return a new uninitialized tensor.


Return a new tensor setting values to one.


Return a new tensor setting values to zero.


Return a new tensor of given shape filled with value.


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


>>> 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)