主要内容

classifyAndUpDateState

使用训练有素的复发神经网络对数据进行分类,并更新网络状态

描述

您可以在CPU或GPU上使用经过训练的深度学习网络进行预测。Using a GPU requires Parallel Computing Toolbox™ and a supported GPU device. For information on supported devices, see释放的G金宝appPU支持(并行计算工具箱)Specify the hardware requirements using the'ExecutionEnvironment'name-value pair argument.

example

[updatedNet,ypred] = classifyAndUpdateState(recnet,sequences)classifies the data insequencesusing the trained recurrent neural networkrecnet并更新网络状态。

This function supports recurrent neural networks only. The inputrecnetmust have at least one recurrent layer.

[updatedNet,ypred] = classifyAndUpdateState(___,姓名,Value)uses any of the arguments in the previous syntaxes and additional options specified by one or more姓名,Value配对参数。例如,“ MINIBATCHSIZE”,27使用大小27的迷你批次对数据进行分类

分类和更新网络状态

example

[updatedNet,ypred,分数] = classifyAndUpdateState(___)在上一个语法中使用任何参数,返回分类得分的矩阵,并更新网络状态。

Tip

在使用不同长度的序列进行预测时,微型批量大小会影响输入数据中添加的填充量,从而导致不同的预测值。尝试使用不同的值以查看哪些与您的网络最有效。To specify mini-batch size and padding options, use the“ MINIBATCHSIZE”'SequenceLength'选项分别。

例子

collapse all

Classify data using a recurrent neural network and update the network state.

LoadJapaneseVowelsNet,如[1]和[2]中所述,对日本元音数据集进行了训练的预处理的长期记忆(LSTM)网络。该网络对按序列长度排序的序列进行了训练,小批量大小为27。

loadJapaneseVowelsNet

查看网络体系结构。

net.Layers
ANS = 5x1层阵列带有层:1'sequenceInput'序列输入序列输入,带12个维度2'lstm'lstm lstm,带有100个隐藏单元3'FC'完全连接的9个完全连接的层4'SoftMax'Softmax'Softmax'SoftMax Softmax 5'SoftMax 5'ClastOutput'分类'输出crossentropyex,具有“ 1”和其他8个类别

加载测试数据。

[XTest,YTest] = japaneseVowelsTestData;

在序列中循环循环。对每个时间步骤进行分类并更新网络状态。

X = XTest{94}; numTimeSteps = size(X,2);fori = 1:numTimeSteps v = X(:,i); [net,label,score] = classifyAndUpdateState(net,v); labels(i) = label;end

在楼梯图中绘制预测标签。该图显示了时间步长之间的预测如何变化。

figure stairs(labels,'-o') xlim([1 numTimeSteps]) xlabel(“时间步长”)ylabel(“预测阶级”) 标题(“随时间步长的分类”)

图包含一个轴对象。带有标题分类的轴对象在时间步骤中包含类型阶段的对象。

将预测与真标签进行比较。绘制一条水平线,显示观察值的真实标签。

truelabel = ytest(94)
truelabel =分类3
hold线([1个NumTimesteps],[Truelabel Truelabel],...'颜色','red',...'LineStyle','--') legend(["Prediction""True Label")))

图包含一个轴对象。The axes object with title Classification Over Time Steps contains 2 objects of type stair, line. These objects represent Prediction, True Label.

输入Arguments

collapse all

训练有素的复发性神经网络,指定为SeriesNetwork或者adagnetwork目的。您可以通过导入验证的网络或使用该网络来获得训练有素的网络trainNetworkfunction.

recnet是一个经常性的神经网络。它必须至少具有一个复发层(例如,LSTM网络)。

序列或时间序列数据,指定为N-1个数字阵列的by-1单元格数组,其中N是观测值的数量,代表单个序列或数据存储的数字阵列。

细胞数组或数字数组输入,dimensi上s of the numeric arrays containing the sequences depend on the type of data.

输入 描述
向量序列 c-经过-smatrices, wherec是序列的功能数量,s是序列长度。
1-D image sequences h-经过-c-经过-sarrays, wherehc分别对应图像的高度和数量,并且s是序列长度。
2-D image sequences h-经过-w-经过-c-经过-sarrays, whereh,w, 和c分别对应于图像的高度,宽度和数量,并且s是序列长度。
3-D image sequences h-经过-w-经过-d-经过-c-经过-s, 在哪里h,w,d, 和c分别对应于3-D图像的高度,宽度,深度和数量,并且s是序列长度。

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

