# How can I prevent errors with ReplicateLayer[]?

I am trying to build a NetGraph[] that takes input of the form {{0.6},{0.6},...,{0.8},{0.8}} and multiplies the last sequence value {0.8} with all of the elements in the sequence.

I have defined the following network:

net = NetGraph[
<|
"last" ->
SequenceLastLayer["Input" -> {"Varying", 1}, "Output" -> 1],
"replicate" -> ReplicateLayer[Automatic],
"sub" -> ThreadingLayer[#1 - #2 &]
|>
,
{NetPort["input"] -> {"last", "sub"},
"last" -> "replicate" -> "sub"}]


but when I apply the network, as in the two following examples:

net[{1, 2, 3}]


and

net[{{1}, {2}, {3}}]


I continually get the following error:

NetGraph::interr2: An unknown internal error occurred. Consult Internal$LastInternalFailure for potential information.  And, using$LastInternalFailure, I get:

InvalidSowNode
At position NeuralNetworksNetPath[Nodes,replicate] in net, in layer of type Replicate: cannot serialize {NeuralNetworksLengthVar[1016995628]} in SowNode[broadcast_axis, {{5,0,0}}, {axis->{1},size->{NeuralNetworksLengthVar[1016995628]}}] for MXNetFunction of layer "replicate".
<<43 raw stack frames>>


I have spent some time on this and, even though it seems like it should be pretty simple, have not been able to get it to work.

I am assuming that this may have something to do with using Automatic in ReplicateLayer[], but this seems to be the only option, as the input sequence is of varying length.

Is this an error on Mathematica's part or is there something I am doing incorrectly? The fact that there aren't any explanations forthcoming for this question makes me think this must be a bug.

• Sorry that my solution from the previous question didn't work with variable size input. I tried, but couldn't make it work either. While waiting for someone more knowledgeable (or MMA 12.1), I can only suggest padding the input to make it constant size. Also, your code does subtraction, but you speak of multiplication - this might be confusing for other users. – aooiiii Jan 16 at 21:20
• Thanks, I had forgotten to change the subtraction part. And your answer worked for what I asked, I just think there may be some compatibility issue between ReplicateLayer[] and specifying variable input. I hope there's a workaround for within NetGraph[]. And I hadn't thought of using padding - I will have a look at that to see if that can work. I also could just better prepare the data outside of the NetGraph[], but I would prefer to be able to have it all within NetGraph[] for training purposes. – Jmeeks29ig Jan 16 at 21:33