主要内容

预测

使用培训的深度学习神经网络预测响应

描述

You can make predictions using a trained neural network for deep learning on either a CPU or GPU.使用GPU需要并行计算工具箱™和CUDA®启用nvidia.®GPU with compute capability 3.0 or higher.Specify the hardware requirements using theexecultenvironment.name-value pair argument.

ypred.=预测(net,imds)预测图像数据的响应imds使用训练有素SeriesNetwork要么Dagnetwork.objectnet。对于dlnetwork.输入,参见预测

ypred.=预测(net,ds)预测s responses for the data in the datastoreds

ypred.=预测(net,TBL.)预测s responses for the data in the tableTBL.

example

ypred.=预测(net,X)预测数字数组中的图像或特征数据的响应X

ypred.=预测(net,X1,...,XN)预测数字阵列中数据的响应X1, …,XN.对于哑铃输入网络net。输入xi.corresponds to the network inputnet.InputNames(i)

[ypred1,...,ypredm] =预测(___)预测s responses for theMoutputs of a multi-output network using any of the previous syntaxes. The outputypredj.对应于网络输出net.outputnames(j)。To return categorical outputs for the classification output layers, set the'ReturnCategorical'option to真正

example

ypred.=预测(net,sequences)预测s responses for the sequence or time series data insequences使用培训的反复网络(例如,LSTM或GU网络)net

example

___=预测(___,名称,Value)预测s responses with additional options specified by one or more name-value pair arguments.

Tip

当用不同长度的序列进行预测时,迷你批量大小可以影响添加到的输入数据的填充量,这导致不同的预测值。尝试使用不同的值,以查看最适合您的网络。要指定迷你批量大小和填充选项,请使用'minibatchsize''SequenceLength'选项分别。

例子

collapse all

加载样本数据。

[xtrain,ytrain] = DigitTrain4darraydata;

digittrain4darraydata.loads the digit training set as 4-D array data.XTrain.is a 28-by-28-by-1-by-5000 array, where 28 is the height and 28 is the width of the images. 1 is the number of channels and 5000 is the number of synthetic images of handwritten digits.YTrainis a categorical vector containing the labels for each observation.

Construct the convolutional neural network architecture.

层= [......imageInputLayer([28 28 1]) convolution2dLayer(5,20) reluLayer maxPooling2dLayer(2,'Stride',2)全连接列(10)SoftmaxLayer分类层];

将随机梯度下降的默认设置设置为带有动量的选项。

选择= trainingOptions('sgdm');

Train the network.

rng('default')net = trainnetwork(xtrain,ytrain,图层,选项);
单CPU培训。初始化输入数据归一化。| ========================================================================================|时代|迭代|经过时间的时间迷你批量|迷你批量|基础学习| | | | (hh:mm:ss) | Accuracy | Loss | Rate | |========================================================================================| | 1 | 1 | 00:00:00 | 10.16% | 2.3195 | 0.0100 | | 2 | 50 | 00:00:03 | 50.78% | 1.7102 | 0.0100 | | 3 | 100 | 00:00:06 | 63.28% | 1.1632 | 0.0100 | | 4 | 150 | 00:00:09 | 60.16% | 1.0859 | 0.0100 | | 6 | 200 | 00:00:12 | 68.75% | 0.8996 | 0.0100 | | 7 | 250 | 00:00:15 | 76.56% | 0.7920 | 0.0100 | | 8 | 300 | 00:00:18 | 73.44% | 0.8411 | 0.0100 | | 9 | 350 | 00:00:21 | 81.25% | 0.5508 | 0.0100 | | 11 | 400 | 00:00:24 | 90.62% | 0.4744 | 0.0100 | | 12 | 450 | 00:00:27 | 92.19% | 0.3614 | 0.0100 | | 13 | 500 | 00:00:30 | 94.53% | 0.3160 | 0.0100 | | 15 | 550 | 00:00:34 | 96.09% | 0.2544 | 0.0100 | | 16 | 600 | 00:00:37 | 92.19% | 0.2765 | 0.0100 | | 17 | 650 | 00:00:39 | 95.31% | 0.2460 | 0.0100 | | 18 | 700 | 00:00:42 | 99.22% | 0.1418 | 0.0100 | | 20 | 750 | 00:00:45 | 98.44% | 0.1000 | 0.0100 | | 21 | 800 | 00:00:48 | 98.44% | 0.1449 | 0.0100 | | 22 | 850 | 00:00:51 | 98.44% | 0.0989 | 0.0100 | | 24 | 900 | 00:00:54 | 96.88% | 0.1315 | 0.0100 | | 25 | 950 | 00:00:57 | 100.00% | 0.0859 | 0.0100 | | 26 | 1000 | 00:01:01 | 100.00% | 0.0701 | 0.0100 | | 27 | 1050 | 00:01:06 | 100.00% | 0.0759 | 0.0100 | | 29 | 1100 | 00:01:10 | 99.22% | 0.0663 | 0.0100 | | 30 | 1150 | 00:01:15 | 98.44% | 0.0776 | 0.0100 | | 30 | 1170 | 00:01:17 | 99.22% | 0.0732 | 0.0100 | |========================================================================================|

