首页 > 其他分享 >第三章 3.12 dropout 和 正则化 克服过拟合

第三章 3.12 dropout 和 正则化 克服过拟合

时间:2024-12-16 20:52:57浏览次数:7  
标签:loss plt val 3.12 train 拟合 import model dropout

代码:

# https://github.com/PacktPublishing/Modern-Computer-Vision-with-PyTorch
# https://github.com/PacktPublishing/Modern-Computer-Vision-with-PyTorch

###################  Chapter Three #######################################

# 第三章  读取数据集并显示
from torch.utils.data import Dataset, DataLoader
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
########################################################################
from torchvision import datasets
import torch
data_folder = '~/data/FMNIST' # This can be any directory you want to
# download FMNIST to
fmnist = datasets.FashionMNIST(data_folder, download=True, train=True)
tr_images = fmnist.data
tr_targets = fmnist.targets

val_fmnist = datasets.FashionMNIST(data_folder, download=True, train=False)
val_images = val_fmnist.data
val_targets = val_fmnist.targets


########################################################################
import matplotlib.pyplot as plt
#matplotlib inline
import numpy as np
from torch.utils.data import Dataset, DataLoader
import torch
import torch.nn as nn
device = 'cuda' if torch.cuda.is_available() else 'cpu'

########################################################################
class FMNISTDataset(Dataset):
    def __init__(self, x, y):
        x = x.float()
        x = x.view(-1,28*28)/255
        self.x, self.y = x, y
    def __getitem__(self, ix):
        x, y = self.x[ix], self.y[ix]
        return x.to(device), y.to(device)
    def __len__(self):
        return len(self.x)

from torch.optim import SGD, Adam

# Model with 2 hidden layers
def get_model():
    model = nn.Sequential(
        nn.Dropout(0.25),
        nn.Linear(28 * 28, 1000),
        nn.ReLU(),
        nn.Dropout(0.25),
        nn.Linear(1000, 1000),
        nn.ReLU(),
        nn.Dropout(0.25),
        nn.Linear(1000, 10)
    ).to(device)

    loss_fn = nn.CrossEntropyLoss()
    optimizer = Adam(model.parameters(), lr=1e-3)
    return model, loss_fn, optimizer

def train_batch(x, y, model, optimizer, loss_fn):
    model.train()
    prediction = model(x)
    l1_regularization=0
    for param in model.parameters():
        l1_regularization += torch.norm(param,1)
        pass
    batch_loss = loss_fn(prediction, y)+0.0001*l1_regularization
    batch_loss.backward()
    optimizer.step()
    optimizer.zero_grad()
    return batch_loss.item()

def accuracy(x, y, model):
    model.eval()
    # this is the same as @torch.no_grad
    # at the top of function, only difference
    # being, grad is not computed in the with scope
    with torch.no_grad():
        prediction = model(x)
    max_values, argmaxes = prediction.max(-1)
    is_correct = argmaxes == y
    return is_correct.cpu().numpy().tolist()

########################################################################
def get_data():
    train = FMNISTDataset(tr_images, tr_targets)
    trn_dl = DataLoader(train, batch_size=32, shuffle=True)#批大小
    val = FMNISTDataset(val_images, val_targets)
    val_dl = DataLoader(val, batch_size=len(val_images), shuffle=False)
    return trn_dl, val_dl
########################################################################
#@torch.no_grad()
def val_loss(x, y, model):
    with torch.no_grad():
        prediction = model(x)
    val_loss = loss_fn(prediction, y)
    return val_loss.item()

########################################################################
trn_dl, val_dl = get_data()
model, loss_fn, optimizer = get_model()

########################################################################
train_losses, train_accuracies = [], []
val_losses, val_accuracies = [], []
for epoch in range(30):# 轮次 30
    print(epoch)
    train_epoch_losses, train_epoch_accuracies = [], []
    for ix, batch in enumerate(iter(trn_dl)):
        x, y = batch
        batch_loss = train_batch(x, y, model, optimizer, loss_fn)
        train_epoch_losses.append(batch_loss)
    train_epoch_loss = np.array(train_epoch_losses).mean()

    for ix, batch in enumerate(iter(trn_dl)):
        x, y = batch
        is_correct = accuracy(x, y, model)
        train_epoch_accuracies.extend(is_correct)
    train_epoch_accuracy = np.mean(train_epoch_accuracies)
    for ix, batch in enumerate(iter(val_dl)):
        x, y = batch
        val_is_correct = accuracy(x, y, model)
        validation_loss = val_loss(x, y, model)
    val_epoch_accuracy = np.mean(val_is_correct)
    train_losses.append(train_epoch_loss)
    train_accuracies.append(train_epoch_accuracy)
    val_losses.append(validation_loss)
    val_accuracies.append(val_epoch_accuracy)

