predictAnd​UpdateStat​e関数での観測値を用​いたネットワークの更​新について

13 views (last 30 days)
Naoto Iwaki
Naoto Iwaki on 16 Oct 2019
Answered: Naoto Iwaki on 17 Oct 2019
深層学習を用いて時系列データの処理を行っているのですが、以下のプログラムを書いて実行したところ最後のfor文内の
[net,YT1pred(:,i)]=predictAndUpdateState(net,XT1Test(:,i),'ExecutionEnvironment','auto');
の部分でエラーを吐き、
「cell からdoubleに変換できない」と警告されました。
ネットワークのリセット以外は将来のタイムステップ予測のところでも似たようなfor文の処理
[net,YT1pred(:,i)]=predictAndUpdateState(net,YT1pred(:,i-1),'ExecutionEnvironment','auto');
を行っており、予測子YT1predを区別してみるなどでワークスペースで一つ一つ確認したところ入力引数の型(cell)は同じだと思いました。
そこで修正方法と可能であればなぜ最後のfor文でエラーを吐いたのかの原因を知りたいです。
以下ソースコード
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列の配列へ
for i=1:300
T1_array{i}=T1_data(1:end,i)';
end
%転置
T1_a=(T1_array)';
%シーケンスの最初の90%で学習を行い残りの10%でテストする
numTimeStepsTrain = floor(0.9*numel(T1_a));
T1Train = T1_a(1:numTimeStepsTrain+1);
T1Test = T1_a(numTimeStepsTrain+1:end);
%予測子と応答の準備
XT1Train = T1Train(1:end-1);
YT1Train = T1Train(2:end);
%lstmアーキテクチャ 定義
numFeatures=1;
numResponse=1;
numHiddenUnits=200;
%lstm層
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(200)
fullyConnectedLayer(numResponse)
regressionLayer];
%トレーニングオプション
options = trainingOptions('adam', ...
'MaxEpochs',20, ...
'GradientThreshold',1, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',10, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress')
net = trainNetwork(XT1Train,YT1Train,layers,options);
XT1Test=T1Test(1:end-1);
net=predictAndUpdateState(net,XT1Train,'MiniBatchSize',1);
[net, YT1pred]=predictAndUpdateState(net, YT1Train(end),'ExecutionEnvironment','auto');
numTimeStepsTest=numel(XT1Test);
for i=2:numTimeStepsTest
[net,YT1pred(:,i)]=predictAndUpdateState(net,YT1pred(:,i-1),'ExecutionEnvironment','auto');
end
%観測値によるネットワーク状態の更新
net=resetState(net);
net=predictAndUpdateState(net,XT1Train,'MiniBatchSize',1);
YT1pred=[];
numTimeStepsTest=numel(XT1Test);
%転置
XT1Test=transpose(XT1Test);
for i=1:numTimeStepsTest
[net,YT1pred(:,i)]=predictAndUpdateState(net,XT1Test(:,i),'ExecutionEnvironment','auto');
end
  1 Comment
michio
michio on 16 Oct 2019
勝手ながらコード表示を編集させて頂きました。
Capture.PNG
赤丸の部分、もしよろしければ活用ください。

Sign in to comment.

Accepted Answer

michio
michio on 16 Oct 2019
YT1pred=[];
YT1pred={}; % 0x0 の空の cell 配列
に変えるとOKです。
[net,YT1pred(:,i)]=predictAndUpdateState(net,XT1Test(:,i),'ExecutionEnvironment','auto');
でエラーが出る原因は出力側にあり、predictAndUpdateState の 第二出力はセル配列ですが、受け取る変数YT1pred(:,i) が double 型であることが原因です。
  1 Comment
michio
michio on 16 Oct 2019
numTimeStepsTest=numel(XT1Test);
YT1pred=cell(1,numTimeStepsTest);
の順の方が良いかと思います。Editor 上でもオレンジ色の下線つきでメッセージが表示されていると思いますが、for ループ内で配列サイズが変わってしまうのは効率が悪いので。

Sign in to comment.

More Answers (1)

Naoto Iwaki
Naoto Iwaki on 17 Oct 2019
問題の修正だけでなく効率性を高くする改善策もありがとうございます。おかげさまで無事解決できました。

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!