介绍
Pytorch是一款facebook发布的深度学生框架,由其易用性,友好性,深受广大用户青睐
安装
张量Tensor
创建张量
张量的属性
tensor常见数据类型
常见方法
CUDA Tensor
常用数学运算
自动求导
前向计算
梯度计算
案例:Pytorch完成线性回归
import torch
import numpy as np
import matplotlib.pyplot as plt
x = torch.rand([50])
y = 3*x+0.8
w = torch.rand(1,requires_grad=True)
b = torch.rand(1,requires_grad=True)
def loss_fn(y,y_predict):
loss = (y_predict - y).pow(2).mean()
for i in [w,b]:
if i.grad is not None:
i.grad.data.zero_()
loss.backward()
return loss.data
def optimize(lr):
w.data -= lr * w.grad.data
b.data -= lr * b.grad.data
for i in range(3000):
y_predict = x*w+b
loss = loss_fn(y,y_predict)
if i%200 == 0:
print(i,loss)
optimize(0.01)
predict = x*w+b
plt.scatter(x.data.numpy(),y.data.numpy(),c='r')
plt.plot(x.data.numpy(),predict.data.numpy())
plt.show()
Pytorch基础模型组件
nn.Module
nn.Sequential
优化器
损失函数
案例:线性回归
import torch
import torch.optim as optim
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
x = torch.rand([50,1])
y = 3*x+0.8
class Lr(nn.Module):
def __init__(self):
super(Lr,self).__init__()
self.linear = nn.Linear(1,1)
def forward(self,x):
out = self.linear(x)
return out
model = Lr()
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(),lr=1e-3)
for i in range(30000):
out = model(x)
loss = criterion(y,out)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1)%1000 == 0:
print("Epch{}/{},Loss{:.6f}".format(i,5000,loss.data))
model.eval()
predict = model(x)
output = predict.data.numpy()
plt.scatter(x.data.numpy(),y.data.numpy(),c='r')
plt.plot(x.data.numpy(),output)
plt.show()
GPU运行代码
案例修改
import torch
import torch.optim as optim
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
x = torch.rand([50,1])
y = 3*x+0.8
class Lr(nn.Module):
def __init__(self):
super(Lr,self).__init__()
self.linear = nn.Linear(1,1)
def forward(self,x):
out = self.linear(x)
return out
x = x.to(device)
y = y.to(device)
model = Lr().to(device)
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(),lr=1e-3)
for i in range(30000):
out = model(x)
loss = criterion(y,out)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1)%1000 == 0:
print("Epch{}/{},Loss{:.6f}".format(i,5000,loss.data))
model.eval()
predict = model(x)
output = predict.cpu().detach().numpy()
plt.scatter(x.cpu().data.numpy(),y.cpu().data.numpy(),c='r')
plt.plot(x.cpu().data.numpy(),output)
plt.show()
常见优化算法
避免过拟合/加速训练