【深度学习】神经网络中 Batch 和 Epoch 之间的区别是什么?我们该如何理解?

一、问题的引入

1.1 随机梯度下降

随机梯度下降(Stochastic Gradient Descent,SGD)是一种优化算法,用于在机器学习和深度学习中更新模型参数,以最小化损失函数。与传统的梯度下降算法不同,SGD在每次参数更新时只使用一个样本(或一小批样本),而不是使用整个训练数据集。这使得SGD具有更快的收敛速度,并且可以处理大规模的数据集。

SGD的基本思想是通过沿着损失函数的梯度方向对模型参数进行更新,以使损失函数逐渐减小。每次迭代中,选择一个随机的样本或小批量样本,并计算该样本对损失函数的梯度。然后,使用这个梯度来更新模型参数。

SGD的更新规则如下:

参数 = 参数 - 学习率 * 损失函数关于参数的梯度

其中,学习率是一个超参数,控制了每次更新的步长。较小的学习率可以使训练更稳定,但可能会导致收敛速度较慢,而较大的学习率可能导致训练不稳定。

尽管SGD在训练过程中可以快速收敛,并且适用于大规模数据集,但由于每次更新仅使用一个样本或小批量样本,其更新方向可能会存在较大的随机性。因此,SGD可能会在更新中出现一些噪声,可能导致损失函数在训练过程中出现波动。

1.2 主要参数

随机梯度下降(Stochastic Gradient Descent,SGD)是一个优化算法,它有几个参数可以调整以影响算法的性能和收敛速度。以下是SGD算法中的主要参数:

学习率(Learning Rate):
学习率是控制每次参数更新步长的超参数。较小的学习率可以使训练更稳定,但可能会导致收敛速度过慢。较大的学习率可能导致训练不稳定甚至发散。调整学习率是优化算法中的一个重要任务,通常需要尝试不同的值来找到最佳学习率。

迭代次数(Epochs):
迭代次数是指训练算法在整个数据集上运行的次数。增加迭代次数可以使模型更好地适应训练数据,但过多的迭代次数可能导致过拟合。在实际应用中,通常需要通过交叉验证等方法来确定合适的迭代次数。

批次大小(Batch Size):
批次大小是每次更新时使用的样本数量。较大的批次大小可以加快训练速度,但可能会增加内存需求。较小的批次大小可能使训练更稳定,但收敛速度可能较慢。批次大小的选择也受到硬件资源和数据集大小的影响。

二、Batch

Batch大小是一个超参数,用于定义在更新内部模型参数之前要处理的样本数。可以将批处理视为循环迭代一个或多个样本,并对它们进行预测。

在批处理结束时,将这些预测与预期输出进行比较,并计算出误差。根据这个误差,更新算法用于改进模型,通常是沿着误差梯度的方向进行调整。

训练数据集可以被分成一个或多个批次。如果在每个批次中使用所有的训练样本来更新模型,那么这个学习算法被称为批量梯度下降。

如果批次大小为一个样本,那么学习算法被称为随机梯度下降。如果批次大小介于一个样本和整个训练数据集之间,那么学习算法被称为小批量梯度下降。

  • 批量梯度下降。批量大小 = 训练集的大小
  • 随机梯度下降。批量大小 = 1
  • 小批量梯度下降。1 < 批量大小 < 训练集的大小

批次大小是指每个批次中包含的训练样本数量。通常情况下,批次大小是一个正整数,例如32、64、128等。选择批次大小是一个重要的决策,它会直接影响训练的速度和稳定性。

在SGD中,每次参数更新都会使用一个批次的样本。与传统的梯度下降不同,SGD使用的是随机样本或者小批量样本来计算梯度并更新模型参数。这种做法具有以下几个优势:

  • 计算效率:与在整个数据集上计算梯度相比,每次只计算一个批次的梯度可以加快训练速度,特别是在大规模数据集上。
  • 参数更新的频率:使用小批量样本更新模型参数,可以在训练过程中进行更频繁的参数更新,从而使模型更快地收敛。
  • 随机性降低过拟合:使用随机的样本更新参数,可以在一定程度上减少训练过程中的过拟合,因为每次更新都是基于不同的子集样本。

三、Epoch

Epoch 是机器学习和深度学习训练过程中的一个重要概念。它表示在训练算法中完整地将整个训练数据集通过模型进行一次前向传播和反向传播的过程。训练数据集中的所有样本都被用于更新模型的参数一次,这称为一个迭代。

