3
$\begingroup$

I have a network with two input ports ("a" and "b"). I want to use this net within a bigger network. The problem is that I cannot find a syntax to specify to which input port the previous layers of the outer network should connect.

To do an example. My internal net can be defined as

net1 = NetGraph[
 {
   "2x" -> ElementwiseLayer[(2*#) &],
   "sum" -> ThreadingLayer[(#1 + #2) &]
   },
  {
   NetPort["a"] -> "2x" -> "sum",
   NetPort["b"] -> "sum"
   }
  ]

I can easily pass the inputs to this net doing

net1[<|"a" -> {1, 2, 3}, "b" -> {10, 20, 30}|>]

or

net1[<|"b" -> {10, 20, 30},"a" -> {1, 2, 3}|>]

both returns {12., 24., 36.}

I want now to place this net within another network. For example

net2 = NetGraph[
  {
   "A" -> PartLayer[1],
   "B" -> PartLayer[2],
   "net1" -> net1
   },
  {
   NetPort["Input"] -> "A",
   NetPort["Input"] -> "B",
   {"A","B"} -> "net1"
   }
  ]
net2[{{1, 2, 3}, {10, 20, 30}}]

This would work, but it's not very robust, as the only thing telling net1 which input send to port "a" and "b" is just the order in which the input are passed.

I tried to do something like

{<|"b" -> "b", "a" -> "a"|>} -> "net1"

but this syntax is not accepted by NetGraph. Is there any better syntax to achieve this?

$\endgroup$
3
$\begingroup$

Found the answer to be

net2 = NetGraph[
  {
   "a" -> PartLayer[1],
   "b" -> PartLayer[2],
   "net1" -> net1
   },
  {
   NetPort["Input"] -> "a",
   NetPort["Input"] -> "b",
   "b" -> NetPort["net1", "b"],
   "a" -> NetPort["net1", "a"]
   }
  ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.