4
$\begingroup$

I'd like to use NetNestOperator on a NetGraph that has custom input / output port names, such as

net = NetGraph[<|"y" -> LinearLayer[1]|>, {NetPort["x"] -> "y" -> NetPort["x"]}, "x" -> {1}]

This should be doable, since there's a one-to-one mapping between the input and output port names (in this case, they are identical), but Mathematica complains when I try for example NetNestOperator[net,2]:

NetGraph::netinvgport: Input is neither a valid input or output port for the given NetGraph.

Of course, for this simple example using

net = NetGraph[<|"y" -> LinearLayer[1]|>, {NetPort["Input"] -> "y" -> NetPort["Output"]}, "Input" -> {1}]

works just fine, but

  1. There's no way to generalize this to multiple inputs and outputs (i.e. using "Input1", "Input2", ... and "Output1", "Output2", ... doesn't work)
  2. Custom port names are nice!

Is there maybe an undocumented option specifying the port mappings in NetNestOperator?

$\endgroup$

1 Answer 1

4
$\begingroup$

For the ports name problem, I'm not aware of any ready-to-use way, but I think it is always doable to re-map the ports to "standard" names through a dummy net. For example

NetGraph[{net}, {NetPort["Input"] -> 1 -> NetPort["Output"]}] // NetNestOperator[#, 2] &

For the multi-ports problem, according to the doc, I don't think NetNestOperator is meant to handle that. But it's easy to make one with WL's functional programming capability. For example using FoldPairList:

mutiportNest[net_NetGraph, n_Integer] /; n >= 1 :=
 NetGraph[
  ConstantArray[net, n]
  , Flatten@FoldPairList[{
      Thread[Rule @@ Outer[NetPort, {##}, Information[net, "InputPortNames"]]]
      , #2} &, Range@n]
  ]
NetGraph[
  {CatenateLayer[0], LinearLayer[2], PartLayer[1], PartLayer[2]}
  , {NetPort/@{"a","b"}->1->2->{3,4}, Thread[{3,4}->NetPort/@{"a","b"}]}//Flatten
  , "a"->"Real", "b"->"Real"
  ];
  
mutiportNest[%, 3]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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