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$ NetMapThreadOperator sounds like what you are looking for...perhaps with a CatenateLayer to turn your two separate input ports into a single pair input and a PartLayer to split them up on output. $\endgroup$ Jan 25, 2022 at 18:45

1 Answer 1


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 *)

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.