requires_grad(tensor, interface=None)[source]

Returns True if the tensor is considered trainable.

Warning

The implementation depends on the contained tensor type, and may be context dependent.

For example, Torch tensors and PennyLane tensors track trainability as a property of the tensor itself. TensorFlow, on the other hand, only tracks trainability if being watched by a gradient tape.

Parameters
• tensor (tensor_like) – input tensor

• interface (str) – The name of the interface. Will be determined automatically if not provided.

Example

Calling this function on a PennyLane NumPy array:

>>> x = np.array([1., 5.], requires_grad=True)
True
False


PyTorch has similar behaviour.

With TensorFlow, the output is dependent on whether the tensor is currently being watched by a gradient tape:

>>> x = tf.Variable([0.6, 0.1])
False
True


While TensorFlow constants are by default not trainable, they can be manually watched by the gradient tape:

>>> x = tf.constant([0.6, 0.1])