姓名-Value Arguments

Specify optional comma-separated pairs of姓名,Value参数。姓名is the argument name and价值是相应的值。姓名must appear inside quotes. You can specify several name and value pair arguments in any order as姓名1,Value1,...,NameN,ValueN

例子:[updatedNet, YPred] = classifyAndUpdateState(recNet,C,'MiniBatchSize',27)使用大小27的迷你批次对数据进行分类。

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

在使用不同长度的序列进行预测时,微型批量大小会影响输入数据中添加的填充量,从而导致不同的预测值。尝试使用不同的值以查看哪些与您的网络最有效。To specify mini-batch size and padding options, use the“ MINIBATCHSIZE”'SequenceLength'选项分别。

例子:“ MINIBATCHSIZE”,256

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

  • 'auto'— Automatically apply a number of optimizations suitable for the input network and hardware resources.

  • 'none'— Disable all acceleration.

默认选项是'auto'

使用'Acceleration'option'auto'可以提供绩效优势,但以增加初始运行时间为代价。随后的兼容参数的调用速度更快。当您计划使用新输入数据多次调用功能时,请使用性能优化。

例子:“加速”,“自动”

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

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

  • 'gpu'— Use the GPU.使用GPU需要并行计算工具箱和支持的GPU设备。金宝app有关支持设备的信息,请参阅金宝app释放的G金宝appPU支持(并行计算工具箱)如果不可用并行计算工具箱或合适的GPU,则软件将返回错误。

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

例子:“执行环境”,“ CPU”

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

  • 'longest'- 每个迷你批次中的垫序列具有与最长序列相同的长度。该选项不会丢弃任何数据,尽管填充可以向网络引入噪声。

  • 'shortest'— 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序列填充,截断和分裂

例子:“序列长度”,“最短”

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

  • 'right'- 右侧的垫子或截断序列。序列从同一时间步长开始,软件会截断或添加填充到序列的末尾。

  • '剩下'- 左侧的PAD或截断序列。该软件会截断或添加填充序列的开始,以便序列在同一时间步长结束。

因为LSTM层一次过程序列数据一次一次,当OutputModeproperty is'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“序列界”option to'剩下'

For sequence-to-sequence networks (when theOutputModeproperty is'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“序列界”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'longest'或一个积极的整数。不要与NaN,因为这样做可以在整个网络中传播错误。

例子:“序列值”,-1

输出参数

collapse all

Updated network.updatedNetis the same type of network as the input network.

预测的类标签,返回为分类向量或分类向量的单元格数组。格式ypred取决于问题的类型。

The following table describes the format ofypred

Task 格式
Sequence-to-label classification N-1 by-1标签的分类向量,其中N是观察的数量。
Sequence-to-sequence classification

N-经过-1 cell array of categorical sequences of labels, whereN是观察的数量。每个序列都具有与相应输入序列相同的时间步骤SequenceLength独立的每个迷你批次的选项。

对于序列到序列分类问题,一个观察结果,sequencescan be a matrix. In this case,ypred是标签的分类序列。

Predicted class scores, returned as a matrix or a cell array of matrices. The format of分数取决于问题的类型。

The following table describes the format of分数

Task 格式
Sequence-to-label classification N-经过-Kmatrix, whereN是观察的数量,并且K是类的数量。
Sequence-to-sequence classification

N-经过-1 cell array of matrices, whereN是观察的数量。The sequences are matrices withK行,哪里K是类的数量。每个序列都具有与相应输入序列相同的时间步骤SequenceLength独立的每个迷你批次的选项。

对于序列到序列分类问题,一个观察结果,sequencescan be a matrix. In this case,分数是预测类得分的矩阵。

Algorithms

当您使用网络训练网络时trainNetwork功能,或使用预测或验证功能dagnetworkSeriesNetworkobjects, the software performs these computations using single-precision, floating-point arithmetic. Functions for training, prediction, and validation includetrainNetwork,预测,classify, 和激活。当您同时使用CPU和GPU训练网络时,该软件使用单精度算术。

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 R2017b