Pytorch学习笔记4
过拟合与欠拟合
交叉验证减少overfitting
把训练集划分为K份,每次取1/k 作为验证集
减轻overfitting
正则化方法:在损失函数中加入一范数,使得参数尽量小,减少模型复杂度,使得曲线更加平滑,使得高维特征参数比较小,使其在保持性能时退化成更低复杂的网络,避免overfitting
Regularization也被称为weight-decay
Pytorch使用:
optimizer=optim.SGD(net.parameters(),lr=learning_rate,weight_dacay=0.01)
SGDM的pytorch实现
optimizer=torch.optim.SGD(model.parameters(),args.lr,momentum=args.momentum,weight_decay
=args.weight_decay
optimizer=torch.optim.SGD(model.parameters(),args.lr,momentum=args.momentum,weight_decay
=args.weight_decay
scheduler=ReduceLROnPlateau(optimizer,'min')
for epoch in x range(args.start_epoch,args.epochs):
train(train_loader,model,criterion,optimizer,epoch)
result_avg,loss_val)=validate(val_loader,model,criterion,epoch)
scheduler.step(loss_val)
Early stopping 的pytorch实现
Dropout
net_dropped=torch.nn.Sequential(
torch.nn.LInear(784,200),
torch.nn.Dropout(0.5)
torch.nn.ReLU(),
)
test时,所有连接都会使用,所以validation时需要加入
net_dropped.eval()
Stochastic Gradient Descent
与之不同,SGD只取一个batch的梯度。也就是说SGD把以前在所有数据集上的梯度改成了在一个batch上的梯度。
原因:速度更快
如果用线性层处理图片,数据过多。
Receptive Field and Weight sharing
一个卷积核代表一个观察角度(边缘信息,模糊信息,脸部信息)
输入通道与卷积核的通道一一对应,比如输入3通道图像,就要用3通道卷积核进行处理,一个通道处理一个通道的图像,但是3通道卷积核也是一个卷积核,最后得到的数值是三个通道数值的累加。
也就是说,三通道图片用三通道单卷积核处理,得到单通道图像。
如果有多卷积核的话,有多少卷积核得到多少通道的图像
池化层
out=F.interpolate(x,scale_factor=2,mode=‘nearest’)
Batch nomalization
Feature scaling
BatchNorm1d
β,γ根据梯度信息自动更新