网络基本构建与训练方法,常用函数解析
torch.nn.functional模块
nn.Module模块
import torch
print(torch.__version__)`
会自动下载,784是mnist数据集每个样本的像素点个数
from pathlib import Path
import requests
DATA_PATH = Path("data")
PATH = DATA_PATH / "mnist"
PATH.mkdir(parents=True, exist_ok=True)
URL = "http://deeplearning.net/data/mnist/"
FILENAME = "mnist.pkl.gz"
if not (PATH / FILENAME).exists():
content = requests.get(URL + FILENAME).content
(PATH / FILENAME).open("wb").write(content)
import pickle
import gzip
with gzip.open((PATH / FILENAME).as_posix(), "rb") as f:
((x_train, y_train), (x_valid, y_valid), _) = pickle.load(f, encoding="latin-1")
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3Iw4CBNL-1670769748412)(C:\Users\Administrator\Desktop\4.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JEr8Ubf7-1670769748414)(C:\Users\Administrator\Desktop\5.png)]
注意数据需转换成tensor才能参与后续建模训练
import torch
#map是python内置函数,会根据提供的函数对指定的序列做映射。
x_train, y_train, x_valid, y_valid = map(
torch.tensor, (x_train, y_train, x_valid, y_valid)
)
n, c = x_train.shape
x_train, x_train.shape, y_train.min(), y_train.max()
print(x_train, y_train)
print(x_train.shape)
print(y_train.min(), y_train.max())
import torch.nn.functional as F
loss_func = F.cross_entropy
def model(xb):
return xb.mm(weights) + bias
bs = 64
xb = x_train[0:bs] # a mini-batch from x
yb = y_train[0:bs]
weights = torch.randn([784, 10], dtype = torch.float, requires_grad = True)
bs = 64
bias = torch.zeros(10, requires_grad=True)
print(loss_func(model(xb), yb))
torch.nn.functional 很多层和函数在这里都会见到
torch.nn.functional中有很多功能,后续会常用的。那什么时候使用nn.Module,什么时候使用nn.functional呢?一般情况下,如果模型有可学习的参数,最好用nn.Module,其他情况nn.functional相对更简单一些。
from torch import nn
class Mnist_NN(nn.Module):
def __init__(self): #构造函数
super().__init__()
self.hidden1 = nn.Linear(784, 128)
self.hidden2 = nn.Linear(128, 256)
self.out = nn.Linear(256, 10)
self.dropout = nn.Dropout(0.5)
def forward(self, x): #前向传播
x = F.relu(self.hidden1(x))
x = F.relu(self.hidden2(x))
x = self.out(x)
return x
net = Mnist_NN()
print(net)
for name, parameter in net.named_parameters():
print(name, parameter,parameter.size())
使用TensorDataset和DataLoader来简化
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
train_ds = TensorDataset(x_train, y_train)
train_dl = DataLoader(train_ds, batch_size=bs, shuffle=True)
valid_ds = TensorDataset(x_valid, y_valid)
valid_dl = DataLoader(valid_ds, batch_size=bs * 2)
def get_data(train_ds, valid_ds, bs):
return (
DataLoader(train_ds, batch_size=bs, shuffle=True), #shuffle:数据是否打乱 训练的时候需要打乱,验证一般不做shuffle
DataLoader(valid_ds, batch_size=bs * 2),
)
import numpy as np
#step迭代次数 opt:优化器
def fit(steps, model, loss_func, opt, train_dl, valid_dl):
for step in range(steps): #epoch
model.train() #更新每一层的权重和偏置
for xb, yb in train_dl: #batch
loss_batch(model, loss_func, xb, yb, opt)
model.eval() #验证不需要更新参数
with torch.no_grad():
losses, nums = zip(
*[loss_batch(model, loss_func, xb, yb) for xb, yb in valid_dl]
)
#zip:配对
val_loss = np.sum(np.multiply(losses, nums)) / np.sum(nums)
print('当前step:'+str(step), '验证集损失:'+str(val_loss))
#zip的用法
a = [1,2,3]
b = [4,5,6]
zipped = zip(a,b) #配对
print(list(zipped))
a2,b2 = zip(*zip(a,b)) #解包
print(a2)
print(b2)
from torch import optim
def get_model():
model = Mnist_NN()
return model, optim.Adam(model.parameters(), lr=0.001)
def loss_batch(model, loss_func, xb, yb, opt=None):
loss = loss_func(model(xb), yb)
if opt is not None:
loss.backward()
opt.step()
opt.zero_grad() # torch默认梯度累加,所以执行完梯度后需要清零
return loss.item(), len(xb)
train_dl, valid_dl = get_data(train_ds, valid_ds, bs)
model, opt = get_model()
fit(25, model, loss_func, opt, train_dl, valid_dl)
correct = 0
total = 0
for xb,yb in valid_dl:
outputs = model(xb)
_,predicted = torch.max(outputs.data, 1) # 1 沿着1这个维度 返回最大值和最大值的位置
total += yb.size(0)
correct += (predicted == yb).sum().item()
print('Accuracy of the network on the 10000 test images:%d %%' %(100* correct/ total))
返回最大值和最大值的位置
total += yb.size(0)
correct += (predicted == yb).sum().item()
print('Accuracy of the network on the 10000 test images:%d %%' %(100* correct/ total))