I hope to wrap up ConvolutionLayer
in a way so that it takes two inputs, does the same operation on those, and outputs their result respectively. For example, MyConv[a,b]
should have two output port for a and b. It is easy to use in pytorch - by simply calling that layer twice, but I can't see how to do it in mathematica. Any help is welcome
$\begingroup$
$\endgroup$
1
1 Answer
$\begingroup$
$\endgroup$
A neat way to use layers with the same parameters in multiple places is with shared arrays (via NetInsertSharedArrays
). These are parameters that are shared among multiple instances of the same layer. E.g.
conv = NetInsertSharedArrays@ConvolutionLayer[10, 3];
net = NetInitialize@NetGraph[
{conv, conv},
{NetPort["a"] -> 1, NetPort["b"] -> 2},
"a" -> {5, 10, 10}, "b" -> {5, 20, 20}];
Now the two instances of conv
are not independent as they would normally be and if you train the net and extract the convolution weights you can see they are identical
net2 = NetTrain[net, "RandomData", MaxTrainingRounds -> 10];
NetExtract[net2, {1, "Weights"}] == NetExtract[net2, {2, "Weights"}]
(* True *)
NetMapThreadOperator
sounds like what you are looking for...perhaps with aCatenateLayer
to turn your two separate input ports into a single pair input and aPartLayer
to split them up on output. $\endgroup$