Message Boards Message Boards

1
|
3273 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

How to add an extra output port to an existing NetGraph ?

Posted 3 years ago

I am using a pre-trained neural network which I downloaded from an ONNX repository. I would like to study the output of a given internal layer in the network.

Here is a simple dummy example of what I would like to achieve. Lets create a simple network and save it in ONNX format

net = NetInitialize@NetGraph[{LinearLayer[2], Ramp, LinearLayer[3], Tanh,CatenateLayer[]}, {NetPort["Input1"] -> 1 -> 2, NetPort["Input2"] -> 3 -> 4, {2, 4} -> 5}, "Input1" -> 6, "Input2" -> 7];
    Export["onnx_model.onnx", net];

Now I load the network just as if I downloaded it from some repository

ONNXnet = Import["onnx_model.onnx"]

The imported network from the ONNX file looks like this enter image description here

I would now like to add an additional output to layer 2 to which I can connect a new network output port :

enter image description here

The only way I have found to do this is by taking the imported network apart and then rebuild it with the extra link to the output port

layerlist=Normal[ONNXnet]

enter image description here

NetGraph[layerlist,{NetPort["Input1"]->1->3,NetPort["Input2"]->2->4,{3,4}->5,3->NetPort["Extra_port"]}]

enter image description here

In case of a simple network like the above example or linear networks, this procedure is not too difficult. But when the original imported network has many layers and complicated links, rebuilding it by hand becomes tedious and is error prone.

I have seen examples of new netgraphs being build on top of existing netgraphs but I don't know how to access a given layer of the existing netgraph being used. I only know how to connect something to the input and output ports of the existing NetGraph, not the internal layers. Something like the following does not work :

NetGraph[<|"net"->ONNXnet|>,{NetPort["net",2]->NetPort["Extra_Port"]}]

Using NetReplacePart to replace layer 2 with an identical layer that has 2 outputs also does not work (this was a suggestion from mathematica.stackexchange)

rep=Netgraph[{ramp},{1->NetPort["Output"],1->NetPort["Extra_port"]}];NetReplacePart[ONNXnet,3->rep]

NetReplacePart::invnetrep: Replacement of net failed: replacement net had extra output port named "foo".

So my question is if it is at all possible to connect a new output port to an existing NetGraph and how to do this.

4 Replies
Posted 3 years ago

Crossposted here.

POSTED BY: Rohit Namjoshi

But with no solution, I thought I might see if the Wolfram community might have an answer

Welcome to Wolfram Community!
Please make sure you know the rules: https://wolfr.am/READ-1ST
Please next time link your post to the duplicated one from MSE site.

POSTED BY: Moderation Team

ok

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract