MCPcopy Index your code
hub / github.com/tinygrad/tinygrad / gradient

Method gradient

tinygrad/tensor.py:827–850  ·  view source on GitHub ↗

Computes the gradient of the targets with respect to self. ```python exec="true" source="above" session="tensor" result="python" x = Tensor.eye(3) y = Tensor([[2.0,0,-2.0]]) z = y.matmul(x).sum() dx, dy = z.gradient(x, y) print(dx.tolist()) # dz/dx print(dy.tolist

(self, *targets:Tensor, gradient:Tensor|None=None)

Source from the content-addressed store, hash-verified

825 # ***** toposort and backward pass *****
826
827 def gradient(self, *targets:Tensor, gradient:Tensor|None=None) -> list[Tensor]:
828 """
829 Computes the gradient of the targets with respect to self.
830
831 ```python exec="true" source="above" session="tensor" result="python"
832 x = Tensor.eye(3)
833 y = Tensor([[2.0,0,-2.0]])
834 z = y.matmul(x).sum()
835 dx, dy = z.gradient(x, y)
836
837 print(dx.tolist()) # dz/dx
838 print(dy.tolist()) # dz/dy
839 ```
840 """
841 assert gradient is not None or self.shape == tuple(), "when no gradient is provided, backward must be called on a scalar tensor"
842 if not (self.is_floating_point() and all(t.is_floating_point() for t in targets)): raise RuntimeError("only float Tensors have gradient")
843 if gradient is None: gradient = Tensor(1.0, dtype=self.dtype, device=self.device)
844 target_uops = [x.uop for x in targets]
845 grads = compute_gradient(self.uop, gradient.uop, set(target_uops))
846 ret:list[Tensor] = []
847 for x in target_uops:
848 if (y:=grads.get(x)) is None: y = x.const_like(0)
849 ret.append(Tensor(y))
850 return ret
851
852 def backward(self, gradient:Tensor|None=None) -> Tensor:
853 """

Callers 15

backwardMethod · 0.95
GradientFunction · 0.80
avg_pool_backwardFunction · 0.80
pad_backwardFunction · 0.80
test_uniform_gradientMethod · 0.80
test_tinygradMethod · 0.80
test_gradientMethod · 0.80
helper_test_opFunction · 0.80

Calls 6

compute_gradientFunction · 0.90
TensorClass · 0.85
is_floating_pointMethod · 0.80
appendMethod · 0.80
getMethod · 0.45
const_likeMethod · 0.45

Tested by 15

test_uniform_gradientMethod · 0.64
test_tinygradMethod · 0.64
test_gradientMethod · 0.64
helper_test_opFunction · 0.64
get_tiny_gradientMethod · 0.64
test_sigmoid_extremeMethod · 0.64
test_exampleMethod · 0.64
test_zero_if_not_usedMethod · 0.64