########################################################################
epochs = np.arange(30)+1#轮数 30
import matplotlib.ticker as mtick
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
#%matplotlib inline
plt.figure(figsize=(20,5))
plt.subplot(211)
plt.plot(epochs, train_losses, 'bo', label='Training loss')
plt.plot(epochs, val_losses, 'r', label='Validation loss')
plt.gca().xaxis.set_major_locator(mticker.MultipleLocator(1))
plt.title('Training and validation loss with no hidden layer')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.grid('off')
#plt.show()
plt.subplot(212)
plt.plot(epochs, train_accuracies, 'bo', label='Training accuracy')
plt.plot(epochs, val_accuracies, 'r', label='Validation accuracy')
plt.gca().xaxis.set_major_locator(mticker.MultipleLocator(1))
plt.title('Training and validation accuracy with no hidden layer')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.gca().set_yticklabels(['{:.0f}%'.format(x*100) for x in plt.gca().get_yticks()])
plt.legend()
plt.grid('off')
plt.show()

 

标签:loss,plt,val,3.12,train,拟合,import,model,dropout
From: https://www.cnblogs.com/excellentHellen/p/18611104

相关文章

  • 如何避免过拟合?如何避免梯度消失和梯度爆炸?
    如何避免过拟合?避免过拟合(overfitting)是机器学习中非常重要的一部分,因为过拟合会导致模型在训练数据上表现很好,但在测试数据或新数据上表现差。为了减少过拟合,可以采取以下几种策略:1.增加训练数据   方法:通过增加更多的数据样本,尤其是多样化的数据,可以帮助模型更好地学......
  • Halcon 中各种拟合算子原理及应用场景
    一、引言在机器视觉和图像处理领域,拟合操作是一种非常重要的技术手段。Halcon作为一款强大的机器视觉软件库,提供了多种拟合算子来处理不同类型的数据,如点、线、曲线等,从而帮助用户从复杂的数据中提取出有价值的几何信息。这些拟合算子基于严谨的数学原理,并且在不同的工业检测......
  • 【人工智能基础06】人工神经网络基础(二):1. 权值初始化(权值优化、避免梯度)、2. 权值衰减
    文章目录一.权值初始化1.零初始化(ZeroInitialization)2.随机初始化(RandomInitialization)3.Xavier初始化4.Kaiming初始化(KaimingInitialization)二.权值衰减:通过正则防止过拟合1.作用机制2.目的3.应用场景三.权值共享与卷积:处理过拟合的问题1.定义2.作用2.......
  • HALCON机器视觉之测量拟合(C# + HALCON,含完整代码)
    HALCON机器视觉之测量拟合(C#+HALCON)一、引言在当今的工业和民用领域,机器视觉技术发挥着越来越重要的作用。其中,测量拟合是机器视觉中的关键环节,它能够从图像中提取物体的各种信息,并将其转化为数学模型,为后续的分析、控制和决策提供依据。HALCON作为一款领先的机器视觉软......
  • 对于分类算法来说,过拟合到底是什么?
    过拟合问题:训练误差(trainingerror)&泛化误差(generalizationerror)通常,对于分类算法可能产生两种类型的误差:训练误差&泛化误差。训练误差:就是你的模型,对于现有的训练样本集,他们之间的拟合程度;泛化误差:就是你模型的泛化能力。就是对于新的样本数据的分类能力是......
  • 11.29深度学习_过拟合欠拟合批量、标准化
    八、过拟合与欠拟合​在训练深层神经网络时,由于模型参数较多,在数据量不足时很容易过拟合。而正则化技术主要就是用于防止过拟合,提升模型的泛化能力(对新数据表现良好)和鲁棒性(对异常数据表现良好)。1.概念认知这里我们简单的回顾下过拟合和欠拟合的基本概念~1.1过拟合......
  • 大语言模型---Dropout 的定义;Dropout 减少过拟合的原因;Dropout 的实现
    文章目录1.Dropout的定义2.Dropout减少过拟合的原因3.Dropout的实现1.Dropout的定义Dropout是一种简单而有效的正则化技术,通过在每次训练迭代中随机丢弃(即屏蔽)一部分神经元,强制模型在没有某些特定神经元参与的情况下学习,从而降低对特定神经元的依赖性。机......
  • 激光雷达定位与建图-拟合问题
    本篇文章介绍如何在点云中提取线段和平面。一、平面拟合1.问题提出给定一组由N个点组成的点云X={x......
  • python安装部署(3.12)
    环境:OS:Centos71.安装依赖包[root@localhost~]#yuminstallopenssl-develbzip2-develexpat-develgdbm-develreadline-develsqlite-develgcccentos7停止服务后,可以使用阿里云yum,参考如下:https://www.cnblogs.com/hxlasky/p/18367611 2.升级openssl[root@localh......
  • 机器学习/数据分析--用通俗语言讲解时间序列自回归(AR)模型,并用其预测天气,拟合度98%+
    时间序列在回归预测的领域的重要性,不言而喻,在数学建模中使用及其频繁,但是你真的了解ARIMA、AR、MA么?ACF图你会看么??时间序列数据如何构造???,我打过不少数学建模,但是都没有弄得很清楚;这篇将详细讲解了基础模型—AR的原理.文章目录1、自回归(AR)详解1、简要说明2、原理讲解......