Main Content

列车网络在云中使用自动相比l Support

此示例显示了如何使用MATLAB自动支持进行并行训练训练卷积神经网络。金宝app深度学习训练通常需要数小时或数天。通过并行计算,您可以在本地或云中的集群中使用多个图形处理单元(GPU)加快训练。如果您可以访问具有多个GPU的计算机,则可以在数据的本地副本上完成此示例。如果您想使用更多的资源,则可以将深度学习培训扩展到云中。要了解有关您的并行培训选择的更多信息,请参阅并行扩展深度学习,在GPU和云中扩展。此示例可以指导您使用MATLAB自动并行支持的步骤来训练云中的深度学习网络。金宝app

Requirements

Before you can run the example, you need to configure a cluster and upload data to the cloud. In MATLAB, you can create clusters in the cloud directly from the MATLAB Desktop. On thetab, in theParallel菜单,选择创建和管理集群。In the Cluster Profile Manager, click创建云集群。Alternatively, you can use MathWorks Cloud Center to create and access compute clusters. For more information, see云中心入门。之后,将数据上传到Amazon S3存储桶中,然后直接从MATLAB访问。此示例使用已经存储在Amazon S3中的CIFAR-10数据集的副本。有关说明,请参阅将深度学习数据上传到云

设置平行池

在集群中启动一个并行池,并将工人数量设置为群集中的GPU数量。如果您指定比GPU更多的工人,那么其余的工人是空闲的。此示例假设您使用的群集设置为默认群集配置文件。检查MATLAB上的默认群集配置文件tab, inParallel>选择默认集群

numberOfWorkers = 8; parpool(numberOfWorkers);
Starting parallel pool (parpool) using the 'MyClusterInTheCloud' profile ... connected to 8 workers.

Load Data Set from the Cloud

使用云加载训练和测试数据集imageDatastore。In this example, you use a copy of the CIFAR-10 data set stored in Amazon S3. To ensure that the workers have access to the datastore in the cloud, make sure that the environment variables for the AWS credentials are set correctly. See将深度学习数据上传到云

imdsTrain = imageDatastore('s3:// cifar10cloud/cifar10/train',,,,...'IncludeSubfolders',真的,...'LabelSource',,,,“折叠式”); imdsTest = imageDatastore('s3://cifar10cloud/cifar10/test',,,,...'IncludeSubfolders',真的,...'LabelSource',,,,“折叠式”);

通过创建一个增强图像数据来训练网络增生模构达塔斯塔尔目的。使用随机翻译和水平反射。数据增强有助于防止网络过度拟合并记住培训图像的确切细节。

图像= [32 32 3];PixelRange = [-4 4];ImageAugmenter = Imagedataaugmenter(...'RandXReflection',真的,...'RandXTranslation',PixelRange,...'RandYTranslation',,,,pixelRange); augmentedImdsTrain = augmentedImageDatastore(imageSize,imdsTrain,...“数据调整”,成像器,...“ outputsizemode”,,,,'randcrop');

Define Network Architecture and Training Options

Define a network architecture for the CIFAR-10 data set. To simplify the code, use convolutional blocks that convolve the input. The pooling layers downsample the spatial dimensions.

blockDepth = 4;%阻止控制卷积块的深度NetWidth = 32;%NetWidth控制卷积块中过滤器的数量layers = [ imageInputLayer(imageSize) convolutionalBlock(netWidth,blockDepth) maxPooling2dLayer(2,“大步”,,,,2) convolutionalBlock(2*netWidth,blockDepth) maxPooling2dLayer(2,“大步”,,,,2) convolutionalBlock(4*netWidth,blockDepth) averagePooling2dLayer(8) fullyConnectedLayer(10) softmaxLayer classificationLayer ];

Define the training options. Train the network in parallel using the current cluster, by setting the execution environment toparallel。When you use multiple GPUs, you increase the available computational resources. Scale up the mini-batch size with the number of GPUs to keep the workload on each GPU constant. Scale the learning rate according to the mini-batch size. Use a learning rate schedule to drop the learning rate as the training progresses. Turn on the training progress plot to obtain visual feedback during training.

minibatchsize = 256 * numberOfworkers;初始leainnrate = 1e-1 * minibatchSize/256;选项=训练('sgdm',,,,...“执行环境”,,,,'parallel',,,,...% Turn on automatic parallel support.“初始删除”,初始leartrearnrate,...%设置初始学习率。“ MINIBATCHSIZE”,,,,miniBatchSize,...%设置MiniBatchSize。'Verbose',,,,false,...% Do not send command line output.“绘图”,,,,“训练过程”,,,,...%打开培训进度图。“ l2reginalization”,1E-10,...“ maxepochs”,,,,50,...“洗牌”,,,,'every-epoch',,,,...'ValidationData',imdstest,...'ValidationFrequency',,,,floor(numel(imdsTrain.Files)/miniBatchSize),...'LearnRateSchedule',,,,“分段”,,,,...'LearnRatedRopFactor',0.1,...'LearnRatedRopperiod',,,,45);

Train Network and Use for Classification

在集群中训练网络。在培训期间,该情节显示进度。

net = trainnetwork(augmentedimdstrain,层,选项)

net =带有属性的系列网络:层:[43×1 nnet.cnn.layer.layer]

Determine the accuracy of the network, by using the trained network to classify the test images on your local machine. Then compare the predicted labels to the actual labels.

YPredicted = classify(net,imdsTest); accuracy = sum(YPredicted == imdsTest.Labels)/numel(imdsTest.Labels)

Define Helper Function

定义一个函数以在网络体系结构中创建卷积块。

functionlayers = ConvolutionAlblock(NumFilters,NumConvlayers)layers = [卷积2Dlayer(3,numfilters,'Padding',,,,'same') batchNormalizationLayer reluLayer ]; layers = repmat(layers,numConvLayers,1);结尾

也可以看看

||

Related Topics