Run the trained network on a test set and predict the scores.

[xtest,ytest] = digittest4darraydata;ypred =预测(net,xtest);

预测, by default, uses a CUDA® enabled GPU with compute capability 3.0, when available. You can also choose to run预测on a CPU using the'executionenvironment','cpu'name-value pair argument.

在测试数据中显示前10个图像,并与预测相比预测

YTest(1:10,:)
ans =10x1 categorical0 0 0 0 0 0 0 0 0 0 0
Ypred(1:10,:)
ans =10x10 single matrix0.9978 0.0001 0.0008 0.0002 0.0003 0.0000 0.0004 0.0000 0.0002 0.0003 0.8881 0.0000 0.0474 0.0001 0.0000 0.0002 0.0029 0.0001 0.0014 0.0598 0.9998 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.9814 0.0000 0.0000 0.0000 0.0000 0.0000 0.0046 0.0000 0.0011 0.0129 0.9748 0.0000 0.0132 0.0003 0.0000 0.0000 0.0002 0.0004 0.0111 0.0001 0.9873 0.0000 0.0001 0.0000 0.0000 0.0000 0.0007 0.0000 0.0072 0.0047 0.9981 0.0000 0.0000 0.0000 0.0000 0.0000 0.0018 0.0000 0.0000 0.0000 1.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.9265 0.0000 0.0046 0.0000 0.0006 0.0009 0.0001 0.0000 0.0018 0.0655 0.9327 0.0000 0.0139 0.0012 0.0001 0.0001 0.0378 0.0000 0.0111 0.0031

YTestcontains the digits corresponding to the images inXTest.。列的列ypred.contain预测估计图像包含特定数字的概率。也就是说,第一列包含给定图像是数字0的概率估计,第二列包含图像是数字1的概率估计,第三列包含图像是数字2的概率估计,等等。你可以看到预测对正确数字的概率估计几乎是1,任何其他数字的概率几乎为0。预测correctly estimates the first 10 observations as digit 0.

Load pretrained network.JapaneseVowelsNet是在日本元音数据集上培训的预磨削的LSTM网络,如[1]和[2]中所述。它训练在序列长度排序的序列中,含有迷你批量大小为27。

loadJapaneseVowelsNet

查看网络架构。

net.Layers
ANS = 5x1层阵列具有图层:1'SENDUNINPUT'序列输入序列输入序列输入12尺寸2'LSTM'LSTM LSTM LSTM LSTM为100个隐藏单元3'FC'完全连接的9完全连接的第4层SoftMax'SoftMax SoftMax SoftMax 5'ClassOutput'分类输出Crossentropyex与“1”和其他8个类

加载测试数据。

[XTest,YTest] = japaneseVowelsTestData;

对测试数据进行预测。

ypred =预测(net,xtest);

查看前10个序列的预测分数。

Ypred(1:10,:)
ans =10x9 single matrix0.9918 0.0000 0.0000 0.0000 0.0006 0.0010 0.0001 0.0006 0.0059 0.9868 0.0000 0.0000 0.0000 0.0006 0.0010 0.0001 0.0010 0.0105 0.9924 0.0000 0.0000 0.0000 0.0006 0.0010 0.0001 0.0006 0.0054 0.9896 0.0000 0.0000 0.0000 0.0006 0.0009 0.0001 0.0007 0.0080 0.9965 0.0000 0.0000 0.0000 0.0007 0.0009 0.0000 0.0003 0.0016 0.9888 0.0000 0.0000 0.0000 0.0006 0.0010 0.0001 0.0008 0.0087 0.9886 0.0000 0.0000 0.0000 0.0006 0.0010 0.0001 0.0008 0.0089 0.9982 0.0000 0.0000 0.0000 0.0006 0.0007 0.0000 0.0001 0.0004 0.9883 0.0000 0.0000 0.0000 0.0006 0.0010 0.0001 0.0008 0.0093 0.9959 0.0000 0.0000 0.0000 0.0007 0.0011 0.0000 0.0004 0.0019

将这些预测分数与这些序列的标签进行比较。该函数将高预测分数分配给正确的类。

YTest(1:10)
ans =10x1 categorical1 1 1 1 1 1 1 1 1 1

输入Arguments

collapse all

训练有素的网络,指定为aSeriesNetwork要么aDagnetwork.目的。You can get a trained network by importing a pretrained network (for example, by using thegooglenet.function) or by training your own network usingtrainNetwork

图像数据存储, specified as anImageDatastore目的。

ImageDatastore允许使用预取批量读取JPG或PNG图像文件。如果您使用自定义函数来读取图像,那么ImageDatastore没有预取。

Tip

UseAugmentedimageGedataStore.用于高学习的图像高效预处理,包括图像调整大小。

不要使用readfcn.option ofimageageAtastore.for preprocessing or resizing as this option is usually significantly slower.

数据存储的内存不足data and preprocessing. The datastore must return data in a table or a cell array. The format of the datastore output depends on the network architecture.

Network Architecture 数据存储输出 Example Output
Single input

表或单元格数组,第一列指定预测器。

Table elements must be scalars, row vectors, or 1-by-1 cell arrays containing a numeric array.

自定义数据存储必须输出表。

数据=读取(DS)
data = 4×1 table Predictors __________________ {224×224×3 double} {224×224×3 double} {224×224×3 double} {224×224×3 double}
数据=读取(DS)
data = 4×1 cell array {224×224×3 double} {224×224×3 double} {224×224×3 double} {224×224×3 double}
多个输入

Cell array with at leastnumInputs列,其中numInputsis the number of network inputs.

首先numInputs列为每个输入指定预测器。

The order of inputs is given by the输入名称sproperty of the network.

数据=读取(DS)
data = 4×2 cell array {224×224×3 double} {128×128×3 double} {224×224×3 double} {128×128×3 double} {224×224×3 double} {128×128×3 double} {224×224×3 double} {128×128×3 double}

预测器的格式取决于数据的类型。

Data 格式of Predictors
2-D图像

h-通过-w-通过-c数字数组,其中h,w, andcare the height, width, and number of channels of the image, respectively.

3-D图像

h-通过-w-通过-d-通过-c数字数组,其中h,w,d, andc是图像的高度,宽度,深度和图像的通道数。

Vector sequence

c-通过-smatrix, wherecis the number of features of the sequence ands是序列长度。

2-D图像序列

h-通过-w-通过-c-通过-s阵列,其中h,w, andccorrespond to the height, width, and number of channels of the image, respectively, ands是序列长度。

迷你批处理中的每个序列必须具有相同的序列长度。

3-D图像序列

h-通过-w-通过-d-通过-c-通过-s阵列,其中h,w,d, andccorrespond to the height, width, depth, and number of channels of the image, respectively, ands是序列长度。

迷你批处理中的每个序列必须具有相同的序列长度。

特征

c-1栏载体,在哪里c是功能的数量。

有关更多信息,请参阅数据存储s for Deep Learning

Image or feature data, specified as a numeric array. The size of the array depends on the type of input:

输入 描述
2-D图像s Ah-通过-w-通过-c-通过-N数字数组,其中h,w, andcare the height, width, and number of channels of the images, respectively, andNis the number of images.
3-D图像 Ah-通过-w-通过-d-通过-c-通过-N数字数组,其中h,w,d, andcare the height, width, depth, and number of channels of the images, respectively, andNis the number of images.
特征 AN-通过-numfeatures.数字数组,其中N是观察人数和numfeatures.is the number of features of the input data.

如果阵列包含NaNS,然后它们通过网络传播。

对于networks with multiple inputs, you can specify multiple arraysX1, …,XN.,在哪里Nis the number of network inputs and the inputxi.corresponds to the network inputnet.InputNames(i)

序列或时间序列数据,指定为一个N-1个数字阵列的1个单元阵列,其中N是观察的数量,表示单个序列或数据存储的数字数组。

对于单元阵列或数字阵列输入,包含序列的数字阵列的尺寸取决于数据的类型。

