主要内容

使用自定义培训循环的火车网络

此示例显示了如何使用自定义学习率计划进行手写数字进行分类的网络。

If训练不提供所需的选项(例如,自定义学习率计划),那么您可以使用自动差异定义自己的自定义培训循环。

此示例训练网络以将手写数字分类为基于时间的衰减学习率时间表:对于每次迭代,求解器使用由此给出的学习率 ρ t = ρ 0 1 + k t , 在哪里t是迭代数, ρ 0 is the initial learning rate, andk是腐烂。

Load Training Data

使用The Digits数据作为图像数据存储成像功能并指定包含图像数据的文件夹。

datafolder = fullfile(toolboxDir(toolboxDir)('nnet'),'nndemos','nndatasets','DigitDataset');imds = imagedatastore(datafolder,...'IncludeSubfolders',真的,....“ Labelsource”,“折叠式”);

将数据划分为培训和验证集。搁置10%的数据供验证splitEachLabelfunction.

[imdsTrain,imdsValidation] = splitEachLabel(imds,0.9,“随机化”);

The network used in this example requires input images of size 28-by-28-by-1. To automatically resize the training images, use an augmented image datastore. Specify additional augmentation operations to perform on the training images: randomly translate the images up to 5 pixels in the horizontal and vertical axes. Data augmentation helps prevent the network from overfitting and memorizing the exact details of the training images.

inputsize = [28 28 1];PixelRange = [-5 5];ImageAugmenter = Imagedataaugmenter(...“ randxtranslation”,PixelRange,...'RandYTranslation',pixelrange);AugimdStrain = augmentedimagedatastore(inputsize(1:2),imdstrain,'DataAugmentation',imageAugmenter);

要自动调整验证图像大小而不执行进一步的数据扩展,请使用增强图像数据存储,而无需指定任何其他预处理操作。

augimdsValidation = augmentedImagedatastore(inputsize(1:2),imdsvalidation);

确定培训数据中的类数。

class =类别(IMDSTRAIN.LABELS);numClasses = numel(class);

定义网络

定义网络进行图像分类。

layers = [imageInputlayer(inputsize,'正常化','none','Name',“输入”)卷积2Dlayer(5,20,,'Name','conv1')batchnormalizationlayer('Name','bn1') reluLayer('Name','relu1')卷积2Dlayer(3,20,,'填充','相同的','Name','conv2')batchnormalizationlayer('Name','bn2') reluLayer('Name','relu2')卷积2Dlayer(3,20,,'填充','相同的','Name','conv3')batchnormalizationlayer('Name','bn3') reluLayer('Name','relu3')完整连接的layerer(numClasses,'Name','fc') softmaxLayer('Name','SoftMax')]; lgraph = layerGraph(layers);

创建一个dlnetwork对象来自图层图。

dlnet = dlnetwork(lgraph)
dlnet = dlnetwork with properties: Layers: [12×1 nnet.cnn.layer.Layer] Connections: [11×2 table] Learnables: [14×3 table] State: [6×3 table] InputNames: {'input'} OutputNames: {'softmax'}

定义模型梯度功能

创建功能模型磨合,在示例结尾处列出,dlnetworkobject, a mini-batch of input data with corresponding labels and returns the gradients of the loss with respect to the learnable parameters in the network and the corresponding loss.

Specify Training Options

Train for ten epochs with a mini-batch size of 128.

numepochs = 10;MiniBatchSize = 128;

Specify the options for SGDM optimization. Specify an initial learn rate of 0.01 with a decay of 0.01, and momentum 0.9.

initialLearnRate = 0.01; decay = 0.01; momentum = 0.9;

火车模型

创建一个Minibatchqueue对象在训练过程中处理和管理小型图像的对象。对于每个迷你批次:

  • Use the custom mini-batch preprocessing functionpreprocessMiniBatch(defined at the end of this example) to convert the labels to one-hot encoded variables.

  • 用维数标签格式化图像数据'SSCB'(空间,空间,通道,批次)。默认情况下,Minibatchqueue对象将数据转换为dlarrayobjects with underlying type单身的. Do not add a format to the class labels.

  • 如果有可用的话,请训练GPU。默认情况下,Minibatchqueueobject converts each output to agpuarray如果有GPU可用。使用GPU需要并行计算工具箱™和支持的GPU设备。万博1manbetx有关支持设备的信息,请参阅万博1manbetx释放的G万博1manbetxPU支持(并行计算工具箱).

mbq = minibatchqueue(augimdsTrain,...“ MINIBATCHSIZE”,minibatchsize,...'minibatchfcn',@preprocessMiniBatch,...'MINIBATCHFORMAT',{'SSCB',''});

初始化培训进度图。

