how to combine two different lgraph and create a new network

29 views (last 30 days)
Hi All
I would like to combine two trained networks. in order to do that I convert each trained network to lgraph, but I don't find how to combine the 2 lgraph.
I use the following code to create the lgraph:
lgraph1 = layerGraph(net1);
lgraph2 = layerGraph(net2);
I tried connectLayers, and replaceLayer, but those function working when need to connect or replace layer in lgraph with other layer (not lgraph to lgraph).
do you have any suggestion how to overcome this issue, or any suggestion how combine 2 tarined networks when net1 is input to net2.
Basically I would like to create a new network unified network which is the combination of the 2 trained network, and use the predict function on the new unified network.
Thanks alot

Accepted Answer

Tomaso Cetto
Tomaso Cetto on 2 Jun 2021
Hey Avi!
I think the addLayers function is what you're after here.
You're right in saying that these functions can't work with two layer graphs - however, what you can do is access the underlying layers of a layerGraph object, and use those when stitching up your two networks. Hopefully the code below will explain what I mean:
% Define both layer graphs
net1 = layerGraph(layers1)
net2 = layerGraph(layers2);
% Add the layers from net2 to net1
net1 = addLayers(net1, net2.Layers);
% Connect the last layer of net1 with the first layer of net2
net1 = connectLayers(net1, 'lastLayerOfNet1', 'firstLayerOfNet2');
% Call assembleNetwork to ensure your new network is ready for prediction
% and does not have any issues
net1 = assembleNetwork(net1);
Let me know if this works for you!
Tomaso
  1 Comment
Avi Sulimarski
Avi Sulimarski on 2 Jun 2021
Hi Tomaso
your answer was very helpful. I tried its and it working perfectly.
Thanks and best Regards
Avi

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!