This example shows how to train a deep learning network on out-of-memory sequence data by transforming and combining datastores.
A transformed datastore transforms or processes data read from an underlying datastore. You can use a transformed datastore as a source of training, validation, test, and prediction data sets for deep learning applications. Use transformed datastores to read out-of-memory data or to perform specific preprocessing operations when reading batches of data. When you have separate datastores containing predictors and labels, you can combine them so you can input the data into a deep learning network.
When training the network, the software creates mini-batches of sequences of the same length by padding, truncating, or splitting the input data. For in-memory data, thetrainingOptions
函数提供了填充和截断输入序列的选项,但是,对于存储数据外的数据,您必须手动填充和截断序列。
负载培训数据
Load the Japanese Vowels data set as described in [1] and [2]. The zip filejapaneseVowels.zip
contains sequences of varying length. The sequences are divided into two folders,火车
andTest
,分别包含训练序列和测试序列。在这些文件夹中的每个文件夹中,序列都分为子文件夹,这些子文件夹从1
to9
。这names of these subfolders are the label names. A MAT file represents each sequence. Each sequence is a matrix with 12 rows, with one row for each feature, and a varying number of columns, with one column for each time step. The number of rows is the sequence dimension and the number of columns is the sequence length.
解压缩序列数据。
对于培训预测指标,创建文件数据存储并指定读取功能为加载
功能。这加载
function, loads the data from the MAT-file into a structure array. To read files from the subfolders in the training folder, set the'IncludeSubfolders'
option totrue
。
Preview the datastore. The returned struct contains a single sequence from the first file.
对于标签,创建一个文件数据存储,并指定读取函数为readLabel
函数,定义在示例的末尾。这readLabel
function extracts the label from the subfolder name.
Preview the datastore. The output corresponds to the label of the first file.
转换并组合数据存储
要将序列数据从预测变量数据存储到深度学习网络中输入序列数据,序列的迷你批次必须具有相同的长度。使用padSequence
function, defined at the end of the datastore, that pads or truncates the sequences to have length 20.
Preview the transformed datastore. The output corresponds to the padded sequence from the first file.
X =1×1 cell array{12×20 double}
要将两个数据库的预测变量和标签输入到深度学习网络中,请使用combine
功能。
Preview the combined datastore. The datastore returns a 1-by-2 cell array. The first element corresponds to the predictors. The second element corresponds to the label.
ans =1×2单元格数组{12×20 double} {[1]}
Define LSTM Network Architecture
定义LSTM网络体系结构。将输入数据的功能数量指定为输入大小。指定具有100个隐藏单元的LSTM层,并输出序列的最后一个元素。最后,指定一个完全连接的层,其输出大小等于类的数量,然后是软磁层和分类层。
Specify the training options. Set the solver to'adam'
and“梯度阈值”
to 2. Set the mini-batch size to 27 and set the maximum number of epochs to 75. The datastores do not support shuffling, so set“洗牌”
to'never'
。
由于迷你批次很小,序列短,因此CPU更适合训练。放'ExecutionEnvironment'
to'cpu'
。在GPU上训练,如果有的话,请设置'ExecutionEnvironment'
to'auto'
(the default value).
火车the LSTM network with the specified training options.
Test the Network
创建一个转换包含了数据存储-out test data using the same steps as for the training data.
Make predictions on the test data using the trained network.
Calculate the classification accuracy on the test data. To get the labels of the test set, create a file datastore with the read functionreadLabel
并指定包括子文件夹。指定输出是通过设置的垂直限制的“统一”
option totrue
。
Functions
这readLabel
功能从指定的文件名中提取标签classNames
。
这padSequence
function pads or truncates the sequence indata.x
to have the specified sequence length and returns the result in a 1-by-1 cell.