Weight Tying for Layers in a CNN model
Show older comments
I am building a Deep Learning network looks like the figure below. I want to tie the weights of : Conv1 to Conv2; Conv3 to Conv6 and Conv4 to Conv5. Is it possible to do so in MATLAB? If yes, please help me with the code.

Answers (2)
Sanjana
on 17 Jun 2024
1 vote
Hi Junaid,
Weight Tying is supported in MATLAB using Nested Layer. I have observed in your code for the "WeightTyingEncoderLayer1.m", that you have not declared and defined the "intialize" function, which is necessary initializing the learnables and parameters of the layers whose weights need to be tied, based on the "numChannels" of the layers chosen for this purpose.
The Error using "convolution2dLayer", is due to the reason that "this.Conv1" is not initialized.
To resolve the errors and implement Weight Tying Using Nested Layer, refer to the following example,
Also, refer to the following link for more information on creating custom "Nested Layer", and specifically refer to the "Custom Layer Template" section to understand how to create a custom nested layer in MATLAB.
1 Comment
Junaid Farooq
on 17 Jun 2024
Samuel Somuyiwa
on 17 Jun 2024
1 vote
See attached weightTyingAutoEncoder layer example. The layer follows on from the example in the link that Sanjana shared earlier.
See an example of how to use the layer below. Note that I couldn't understand what dlhdl.layer.sliceLayer in your example was meant to do or whether it was used correctly in that example. So, I have not included it in the example below
inputSize = [32 32 32];
layers = [
imageInputLayer(inputSize,Normalization="none")
weightTyingAutoEncoderLayer
];
net = dlnetwork(layers);
%%
X = dlarray(rand(inputSize),'SSCB');
Y = predict(net, X);
2 Comments
Junaid Farooq
on 17 Jun 2024
Samuel Somuyiwa
on 18 Jun 2024
Yes, the convolution layers get the input from the input layer.
No, adding RELU layer between the convolution layers will not change the values of the code.
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!