Transfer Learning with GoogLeNet Error when Freezing Weights

조회 수: 31 (최근 30일)
Andrew Chen
Andrew Chen 2018년 4월 6일
댓글: Runcong Kuang 2022년 8월 17일
Hello,
Recently I have been exploring the machine learning and neural network capabilities of Matlab and I have been trying to use transfer learning on the GoogLeNet pretrained network to see how it performs on some text recognition datasets.
I have been following the syntax closely in the documentation here but I'm encountering some errors when I try to freeze the initial layers. The syntax in the example is as follows:
layers = lgraph.Layers;
connections = lgraph.Connections;
layers(1:110) = freezeWeights(layers(1:110));
lgraph = createLgraphUsingConnections(layers,connections);
For some reason when trying to set the layers and connections variables as is shown above, I get the error message "The expression to the left of the equals sign is not a valid target for an assignment". This happens both when I try to assign the 'layers' and 'connections' variables. Strangely enough, I can set both of these variables if I first type 'lgraph.Layers' or 'lgraph.Connections' into the workspace and then following those workspace, allowing the '.Layers' or '.Connections' properties be stored in the default temporary variable 'ans' and then setting the desired variables with "layers = ans" or "connections = ans".
The second problem I am encountering is with the 'freezeWeights()' command. When I try to run that I get the error message, "Undefined function or variable 'freezeWeights'" which is strange considering I have downloaded the googleNet support package. Is there another support package that has the freezeWeights command? I can't seem to find anything on that command.
Thank you for any assistance.

채택된 답변

Oleksii Sidorov
Oleksii Sidorov 2018년 4월 7일
Dear Andrew,
the first version of MatLab where function freezeWeights() was realized is 2018a. I'm quite sure that you face problems because you are using older version of MatLab.
  댓글 수: 1
Andrew Chen
Andrew Chen 2018년 4월 10일
This is in fact the case. I was using 2017b. There is a transfer learning example there with the googlenet but it does not use the freeze weights function.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

pefage pefage
pefage pefage 2018년 4월 10일
Got the same issue on MATLAB 2018a
  댓글 수: 2
pefage pefage
pefage pefage 2018년 4월 10일
Not sure but I've written the code for freezeWeights() myself:
for i = 1:numel(layers)
if isprop(layers(i),'WeightLearnRateFactor')
layers(i).WeightLearnRateFactor = 0;
end
if isprop(layers(i),'WeightL2Factor')
layers(i).WeightL2Factor = 0;
end
if isprop(layers(i),'BiasLearnRateFactor')
layers(i).BiasLearnRateFactor = 0;
end
if isprop(layers(i),'BiasL2Factor')
layers(i).BiasL2Factor = 0;
end
end
Runcong Kuang
Runcong Kuang 2022년 8월 17일
I can see the "freezeWeights.m" file integrated in matlab now:
```
% layers = freezeWeights(layers) sets the learning rates of all the
% parameters of the layers in the layer array |layers| to zero.
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
```
So there is no need to make '2Factor' 0.

댓글을 달려면 로그인하십시오.


Michael Markzon
Michael Markzon 2018년 5월 14일
I am using 2018a and got the error
  댓글 수: 1
Christopher Bokros
Christopher Bokros 2018년 6월 22일
Same here, but since the code is as easy to write as @pefage pefage demonstrated it's not a big deal.

댓글을 달려면 로그인하십시오.


Aqib Mumtaz
Aqib Mumtaz 2018년 9월 13일
I am using 2018a on windows10.
I solved freezeWeights issue as @pefage pefage demonstrated
but now I am getting undefined error on createLgraphUsingConnections(layers,connections)
Undefined function or variable 'createLgraphUsingConnections'.
How to proceed now?
  댓글 수: 2
Deepali Mishra
Deepali Mishra 2019년 12월 7일
Don't use 'createLgraphUsingConnections' and train your model after using freezeWeights , that issue won't come again.
Runcong Kuang
Runcong Kuang 2022년 8월 17일
Hi, I am curious because I am only using my own defined layers as neural network.
If I directly use layers (layer array), do I need to do 'createLgraphUsingConnections'?
It's only for layergraph object, right?
I think the example here is fancy in using lgraph for replacement and connection.
If you have some insight, please tell me.
Thanks.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by