mygrad.Tensor.constant#

property Tensor.constant: bool#

If True, this tensor is a constant; it will not propagate any gradient.

Additionally, any tensor that is a descendant of constant tensors will also be a constant.

Integer-valued tesnors, Python scalars and NumPy arrays are treated as constant tensors when included in MyGrad computational graphs.

Returns
bool

Examples

Constant-tensors do not back-propagate gradients:

>>> import mygrad as mg
>>> x = mg.Tensor([1., 2.], constant=True)
>>> y = mg.Tensor([0., 3.], constant=False)
>>> f = x * y
>>> f.backward()
>>> x.grad is None  # x has no gradient
True
>>> y.grad
array([1., 2.])

A tensor that is derived solely from constant tensors is also a constant:

>>> import numpy as np
>>> x = mg.Tensor([1., 2.], constant=True)
>>> y = mg.Tensor([0., 3.], constant=True)
>>> z = (x + y) ** 2 - np.array([8., 7.])
>>> z.constant
True

Integer-valued tensors are treated as constants

>>> mg.Tensor([1, 2]).constant
True