import torch w = torch.tensor([[3.0,1.0]],requires_grad=True) b = torch.tensor([[3.0]],requires_grad=True) X = torch.randn(10,2) Y = torch.randn(10,1) Y_hat = X @ w.t() + b # Y_hat定义后其正向传播被立即执行,与其后面的loss创建语句无关
loss = torch.mean(torch.pow(Y_hat-Y,2)) print(loss.data) print(Y_hat.data)
import torch w = torch.tensor([[3.0,1.0]],requires_grad=True) b = torch.tensor([[3.0]],requires_grad=True) X = torch.tensor([[-1.0,-1.0],[1.0,1.0]]) Y = torch.tensor([[2.0,3.0]])
relu = MyReLU.apply Y_hat = relu(X@w.t() + b) loss = torch.mean(torch.pow(Y_hat-Y,2)) loss.backward() print(w.grad) print(b.grad) # Y_hat的梯度函数即MyReLU.backward print(Y_hat.grad_fn)
计算图与反向传播
import torch
x = torch.tensor(3.0,requires_grad=True) y1 = x + 1 y2 = 2*x loss = (y1-y2)**2