输入 描述
矢量序列 c-通过-smatrices, wherec是序列的特征数量和s是序列长度。
2-D图像序列s h-通过-w-通过-c-通过-sarrays, whereh,w, andc对应于图像的高度,宽度和数量,以及s是序列长度。
3-D图像序列s h-通过-w-通过-d-通过-c-通过-s,在哪里h,w,d, andc对应于3-D图像的高度,宽度,深度和数量,以及s是序列长度。

对于数据存储区输入,数据存储必须将数据返回为序列的单元格数组或第一列包含序列的表。序列数据的尺寸必须对应于上表。

图像或特征数据表。表中的每一行对应于观察。

The arrangement of predictors in the table columns depend on the type of input data.

输入 预测因子
图像数据
  • 图像中的绝对或相对文件路径,指定为单列中的字符向量

  • 指定为3-D数字数组的图像

Specify predictors in a single column.

Feature data

Numeric scalar.

Specify predictors innumfeatures.columns of the table, wherenumfeatures.is the number of features of the input data.

This argument supports networks with a single input only.

数据类型:table

名称-Value Pair Arguments

例:'minibatchsize',256specifies the mini-batch size as 256.

指定可选的逗号分隔对名称,Value论据。名称is the argument name and是相应的价值。名称必须出现在单引号内('').

用于预测的迷你批次的大小,指定为正整数。较大的迷你批量大小需要更多内存,但可以导致更快的预测。

当用不同长度的序列进行预测时,迷你批量大小可以影响添加到的输入数据的填充量,这导致不同的预测值。尝试使用不同的值,以查看最适合您的网络。要指定迷你批量大小和填充选项,请使用'minibatchsize''SequenceLength'选项分别。

例:'minibatchsize',256

性能优化,指定为逗号分隔对组成'Acceleration'和one of the following:

  • 'auto'- 自动应用适合输入网络和硬件资源的许多优化。

  • 'mex'— Compile and execute a MEX function. This option is available when using a GPU only.使用GPU需要并行计算工具箱和CUDA启用的NVIDIA GPU,其中计算能力3.0或更高。如果不可用并行计算工具箱或合适的GPU,则软件返回错误。

  • 'none'— Disable all acceleration.

默认选项是'auto'。如果'auto'被指定,matlab®will apply a number of compatible optimizations. If you use the'auto'选项,MATLAB不会生成MEX功能。

使用'Acceleration'选择'auto''mex'可以提供绩效福利,但牺牲了增加的初始运行时间。具有兼容参数的后续调用更快。使用新输入数据计划多次调用函数时,使用性能优化。

The'mex'option generates and executes a MEX function based on the network and parameters used in the function call. You can have several MEX functions associated with a single network at one time. Clearing the network variable also clears any MEX functions associated with that network.

The'mex'option is only available when you are using a GPU. You must have a C/C++ compiler installed and the GPU Coder™ Interface for Deep Learning Libraries support package. Install the support package using the Add-On Explorer in MATLAB. For setup instructions, seeMEX Setup(GPU编码器)。不需要GPU编码器。

The'mex'选项不支持所有图层。万博1manbetx有关支持的图层列表,请参阅万博1manbetx万博1manbetx支持的层数(GPU编码器)。包含a的经常性神经网络(RNN)sequenceInputlayer.不受支持。万博1manbetx

The'mex'选项不支持具有多个输入图层或多个万博1manbetx输出层的网络。

你不能使用马铃薯Compiler™使用时部署网络'mex'option.

例:'Acceleration','mex'

硬件资源,指定为逗号分隔对组成'ExecutionEnvironment'和one of the following:

  • 'auto'- 如果有可用的话,请使用GPU;否则,使用CPU。

  • 'gpu'— Use the GPU.使用GPU需要并行计算工具箱和CUDA启用的NVIDIA GPU,其中计算能力3.0或更高。如果不可用并行计算工具箱或合适的GPU,则软件返回错误。

  • '中央处理器'— Use the CPU.

例:'executionenvironment','cpu'

选项返回分类标签,指定为真正要么false

如果return类别is真正,然后该函数返回分类输出层的分类标签。否则,该函数返回对分类输出层的预测分数。

选项要填充,截断或拆分输入序列,指定为以下之一:

  • '最长'- 每个迷你批处理中的缝合序列具有与最长序列相同的长度。此选项不会丢弃任何数据,但填充可以向网络引入噪点。

  • “最短”— Truncate sequences in each mini-batch to have the same length as the shortest sequence. This option ensures that no padding is added, at the cost of discarding data.

  • 正整数 - 对于每个小批次,填充序列到最接近的指定长度的倍数大于小批次中最长的序列长度,然后将序列分成规定长度的较小序列。如果发生拆分,则软件会创建额外的百分之批处理。如果完整序列不适合内存,请使用此选项。或者,尝试通过设置序列减少每百分之批次的序列数'minibatchsize'值较低的值。

