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.gradwill always returnNone).- If a new tensor is returned:
Defaults to
Falsefor float-type data.Defaults to
Truefor 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_likeReturn an empty tensor with shape and type of input.
ones_likeReturn an tensor of ones with shape and type of input.
zeros_likeReturn an tensor of zeros with shape and type of input.
full_likeReturn a new tensor with shape of input filled with value.
emptyReturn a new uninitialized tensor.
onesReturn a new tensor setting values to one.
zerosReturn a new tensor setting values to zero.
fullReturn 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)