関数endclearに関するエラーの解決法

1 次查看(过去 30 天)
塁 小熊
塁 小熊 2021-12-6
评论: 塁 小熊 2021-12-7
こんにちは。小熊塁です。
cd 'D:\Thasis\回帰用データセット25転移'
folder_name=pwd;
imds=imageDatastore(folder_name);
XValidation=zeros(227,227,1,25);
for i=1:numel(imds.Files)
I=imread(imds.Files{i});
XValidation(:,:,:,i)=I;
end
YTrain=[216.7;276.7;258.3;126.7;223.3;156.7;215.0;213.3;203.3;258.3;
143.3;180.0;148.3;261.7;163.3;235.0;220;305.0;183.3;146.7;200.0;175.0;123.3;106.7;178.3;206.7;183.3;
145.0;245.0;218.3;220.0;270.0;148.3;163.3;193.3;215.0;141.7;281.7;173.3;156.7;176.7;146.7;205.0;
168.3;195.0;220.0;113.3;131.7;115.0;105.0;108.3;118.3;143.3;118.3;95.0;115.0;123.3;173.3;216.7;113.3;181.7;161.7;135.0;
141.7;123.3;216.7;153.3;233.3;151.7;153.3];
YValidation=[174.2;313.3;286.7;316.7;145.0;193.3;316.7;243.3;228.3;241.7;230.0;115.0;206.7;216.7;138.3;146.7;223.3;275.0;163.3;158.3;128.3;216.7;158.3;138.3;135.0];
net = squeezenet;
layers = net.Layers;
numResponses = 1;
layers = [
layers(1:12)
fullyConnectedLayer(numResponses)
regressionLayer];
layers(1:12) = freezeWeights(layers(1:12));
options = trainingOptions('sgdm',...
'InitialLearnRate',0.001, ...
'ValidationData',{XValidation,YValidation},...
'Plots','training-progress',...
'Verbose',false);
net = trainNetwork(XTrain,YTrain,layers,options);
YPred = predict(net,XValidation);
predictionError = YValidation - YPred;
thr = 10;
numCorrect = sum(abs(predictionError) < thr);
numImagesValidation = numel(YValidation);
accuracy = numCorrect/numImagesValidation
rmse = sqrt(mean(predictionError.^2))
という風なコードを作成したのですが、関数freezeWeightsにおける
function layers = freezeWeights(layers)
for ii = 1:size(layers,1)
props = properties(layers(ii));
for p = 1:numel(props)
propName = props{p};
if ~isempty(regexp(propName, 'LearnRateFactor$', 'once'))
layers(ii).(propName) = 0;
end
end
endclear
end
endclearが認識されず、学習まで行きつけません。
もしこれを解決する方法を知っている方がいれば、ご助言を頂きたいです。
よろしくお願いします。

采纳的回答

Kojiro Saito
Kojiro Saito 2021-12-6
ドキュメントのコード例を確認しましたが、freezeWeightsの中身はendclearではなく、endだけになっていました。
function layers = freezeWeights(layers)
for ii = 1:size(layers,1)
props = properties(layers(ii));
for p = 1:numel(props)
propName = props{p};
if ~isempty(regexp(propName, 'LearnRateFactor$', 'once'))
layers(ii).(propName) = 0;
end
end
end % ← endのみにする
end
おそらく、ワークスペースを消去するために使ったclearコマンドが、freezeWeights.mのfor文のendの後に追記されてしまったのかと思われます。
  1 个评论
塁 小熊
塁 小熊 2021-12-7
なるほど、そういうことでしたか…。
clearを消して保存したところ、無事に動作しました!
ありがとうございます。

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Deep Learning Toolbox 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!