mygrad.ones#

mygrad.ones(shape: ~typing.Sequence[int] | int, dtype: ~mygrad.typing._dtype_like.DTypeLikeReals = <class 'numpy.float32'>, *, constant: bool | None = None) Tensor[source]#

Return a Tensor of the given shape and type, filled with ones.

This docstring was adapted from numpy.ones [1]

Parameters:
shapeUnion[int, Tuple[int]]

The shape of the output Tensor.

dtypedata-type, optional (default=numpy.float32)

The data type of the output Tensor.

constantOptional[bool]

If True, this tensor is a constant, and thus does not facilitate back propagation.

Defaults to False for float-type data. Defaults to True for integer-type data.

Integer-type tensors must be constant.

Returns:
Tensor

A Tensor of ones with the given shape and data type.

See also

ones_like

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

empty

Return a new uninitialized tensor.

zeros

Return a new tensor setting values to zero.

full

Return a new tensor of given shape filled with value.

References

Examples

>>> import mygrad as mg
>>> mg.ones(5)
Tensor([ 1.,  1.,  1.,  1.,  1.])
>>> mg.ones((5,), dtype=int)
Tensor([1, 1, 1, 1, 1])
>>> mg.ones((2, 1))
Tensor([[ 1.],
       [ 1.]])
>>> mg.ones((2, 2))
Tensor([[ 1.,  1.],
        [ 1.,  1.]])