在训练过程中,我们通常会将训练数据集分成多个批次(batch),然后在每个批次上进行参数更新。每当整个训练数据集中的所有样本都通过模型并参与了参数更新,就完成了一个迭代。

Epoch 的概念是为了让模型在整个训练数据集上得到充分的学习,以便提高模型的性能和泛化能力。增加 Epoch 的数量可以使模型更好地适应训练数据,但过多的 Epoch 可能会导致过拟合,即模型在训练数据上表现很好,但在新数据上表现不佳。

在实际训练过程中,通常需要根据问题的特点和数据集的大小来选择合适的 Epoch 数量。有时候,使用交叉验证等技术来确定最佳的 Epoch 数量,以避免过拟合或欠拟合。

下面对随机梯度下降中的Epoch进行详细解读:

  1. 在随机梯度下降中,一个Epoch指的是通过模型前向传播和反向传播,在整个训练数据集的所有样本上进行一次更新模型参数的过程。这意味着在每个Epoch中,所有训练样本都会被用来计算梯度并更新模型。
  2. 尽管在每个Epoch中都会遍历整个训练数据集,但由于SGD每次更新只使用一个随机样本或小批量样本,每个Epoch中的参数更新具有一定的随机性。这种随机性可以帮助算法在训练过程中逃离局部极小值,但也可能导致训练过程中损失函数的波动。
  3. 在实际训练中,一个Epoch的定义会因批次大小(batch size)的不同而有所变化。假设训练数据集有N个样本,批次大小为B,则一个Epoch需要进行 N/B 轮参数更新。在每一轮中,模型会使用一个随机样本或小批量样本来计算梯度并更新参数。
  4. 选择适当的Epoch数量是一个重要的超参数选择。通常,如果Epoch数量过低,模型可能没有足够的机会在数据上进行学习;如果Epoch数量过高,可能导致过拟合。常见的做法是观察损失函数在训练集和验证集上的表现,并通过交叉验证等技术来选择最佳的Epoch数量。

四、两者之间的联系和区别

Batch(批次):

  • 批次是在每次参数更新时使用的一小部分训练样本。具体来说,一个批次包含的样本数量由批次大小(batch size)决定,可以是一个正整数,如32、64、128等。
  • 在每个批次中,模型使用这些样本进行前向传播、计算损失并进行反向传播,然后根据计算得到的梯度来更新模型参数。

Epoch(迭代):

  • 一个Epoch表示在整个训练数据集上进行一次完整的训练迭代。在一个Epoch中,模型会遍历整个训练数据集中的所有样本,使用它们来计算梯度并更新模型参数。
  • Epoch的数量决定了整个训练过程要进行多少次这样的完整迭代。

