MATLAB Answers

0

How to add a layer to a neural network while keeping the weights and biases of a trained layer constant?

Asked by Ekaterina Kryuchkova on 9 Apr 2019
Latest activity Answered by Shounak Mitra on 12 Apr 2019
I would like to first train a single layer neural network, then make another neural network that has the same weights and biases as the trained one, but also has one more layer with weights that are all ones. I am getting errors like "net.LW{2,1} must be a 2-by-2 matrix.". How can I fix the code below and still do what I would like?
Alternatively, how can I add one more layer to the neural network without changing the weights on the existing layer/s or the performance?
images = loadMNISTImages('train-images.idx3-ubyte'); % initialize figure
labels = loadMNISTLabels('train-labels.idx1-ubyte'); % initialize figure
labels = labels'; % transpose
labels(labels==0)=10; % dummyvar function doesn´t take zeroes
labels=dummyvar(labels)';
x=images;
t=labels;
[~,n]=size(x);
net = patternnet(2);
net = configure(net,x,t);
net.trainParam.epochs = 10;
net = train(net,x,t);
y=net(x);
perf1 = perform(net,t,y)
net2 = patternnet([2,2]);
net2= configure (net2, x,t);
net2.IW{1,1}=net.IW{1,1};
net2.LW{3,2}=ones(size(net2.LW{3,2}));
net2.LW{2,1}=net.LW{2,1};
net2.b{1,1}=net.b{1,1};
net2.b{3,1}=ones(size(net2.b{3,1}));
net2.b{2,1}=net.b{2,1};

  0 Comments

Sign in to comment.

1 Answer

Answer by Shounak Mitra on 12 Apr 2019

Hi,
Re: Alternatively, how can I add one more layer to the neural network without changing the weights on the existing layer/s or the performance?
What you're asking is kind of similar to the basis of transfer learning where we take a network, replace the last few layers (or edit some other layers) with the ones keeping the output as per your requirement. In your case, there's a shape mismatch where net.LW{2,1} should be a 2x2 matrix that needs to be consistent with the input dimension of [2,2].
--
Shounak

  0 Comments

Sign in to comment.