필터 지우기
필터 지우기

How to manually modify weights in a SeriesNetwork?

조회 수: 32 (최근 30일)
Carlo Tomasi
Carlo Tomasi 2017년 7월 6일
편집: Andrea Tagliabue 2022년 7월 26일
For some of my studies on deep neural nets, I need to change select weights in a (previously trained) SeriesNetwork by hand, and evaluate changes in the classification results. Ideally, I would like to do
net.Layers(k).Weights = W;
where net is a SeriesNetwork (a class in the Neural Network Toolbox), k is an integer that indexes a fully connected layer, and W is a suitable array. I would then classify inputs with the net modified in this way.
However, the field net.Layers(k).Weights is read-only, so the instruction above will generate the following error message:
You cannot set the read-only property 'Layers' of SeriesNetwork.
Is there some way to circumvent this restriction?
Here is what I tried, to no avail: First define an array of Layers by saying something like this:
layers = [imageInputLayer([28 28 1])
% Severa layers here
classificationLayer()];
then initialize the weights as desired, and finally make these layers into a SeriesNetwork with the class’s constructor:
net = SeriesNetwork(layers);
While this does create a new SeriesNetwork, attempting to classify an input with
y = classify(net, x);
where x is a suitable input results in the following error message:
Error using nnet.internal.cnn.layer.FullyConnected/forwardPropagateSize (line 99)
An input size for the layer must be defined in order to call forwardPropagateSize.
Error in SeriesNetwork>iDetermineLayerOutputSize (line 417)
inputSize = layers{i}.forwardPropagateSize(inputSize);
Error in SeriesNetwork/get.OutputSize (line 80)
val = iDetermineLayerOutputSize( internalLayers, outputLayerIdx );
Error in SeriesNetwork/predict (line 185)
Y = precision.cast( zeros([this.OutputSize dispatcher.NumObservations]) );
Error in SeriesNetwork/classify (line 250)
scores = this.predict( X, varargin{:} );
What is missing in my use of the constructor to make net a fully-fledged SeriesNetwork that can be used with classify?

채택된 답변

Carlo Tomasi
Carlo Tomasi 2018년 2월 21일
See this Q/A .

추가 답변 (2개)

DL
DL 2021년 3월 29일
You can covert the trained NN to an object and it is a possible way, which I had test for it.
modify_able_NN = NN.saveobj;
% Changes made in NN
Modified_NN = NN.loadobj(modify_able_NN);
  댓글 수: 2
qingqing
qingqing 2021년 6월 30일
Thanks for your comment.It helps a lot!
Andrea Tagliabue
Andrea Tagliabue 2022년 7월 26일
편집: Andrea Tagliabue 2022년 7월 26일
The above code does not work for me. Trying to access the saveobj method in a shallow neural network returns the following errors: "Switch expression must be a scalar or a character vector". Any suggestion?
Code to reproduce:
myNN = feedforwardnet(10);
modifiableNN= myNN.saveobj
SWITCH expression must be a scalar or a character vector.

Error in indexing (line 173)
switch (subs)

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


Shashank
Shashank 2017년 7월 14일
편집: Shashank 2017년 7월 14일
Hi Carlo,
Please see the following image to understand how the weights of various layers are stored in MATLAB.
It is stored as a cell array of matrices and each matrix corresponds to a weight matrix of that layer. It is like a map from 1st to 2nd layer , 2nd to 3rd and so on. Hence only the corresponding indices have weight vectors.
The 1st layer is called the input layer and weights can be set by :
>>net.IW{1} = [1;2;3;4;5;6;7;8;9;10;11;10];
However, please train the network using the following function before modifying the weight vectors:
>> net = trainscg(net,x,t); % where x and t are the input and output variables respectively.
For the succeeding layers, you can modify the weights using:
>> net.LW{2,1}=rand(128,12);
as shown in the screenshot.Please do not modify the empty vectors
Hope this helps.
- Shashank
  댓글 수: 1
Carlo Tomasi
Carlo Tomasi 2017년 7월 14일
Thank you, Shashank. However, your solution works for what Matlab calls ``shallow networks.'' In other words, the following needs to be true:
all(class(net) == 'network')
My question was about series networks, for which the following is true:
all(class(net) == 'SeriesNetwork')
Unfortunately, members of class SeriesNetwork are not structured as you say (they have no .IW or .LW fields, for instance).

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

카테고리

Help CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by