import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms
# 创建tensor
x = torch.tensor(1., requires_grad=True)
w = torch.tensor(2., requires_grad=True)
b = torch.tensor(3., requires_grad=True)
y = w * x + b # y = 2 * x + 3 #w=2,b=3 # 创建graph
y.backward() # 计算梯度
# 查看每个tensor的梯度
print(x.grad) # x.grad = 2 对x求偏导,既w的值
print(w.grad) # w.grad = 1 对w求偏导,既x的值
print(b.grad) # b.grad = 1 对b求偏导,1
#################################################
x = torch.randn(10, 3)
y = torch.randn(10, 2)
linear = nn.Linear(3, 2) # 创建FC层
print ('w: ', linear.weight) # 显示的是2*3矩阵,在进行计算的时候会转置成3*2
print ('b: ', linear.bias) # 显示的是1*2矩阵,在进行计算的时候会转置成2*1
criterion = nn.MSELoss() # 均方差损失函数
optimizer = torch.optim.SGD(linear.parameters(), lr=0.01) # 迭代优化器
pred = linear(x) # 向前传播
loss = criterion(pred, y) # 计算损失值
print('loss: ', loss.item())
loss.backward() # 反向传播
print ('dL/dw: ', linear.weight.grad) # 权重的梯度
print ('dL/db: ', linear.bias.grad) # 偏置量的梯度
optimizer.step() # 更新权重和偏置量
参考:
https://github.com/yunjey/pytorch-tutorial/blob/master/tutorials/01-basics/pytorch_basics/main.py