predictAnd​UpdateStat​e関数で時系列の予測​とネットワークの更新​をするときに起きた問​題

10 views (last 30 days)
Naoto Iwaki
Naoto Iwaki on 15 Oct 2019
Commented: Naoto Iwaki on 16 Oct 2019
predictAndUpdateState関数でネットワークの更新と将来の予測を行っているのですが、
[net, YT1pred]=predictAndUpdateState(net, YT1Train(end),'ExecutionEnvironment','auto');
 とすると「不適切なネットワークの状態。ネットワークでは 14 のミニバッチ サイズが必要ですが、サイズ 1 のミニバッチが渡されました。」というエラーを吐き出しました。
(深層学習を使用した時系列予測というドキュメンテーションなどを参考にしました
一方、
[net, YT1pred]=predictAndUpdateState(net, YT1Train(end-13:end),'ExecutionEnvironment','auto');
 とすると通りました。トレーニングオプションにおいてミニバッチのエポックの最大回数は変えましたがミニバッチのサイズは調整していないので上の修正で通る理由がわかりません。(上記の深層学習を使用した時系列予測とミニバッチサイズは同じ値でした)
また、predictAndUpdateState上でミニバッチサイズを調整することはできるでしょうか?predictAndUpdateState関数内の入力引数でミニバッチサイズを指定しても無理でした。
%スプレッドシート読み込み (Read the Spreadsheet)
opts = detectImportOptions('pressure_data_20190326_1.xlsx','DataRange','B5');
T1=readtable('pressure_data_20190326_1.xlsx',opts,'ReadVariableNames',false);
T1_data = T1.Variables;
%1行N列の配列へ(Convert to 1×N array)
for i=1:300
T1_array{i}=T1_data(1:end,i)';
end
%転置 (Transpose)
T1_a=(T1_array)';
%シーケンスの最初の90%で学習を行い残りの10%でテストする (Partition the training and test data. Train on the first 90% of the sequence and test on the last 10%.)
numTimeStepsTrain = floor(0.9*numel(T1_a));
T1Train = T1_a(1:numTimeStepsTrain+1);
T1Test = T1_a(numTimeStepsTrain+1:end);
%予測子と応答の準備 (Prepare Predictors and Responses)
XT1Train = T1Train(1:end-1);
YT1Train = T1Train(2:end);
%lstmアーキテクチャ定義 (Define LSTM Network Architecture)
numFeatures=1;
numResponse=1;
numHiddenUnits=200;
%LSTM層 (LSTM layers)
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(200)
fullyConnectedLayer(numResponse)
regressionLayer];
%トレーニングオプション (trainingoptions)
options = trainingOptions('adam', ...
'MaxEpochs',20, ...
'GradientThreshold',1, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',10, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress')
%LSTMネットワークの学習 (Train LSTM Network)
net = trainNetwork(XT1Train,YT1Train,layers,options);
%将来のタイムステップの予測 (Forecast Future Time Steps)
XT1Test=T1Test(1:end-1);
net=predictAndUpdateState(net,XT1Train);
[net, YT1pred]=predictAndUpdateState(net, YT1Train(end),'ExecutionEnvironment','auto');
%[net, YT1pred]=predictAndUpdateState(net, YT1Train(end-13:end),'ExecutionEnvironment','auto');

Accepted Answer

michio
michio on 15 Oct 2019
net=predictAndUpdateState(net,XT1Train);
net=predictAndUpdateState(net,XT1Train,'MiniBatchSize',1);
に変更するととりあえずエラーは避けられます。まだ調査中ですが、コードを実行してみたところ下記の行の実行後に HiddenState, CallState が 200x14になっていることが影響しているようです。
>> net=predictAndUpdateState(net,XT1Train);
>> net.Layers(2)
ans =
LSTMLayer のプロパティ:
Name: 'lstm'
ハイパーパラメーター
InputSize: 1
NumHiddenUnits: 200
OutputMode: 'sequence'
StateActivationFunction: 'tanh'
GateActivationFunction: 'sigmoid'
学習可能パラメーター
InputWeights: [800×1 single]
RecurrentWeights: [800×200 single]
Bias: [800×1 single]
状態パラメーター
HiddenState: [200×14 single]
CellState: [200×14 single]
すべてのプロパティを表示
  2 Comments
michio
michio on 15 Oct 2019
なぜ 14 か分かりましたので追記します。
net=predictAndUpdateState(net,X1Train);
では、最後の mini-batch データによってネットワークの状態が更新されます。
X1Train のサイズは 270x1 、そして predictAndUpdateState の mini-batch サイズはデフォルトで 128 。ですので、X1Train は 128x1, 128x1, 14x1 の mini-batch に分割されることになり、最後の mini-batch、すなわち 14x1 のデータにて状態が更新されるので、上で触れたように HiddenState, CallState が 200x14 となっています。
その後の predictAndUpdateState ではデータサイズ 14x1 セル配列に対してのみ機能することになります。
net=predictAndUpdateState(net,XT1Train,'MiniBatchSize',1);
の代わりに
net=predictAndUpdateState(net,XT1Train,'MiniBatchSize',269);
でもいいですね。
Naoto Iwaki
Naoto Iwaki on 16 Oct 2019
原因追究までありがとうございます。なるほどそのような処理の流れになっていたんですね。
恥ずかしながらミニバッチサイズを指定する処理を「[net, YT1pred]= 」で始まるところでやってしまった勘違いもしていたようで本当に助かりました。

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!