图LineLosttrain = AnimatedLine('颜色',[0.85 0.325 0.098]); ylim([0 inf]) xlabel(“迭代”)ylabel("Loss") 网格

Initialize the velocity parameter for the SGDM solver.

velocity = [];

使用自定义培训循环训练网络。对于每个阶段,将数据和循环循环循环,以减少微型数据。对于每个迷你批次:

  • Evaluate the model gradients, state, and loss using thedlfeval模型磨合功能并更新网络状态。

  • 确定基于时间的衰减学习率计划的学习率。

  • Update the network parameters using thesgdmupdatefunction.

  • Display the training progress.

迭代= 0;start = tic;%循环在时期。forepoch = 1:numepochs% Shuffle data.洗牌(MBQ);%循环在迷你批次上。whilehasdata(MBQ)迭代=迭代 + 1;%读取迷你数据的数据。[dlX, dlY] = next(mbq);%使用DLFEVAL评估模型梯度,状态和损失%模型级功能并更新网络状态。[渐变,状态,损失] = dlfeval(@modelgradients,dlnet,dlx,dly);dlnet.state = state;%确定基于时间的衰减学习率计划的学习率。LEALTRATE = inationLearnrate/(1 +衰减*迭代);%使用SGDM优化器更新网络参数。[dlnet,速度] = sgdmupdate(dlnet,渐变,速度,学习率,动量);% Display the training progress.D = duration(0,0,toc(start),'格式','HH:MM:SS');附加点(LineLosstrain,迭代,损失)标题(“时代:”+ epoch +", Elapsed: "+ string(D)) drawnowendend

测试模型

Test the classification accuracy of the model by comparing the predictions on the validation set with the true labels.

训练后,对新数据进行预测不需要标签。创造Minibatchqueueobject containing only the predictors of the test data:

  • To ignore the labels for testing, set the number of outputs of the mini-batch queue to 1.

  • 指定与训练相同的迷你批量尺寸。

  • Preprocess the predictors using the预处理前培养子函数,在示例的末尾列出。

  • For the single output of the datastore, specify the mini-batch format'SSCB'(空间,空间,通道,批次)。

numOutputs = 1;mbqtest = minibatchqueue(augimdsvalidation,numOutputs,...“ MINIBATCHSIZE”,minibatchsize,...'minibatchfcn',@preprocessminibatch predictors,...'MINIBATCHFORMAT','SSCB');

Loop over the mini-batches and classify the images using模型预期函数,在示例的末尾列出。

预测= modelpredictions(dlnet,mbqtest,class);

评估分类精度。

ytest = imdsvalidation.labels;精度=平均值(预测== ytest)
精度= 0.9530

Model Gradients Function

The模型磨合功能为dlnetworkobjectdlnet,一个小批量的输入数据dlX带有相应的标签Y并返回相对于可学习参数的损失梯度dlnet, the network state, and the loss. To compute the gradients automatically, use thedlgradientfunction.

function[渐变,状态,损失] = modelgradigent(dlnet,dlx,y)[dlypred,state] = forthrow(dlnet,dlx);损失= crossentropy(dlypred,y);渐变= dlgradient(损失,dlnet.learnables);损失= double(聚集(extractdata(loss)));end

模型预测功能

The模型预期功能为dlnetworkobjectdlnet, aMinibatchqueue输入数据mbq, and the network classes, and computes the model predictions by iterating over all data in theMinibatchqueueobject. The function uses the上ehotdecodefunction to find the predicted class with the highest score.

function预测= modelpredictions(DLNET,MBQ,类)预测= [];whilehasdata(mbq)dlxtest = next(mbq);dlypred =预测(dlnet,dlxtest);ypred = oneHotDecode(dlypred,class,1)';预测= [预测;ypred];endend

迷你批次预处理功能

ThepreprocessMiniBatch函数使用以下步骤进行预处理预测变量和标签:

  1. Preprocess the images using the预处理前培养子function.

  2. 从传入的细胞阵列中提取标签数据,然后将沿第二维的分类阵列连接到一个分类阵列中。

  3. One-hot encode the categorical labels into numeric arrays. Encoding into the first dimension produces an encoded array that matches the shape of the network output.

function[X,Y] = preprocessMiniBatch(XCell,YCell)% Preprocess predictors.X =预处理示例(XCell);%从细胞和连接酸盐中提取标签数据。y = cat(2,ycell {1:end});% One-hot encode labels.y = onehotencode(y,1);end

Mini-Batch Predictors Preprocessing Function

The预处理前培养子函数通过从输入单元格数组中提取图像数据并连接到数字阵列来预处理预测变量。对于灰度输入,在第四维上串联为每个图像添加了第三维,以用作单例通道维度。

functionX =预处理示例(XCell)% Concatenate.x = cat(4,xcell {1:end});end

See Also

|||||||||

相关话题