Learn practical skills, build real-world projects, and advance your career
Updated 5 years ago
import torch
x = torch.tensor([[3.,1.,4.],[5.,6.,7.]])
w = torch.tensor([[4.,6.],[7.,8.],[8.,10.]], requires_grad=True)
b = torch.tensor([5.,6.,7.], requires_grad=True)
y = torch.mm(w,x)
y = y.add(b)
y
tensor([[ 47., 46., 65.],
[ 66., 61., 91.],
[ 79., 74., 109.]], grad_fn=<AddBackward0>)
y.mean().backward()
print('dy/dx:', x.grad)
print('dy/dw:', w.grad)
print('dy/db:', b.grad)
dy/dx: None
dy/dw: tensor([[0.8889, 2.0000],
[0.8889, 2.0000],
[0.8889, 2.0000]])
dy/db: tensor([0.3333, 0.3333, 0.3333])