区别:

  • 批次和Epoch是两个不同的训练阶段。在每个Epoch中,会进行多个批次的参数更新。
  • 批次用于在每次更新时计算梯度,以便调整模型参数。它们是训练数据的子集。
  • Epoch用于描述整个训练数据集在模型中的一次完整传递。它代表了训练过程中的一轮完整迭代。
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
深度学习工具包 Deprecation notice. ----- This toolbox is outdated and no longer maintained. There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/) I would suggest you use one of the tools mentioned above rather than use this toolbox. Best, Rasmus. DeepLearnToolbox ================ A Matlab toolbox for Deep Learning. Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data. It is inspired by the human brain's apparent deep (layered, hierarchical) architecture. A good overview of the theory of Deep Learning theory is [Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf) For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng. * [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007) * [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010) * [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011) If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284) ``` @MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012", } ``` Contact: rasmusbergpalm at gmail dot com Directories included in the toolbox ----------------------------------- `NN/` - A library for Feedforward Backpropagation Neural Networks `CNN/` - A library for Convolutional Neural Networks `DBN/` - A library for Deep Belief Networks `SAE/` - A library for Stacked Auto-Encoders `CAE/` - A library for Convolutional Auto-Encoders `util/` - Utility functions used by the libraries `data/` - Data used by the examples `tests/` - unit tests to verify toolbox is working For references on each library check REFS.md Setup ----- 1. Download. 2. addpath(genpath('DeepLearnToolbox')); Example: Deep Belief Network --------------------- ```matlab function test_example_DBN load mnist_uint8; train_x = double(train_x) / 255; test_x = double(test_x) / 255; train_y = double(train_y); test_y = double(test_y); %% ex1 train a 100 hidden unit RBM and visualize its weights rand('state',0) dbn.sizes = [100]; opts.numepochs = 1; opts.batchsize = 100; opts.momentum = 0; opts.alpha = 1; dbn = dbnsetup(dbn, train_x, opts); dbn = dbntrain(dbn, train_x, opts); figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights %% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NN rand('state',0) %train dbn dbn.sizes = [100 100]; opts.numepochs = 1; opts.batchsize = 100; opts.momentum = 0; opts.alpha = 1; dbn = dbnsetup(dbn, train_x, opts); dbn = dbntrain(dbn, train_x, opts); %unfold dbn to nn nn = dbnunfoldtonn(dbn, 10); nn.activation_function = 'sigm'; %train nn opts.numepochs = 1; opts.batchsize = 100; nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.10, 'Too big error'); ``` Example: Stacked Auto-Encoders --------------------- ```matlab function test_example_SAE load mnist_uint8; train_x = double(train_x)/255; test_x = double(test_x)/255; train_y = double(train_y); test_y = double(test_y); %% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN % Setup and train a stacked denoising autoencoder (SDAE) rand('state',0) sae = saesetup([784 100]); sae.ae{1}.activation_function = 'sigm'; sae.ae{1}.learningRate = 1; sae.ae{1}.inputZeroMaskedFraction = 0.5; opts.numepochs = 1; opts.batchsize = 100; sae = saetrain(sae, train_x, opts); visualize(sae.ae{1}.W{1}(:,2:end)') % Use the SDAE to initialize a FFNN nn = nnsetup([784 100 10]); nn.activation_function = 'sigm'; nn.learningRate = 1; nn.W{1} = sae.ae{1}.W{1}; % Train the FFNN opts.numepochs = 1; opts.batchsize = 100; nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.16, 'Too big error'); ``` Example: Convolutional Neural Nets --------------------- ```matlab function test_example_CNN load mnist_uint8; train_x = double(reshape(train_x',28,28,60000))/255; test_x = double(reshape(test_x',28,28,10000))/255; train_y = double(train_y'); test_y = double(test_y'); %% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% error rand('state',0) cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer }; cnn = cnnsetup(cnn, train_x, train_y); opts.alpha = 1; opts.batchsize = 50; opts.numepochs = 1; cnn = cnntrain(cnn, train_x, train_y, opts); [er, bad] = cnntest(cnn, test_x, test_y); %plot mean squared error figure; plot(cnn.rL); assert(er<0.12, 'Too big error'); ``` Example: Neural Networks --------------------- ```matlab function test_example_NN load mnist_uint8; train_x = double(train_x) / 255; test_x = double(test_x) / 255; train_y = double(train_y); test_y = double(test_y); % normalize [train_x, mu, sigma] = zscore(train_x); test_x = normalize(test_x, mu, sigma); %% ex1 vanilla neural net rand('state',0) nn = nnsetup([784 100 10]); opts.numepochs = 1; % Number of full sweeps through data opts.batchsize = 100; % Take a mean gradient step over this many samples [nn, L] = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.08, 'Too big error'); %% ex2 neural net with L2 weight decay rand('state',0) nn = nnsetup([784 100 10]); nn.weightPenaltyL2 = 1e-4; % L2 weight decay opts.numepochs = 1; % Number of full sweeps through data opts.batchsize = 100; % Take a mean gradient step over this many samples nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); %% ex3 neural net with dropout rand('state',0) nn = nnsetup([784 100 10]); nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through data opts.batchsize = 100; % Take a mean gradient step over this many samples nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); %% ex4 neural net with sigmoid activation function rand('state',0) nn = nnsetup([784 100 10]); nn.activation_function = 'sigm'; % Sigmoid activation function nn.learningRate = 1; % Sigm require a lower learning rate opts.numepochs = 1; % Number of full sweeps through data opts.batchsize = 100; % Take a mean gradient step over this many samples nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); %% ex5 plotting functionality rand('state',0) nn = nnsetup([784 20 10]); opts.numepochs = 5; % Number of full sweeps through data nn.output = 'softmax'; % use softmax output opts.batchsize = 1000; % Take a mean gradient step over this many samples opts.plot = 1; % enable plotting nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); %% ex6 neural net with sigmoid activation and plotting of validation and training error % split training data into training and validation data vx = train_x(1:10000,:); tx = train_x(10001:end,:); vy = train_y(1:10000,:); ty = train_y(10001:end,:); rand('state',0) nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax output opts.numepochs = 5; % Number of full sweeps through data opts.batchsize = 1000; % Take a mean gradient step over this many samples opts.plot = 1; % enable plotting nn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally) [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); ``` [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旅途中的宽~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值