?
Pytorch没有官方的高阶API,一般需要用户自己实现训练循环、验证循环、和预测循环。
作者通过仿照keras的功能对Pytorch的nn.Module进行了封装,设计了torchkeras.KerasModel类,
实现了 fit, evaluate,predict等方法,训练,验证,预测方法。相当于用户自定义高阶API。
并示范了用它实现线性回归模型和DNN二分类模型。
torchkeras.KerasModel类看起来非常强大,但实际上它们的源码非常简单,不足200行。 我们在第一章中一、Pytorch的建模流程
用到的训练代码其实就是torchkeras库的核心源码。
import torch
import torchkeras
print("torch.__version__="+torch.__version__)
print("torchkeras.__version__="+torchkeras.__version__)
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import Dataset,DataLoader,TensorDataset
import torchkeras
import pytorch_lightning as pl
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
#正负样本数量
n_positive,n_negative = 2000,2000
#生成正样本, 小圆环分布
r_p = 5.0 + torch.normal(0.0,1.0,size = [n_positive,1])
theta_p = 2*np.pi*torch.rand([n_positive,1])
Xp = torch.cat([r_p*torch.cos(theta_p),r_p*torch.sin(theta_p)],axis = 1)
Yp = torch.ones_like(r_p)
#生成负样本, 大圆环分布
r_n = 8.0 + torch.normal(0.0,1.0,size = [n_negative,1])
theta_n = 2*np.pi*torch.rand([n_negative,1])
Xn = torch.cat([r_n*torch.cos(theta_n),r_n*torch.sin(theta_n)],axis = 1)
Yn = torch.zeros_like(r_n)
#汇总样本
X = torch.cat([Xp,Xn],axis = 0)
Y = torch.cat([Yp,Yn],axis = 0)
#可视化
plt.figure(figsize = (6,6))
plt.scatter(Xp[:,0],Xp[:,1],c = "r")
plt.scatter(Xn[:,0],Xn[:,1],c = "g")
plt.legend(["positive","negative"]);
ds = TensorDataset(X,Y)
ds_train,ds_val = torch.utils.data.random_split(ds,[int(len(ds)*0.7),len(ds)-int(len(ds)*0.7)])
dl_train = DataLoader(ds_train,batch_size = 100,shuffle=True,num_workers=2)
dl_val = DataLoader(ds_val,batch_size = 100,num_workers=2)
for features,labels in dl_train:
break
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(2,4)
self.fc2 = nn.Linear(4,8)
self.fc3 = nn.Linear(8,1)
def forward(self,x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
y = self.fc3(x)
return y
torchkeras.metrics :这个库里存放了很多评价指标
torchkeras.KerasModel:这个库时torchkeras的标准模型类库,初始化时传入的参数是实例化的模型net,损失函数,指标记录字典,优化器。
? ? ? ? ? ??torchkeras.KerasModel库实现了3个方法,fit(训练),evaluate(验证),predict(预测),KerasModel.fit()传入的参数是训练集迭代器teain_dl,验证集迭代器(val_dl),epoch,skpt_path(权重保存路径),patience(早停参数,多少次没有改进模型停止训练),monitor(翻译是监视器,设定val_loss或val_acc,执行fit时会直接打印训练monitor的曲线),mode(这个参数应该是对应monitor,选val_loss时设定最小min 模式,val_acc时设定为最大max模式)。
torchkeras.LightModel? :借鉴pytorch_Lighting,实现了多GPU训练,Tensorboard功能,回调函数等功能。
有些model输入输出,Loss结构和torchkeras结构有些不同,只需要copy出torchkeras.KerasModel或torchkeras.LightModel 源码,适当修改即可。
具体用法,见如下代码:
from torchkeras import KerasModel
from torchkeras.metrics import Accuracy
net = Net()
loss_fn = nn.BCEWithLogitsLoss()
metric_dict = {"acc":Accuracy()}
optimizer = torch.optim.Adam(net.parameters(), lr=0.001)
model = KerasModel(net,
loss_fn = loss_fn,
metrics_dict= metric_dict,
optimizer = optimizer
)
from torchkeras import summary
summary(net,input_data=features);
训练模型
dfhistory = model.fit(
train_data=dl_train,
val_data=dl_val,
epochs=100,
ckpt_path='checkpoint',
patience=10,
monitor='val_acc',
mode='max'
)
# 结果可视化
fig, (ax1,ax2) = plt.subplots(nrows=1,ncols=2,figsize = (12,5))
ax1.scatter(Xp[:,0],Xp[:,1], c="r")
ax1.scatter(Xn[:,0],Xn[:,1],c = "g")
ax1.legend(["positive","negative"]);
ax1.set_title("y_true");
Xp_pred = X[torch.squeeze(net.forward(X)>=0.5)]
Xn_pred = X[torch.squeeze(net.forward(X)<0.5)]
ax2.scatter(Xp_pred[:,0],Xp_pred[:,1],c = "r")
ax2.scatter(Xn_pred[:,0],Xn_pred[:,1],c = "g")
ax2.legend(["positive","negative"]);
ax2.set_title("y_pred");
评估模型