To learn more about the effect of padding, truncating, and splitting the input sequences, see序列填充,截断和分裂

例:'Sequencelength','最短'

填充或截断的方向,指定为以下之一:

  • 'right'- 右侧的垫或截断序列。该序列在同一时间步骤开始,软件截断或将填充添加到序列的末尾。

  • 'left'- 左侧或截断序列。该软件截断或将填充添加到序列的开始,使序列同时结束。

因为LSTM层次处理序列数据一次一次步骤,当图层时OutputMode属性是'last', any padding in the final time steps can negatively influence the layer output. To pad or truncate sequence data on the left, set the'sequencdpaddingdirection'option to'left'

对于sequence-to-sequence networks (when theOutputMode属性是'sequence'for each LSTM layer), any padding in the first time steps can negatively influence the predictions for the earlier time steps. To pad or truncate sequence data on the right, set the'sequencdpaddingdirection'option to'right'

To learn more about the effect of padding, truncating, and splitting the input sequences, see序列填充,截断和分裂

值by which to pad input sequences, specified as a scalar. The option is valid only whenSequenceLengthis'最长'或正整数。不要用垫缝NaN,因为这样做可以在整个网络中传播错误。

例:'sequencdpaddingsvalue',-1

输出参数

collapse all

Predicted scores or responses, returned as a matrix, a 4-D numeric array, or a cell array of matrices. The format ofypred.取决于问题的类型。

下表描述了分类问题的格式。

Task 格式
图像分类 N-通过-Kmatrix, whereN是观察人数,和Kis the number of classes
Sequence-to-label classification
Feature classification
Sequence-to-sequence classification

N-通过-1 cell array of matrices, whereNis the number of observations. The sequences are matrices withK行,在哪里K是课程的数量。在应用后,每个序列具有与相应的输入序列相同的时间步骤SequenceLength每个迷你批处理的选项独立。

The following table describes the format for regression problems.

Task 格式
2-D图像regression
  • N-通过-Rmatrix, whereN是图像的数量和R是响应的数量。

  • h-通过-w-通过-c-通过-N数字数组,其中h,w, andcare the height, width, and number of channels of the images, respectively, andNis the number of images.

3-D图像回归
  • N-通过-Rmatrix, whereN是图像的数量和R是响应的数量。

  • h-通过-w-通过-d-通过-c-通过-N数字数组,其中h,w,d, andcare the height, width, depth, and number of channels of the images, respectively, andNis the number of images.

Sequence-to-one regression N-通过-Rmatrix, whereN是序列的数量和R是响应的数量。
Sequence-to-sequence regression

N-通过-1 cell array of numeric sequences, whereN是序列的数量。序列是矩阵R行,在哪里R是响应的数量。在应用后,每个序列具有与相应的输入序列相同的时间步骤SequenceLength每个迷你批处理的选项独立。

与一个sequence-to-sequence回归任务observation,sequencescan be a matrix. In this case,ypred.is a matrix of responses.

特征回归

N-通过-Rmatrix, whereN是观察人数和R是响应的数量。

对于sequence-to-sequence regression problems with one observation,sequencescan be a matrix. In this case,ypred.is a matrix of responses.

Algorithms

如果图像数据包含NaNS,预测通过网络传播它们。如果网络具有Relu层,则这些图层忽略了NaNs。但是,如果网络没有Relu层,那么预测将NAN返回预测。

深度学习培训,预测和验证的所有功能,深度学习工具箱™使用单精度浮点算术执行计算。深度学习的功能包括trainNetwork,预测,分类, and激活。当您使用CPU和GPU培训网络时,该软件使用单精度算术。

Alternatives

You can compute the predicted scores and the predicted classes from a trained network using分类

You can also compute the activations from a network layer using激活

对于序列到标签和序列到序列分类网络(例如,LSTM网络),您可以使用预测和更新网络状态classifyandupdateState.predictandanddatestate.

References

[1] M. Kudo,J. Toyama和M. Shimbo。“使用过度区域的多维曲线分类。”模式识别字母。卷。20,第11-13页,第1103-1111页。

[2]UCI Machine Learning Repository: Japanese Vowels Dataset。https://archive.ics.uci.edu/ml/datasets/japanese+vowels.

扩展能力

Introduced in R2016a