Matlab LSTM

数据格式

样本一列数据存入cell的一行
标签为一列 categorical类型
网络设置时分类为last,回归为sequence

代码

%数据参数
inputSize = 784;
numClasses = 10;

%结构
layers = [ ...
    sequenceInputLayer(inputSize)
    lstmLayer(125,'OutputMode','sequence')
    lstmLayer(125,'OutputMode','last')
    fullyConnectedLayer(numClasses)
    softmaxLayer()
    classificationLayer()
    ];

%参数
options = trainingOptions('adam', ...
    'MaxEpochs',8000, ...
    'MiniBatchSize',7000, ...
    'Verbose',true, ...
    'InitialLearnRate',0.02, ...
    'L2Regularization',0.0010, ...
    'LearnRateDropPeriod',4, ...
    'LearnRateDropFactor',0.04, ...
    'plots','training-progress');

%训练
net = trainNetwork(TrainData,TrainLabels,layers,options);

%测试
YPred = classify(net,TestData, ...
    'MiniBatchSize',7000, ...
    'SequenceLength','longest');

%准确率
acc = sum(YPred == TestLabels)./numel(TestLabels)

你可能感兴趣的:(Matlab)