数据集的预处理模块
-
tensor格式转换
主要是讲numpy格式转换为tensor格式
x_train, y_train, x_valid, y_valid = map( torch.tensor, (x_train, y_train, x_valid, y_valid) )
-
torch.nn.finction模块与torch.nn.Module模块
torch.nn.functional中有很多功能,后续会常用的。那什么时候使用nn.Module,什么时候使用nn.functional呢?一般情况下,如果模型有可学习的参数,最好用nn.Module,其他情况nn.functional相对更简单一些
#例如一些损失函数,激活层等使用torch.Function
import torch.nn.Function as F
loss_fun = F.cross_entropy
from torch import nn
#在建立一些网络模块时直接使用torch.Module,且只需要写前向传播,在使用module模块时已经提前初始化好变量。
class Minist_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)
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)
--------------------------------------------------
Mnist_NN(
(hidden1): Linear(in_features=784, out_features=128, bias=True)
(hidden2): Linear(in_features=128, out_features=256, bias=True)
(out): Linear(in_features=256, out_features=10, bias=True)
)
TensorDataset和DataLoader模块简化数据集处理以及batchsize的设置。
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),
DataLoader(valid_ds, batch_size=bs * 2),
)
进行模型的组装
#相关辅助函数
from torch import optim
def get_model():
model = Mnist_NN()
#优化器函数中要传入所优化的参数
return model, optim.SGD(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()
return loss.item(), len(xb)
import numpy as np
def fit(steps, model, loss_func, opt, train_dl, valid_dl):
for step in range(steps):
#训练的时候加上model.train在训练过程中进行normalize相关操作
model.train()
for xb, yb in train_dl:
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]
)
val_loss = np.sum(np.multiply(losses, nums)) / np.sum(nums)
print('当前step:'+str(step), '验证集损失:'+str(val_loss))
结果输出
train_dl, valid_dl = get_data(train_ds, valid_ds, bs)
model, opt = get_model()
fit(20, model, loss_func, opt, train_dl, valid_dl)
-----------------------------------------------------------------
当前step:0 验证集损失:2.281271044921875
当前step:1 验证集损失:2.2509783180236815
当前step:2 验证集损失:2.203812783432007
当前step:3 验证集损失:2.1252328746795652
当前step:4 验证集损失:1.9954518688201903
当前step:5 验证集损失:1.7956134561538697
当前step:6 验证集损失:1.5333285322189332
当前step:7 验证集损失:1.261820195388794
当前step:8 验证集损失:1.0405126466751098
当前step:9 验证集损失:0.880806344127655
当前step:10 验证集损失:0.7669796929359436
当前step:11 验证集损失:0.6844435347557067
当前step:12 验证集损失:0.6225414663314819
当前step:13 验证集损失:0.5751254560470581
当前step:14 验证集损失:0.5374272463321685
当前step:15 验证集损失:0.5071435091495514
当前step:16 验证集损失:0.48235899238586427
当前step:17 验证集损失:0.46154185042381285
当前step:18 验证集损失:0.44456041851043704
当前step:19 验证集损失:0.42940848422050476