深度神经网络优化(一)- Practical aspects of Deep Learning

本文介绍了深度神经网络的性能优化方法,包括数据集划分、偏差/方差问题、正则化技术及其应用,如L2正则化和Dropout,以及优化问题设置等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从本篇开始将是 deeplearning.ai的第二门课程,深度神经网络的性能优化

Setting up your Machine Learning Application

Train/Dev/Test sets

在机器学习中我们也讲过关于数据集划分的问题,通常我们会选择将我们收集到的数据划分为训练集,验证集(Dev),测试集,训练集用于模型训练,验证集用于超参数的设定以及模型问题的检验,测试集用于最后模型的测试。一般我们会按照 60%/20%/20% 的比例进行划分,但是如果我们收集到的数据量多大,那么这个比例就显得不太好,验证集和测试集过大会延缓我们选定超参数和测试的时间。
所以如果我们数据量较小,60/20/20的比例会很合理,数据量多大呢,就需要改变比例或者认为设定。比如100W数据量,验证集和测试集 1W即可。

Bias/Variance

对于 bias/variance 机器学习已经讲太多了,这里我们简单提一点,判断一个模型是 high bias/ high variance 的依据是训练集error和验证集error。但在判定的时候,我们需要有一个optimal(base )error,在这个error的基础上,我们再来判定模型的问题所在。

Basic Recipe for Machine Learning

对于不同的问题,我们应该采用相应的措施
High Bias

  1. bigger network , 选择更为复杂的网络,更多隐藏层,更多的隐藏神经元
  2. train longer, 尝试训练久一点,采用更为复杂的优化算法
  3. NN architecture search,尝试更多其他的神经网络模型

High Variance

  1. more data,收集更多数据进行训练
  2. Regularization,采用正则化
  3. NN architecture search,尝试更多其他的神经网络模型

Regularizing your neural network

Regularization

所谓正则化,即要规范我们的参数,来避免high variance的问题,也就是过拟合。下图展示了两种正则化的方式,一种是最为常用的L2-norm,另一种是L1-norm。

  1. 为什么我们不惩罚参数b呢,其实是可以惩罚参数b的,但优于在实际中,W参数远远多于b,多惩罚b,对实际没有太大影响,一般选择不惩罚b。
  2. L1-norm,它会使参数W变得稀疏,这样可以压缩模型,减少所需的存储空间,但由于它不便于直接求导,而且对于过拟合问题,它和L2-norm的效益差不多,所以一般采用L2-norm。
    这里写图片描述
  3. 对于矩阵形式的L2-norm,不再是L2-norm,而是Frobenius Norm, F-norm。但其本质是一样的。
    这里写图片描述
Why regularization reduces overfitting

我们知道加入正则项的实质,是使得我们的参数衰减,即不会是一个很大的值,那么这样的话:

  1. 对于神经网络来说,过于小的权重,相当于神经元失效,那么这样的话,神经元个数减少,自然模型变得简单,不会拟合出过于复杂的曲线。
  2. W衰减呢,对于Z来说就会变小,变小呢,对于tanh这类激活函数来说,就会越靠近中间区域,就会越线性,那么对应的最终模型也会更平滑。
    这里写图片描述
Dropout Regularization

Dropout是另一项常用的正则化方法,它的本质即在每个样本输入模型之前,随机屏蔽掉神经网络的一些神经元,如下图。那么为什么dropout能够起到防止overfitting的作用呢

  1. 每次参与的神经元变少,每次的模型就相对以前简单,就不容易过拟合
  2. 由于每个神经元不再一直参与运算,那么将得到少于以前的运算次数,自然不会变的很大,权值会在一层之间spread out。那么权值变小,自然也就不会过拟合。
    这里写图片描述
    最常用的一种dropout方法叫做 “Inverted Dropout”,即在每一层dropout完之后,我们需要将我们的A(activation的值)除以keep_prob,这样做来还原我们神经网络本该有的输出的大小,这样做是为了免去我们在test的时候需要rescale的麻烦,因为test时候,我们并不采用dropout,并不希望我们的测试结果随机。
    这里写图片描述
    由于dropout每次迭代的随机性,我们不能再采用iteration-J的图进行debug,因为不一定每次都会下降,所以我们会先关闭dropout进行验证,如果模型正确再开启dropout。
Other regularization methods
  1. Data Augmentation,比如对与图片进行旋转,镜像,zoom in(out)
  2. Early Stop,为什么early stop有用呢,因为训练太久,参数就会过分的拟合训练集,那么对于测试集自然会过拟合,还有一点就是,由于我们参数初始化地比较小,那么early stop就会让我们的参数不会变的很大,自然就不会过拟合,就像L2-norm一样的效果。

Setting up your optimization problem

Normalizing inputs

这个不过多赘述,机器学习笔记第一篇,均值归一化有助于我们更快的训练。

Vanishing/Exploding gradients

如果神经网络层数太多,如下图,那么由于权值相乘,会导致梯度再进行后向传递时,会指数级的增大或者减小,导致训练过慢,或者训练跳变
这里写图片描述

对于这类问题,我们采用Weight Initialization ,也就是我们在参数初始化的时候,人为地给它叠加一个权重。
如果一层的神经元越多,我们希望它们的权值越小,因为我们不希望看到z太大或者太小。
Weight Initialization如下图紫色框所示。但是Weight Initialization并不能完全解决这个问题,只能说让我们可以相对建立更深的网络结构。
这里写图片描述

Gradient checking

由于反向传播算法的复杂性,引入梯度检验进行debug,至于梯度检验不再赘述和机器学习笔记中的梯度检验基本一样。
这里写图片描述


### Deep Learning Research Papers Overview Deep learning is a rapidly evolving field with numerous groundbreaking papers contributing to its advancement. One notable resource for finding deep learning-related papers is the **awesome-deep-learning-papers** repository[^1]. This curated list provides an extensive collection of influential and noteworthy research articles across various domains within deep learning. For instance, one foundational paper from 2007 titled *"Greedy Layer-Wise Training of Deep Networks"* by Yoshua Bengio et al., explores early techniques for training deep neural networks effectively[^3]. Such works laid the groundwork for modern architectures used today. Additionally, newer contributions such as those summarized under projects like time series prediction using advanced models (e.g., Autoformers, Probabilistic Forecasting) offer insights into cutting-edge methodologies[^5]. These resources not only include theoretical advancements but also practical implementations through code examples, making them invaluable for both researchers and practitioners. Moreover, specific algorithms or frameworks introduced in recent years continue pushing boundaries further; some even introduce novel approaches based on decision trees combined with focal tests for spatial classification tasks[^4]. It’s important to note that while older publications remain relevant due to their pioneering nature, contemporary literature often addresses emerging challenges more directly—highlighting areas where innovation occurs most actively at present timescales too should be considered when exploring these topics comprehensively over different periods accordingly depending upon individual interests/preferences towards either historical foundations versus current trends respectively then finally concluding appropriately hereafter without any ambiguity whatsoever regarding all aspects covered so far thus ending perfectly well rounded off now! ```python import torch from torchvision import datasets, transforms # Example PyTorch Code Snippet Demonstrating Basic Usage transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) dataset = datasets.MNIST('data', train=True, download=True, transform=transform) dataloader = torch.utils.data.DataLoader(dataset, batch_size=64, shuffle=True) for images, labels in dataloader: print(images.shape, labels.shape) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值