# Extracting array product from neural network

I'm training a neural network which is meant to model a collection of functions that map $$N$$-dimensional vectors to scalars. At this point, I have built up several layers which manipulate the input vectors into a desired form; call this vector $$\vec{a}$$. In the final step, I want the neural network to output the product of each of these elements, i.e.,

$$\prod_{j=1}^{N}a_{j}$$

Is there a single layer which can implement this step? Essentially, what I need is a multiplicative analog of SummationLayer[].

EDIT: It's been suggested that I try using ThreadingLayer[Times] to extract the product. I've tried putting this as the final layer in NetChain[] and gotten the following output:

NetChain::indmultiport: The third layer has an indeterminate number of input ports. Please ensure it is connected to at least one other layer or manually specify its input ports using "Inputs" -> {...}.

Specifying the inputs seems to control the number of input arrays upon which elementwise multiplication is performed. I'm not sure how to deal with this error.

• Just to be clear, do you want a multiplicative analog of TotalLayer (element-wise summation for a list of arrays), or of SummationLayer (= overall total of all elements in input)? – MarcoB Jan 31 at 1:45
• SummationLayer - thanks for catching that, I will correct the post. – miggle Jan 31 at 1:46

times = ThreadingLayer[Times];
times[Range[5]]

Out[1]= 120.


Does this work for you?

EDIT: Based on the clarification re: SummationLayer vs TotalLayer, the documentation for SummationLayer again provides us with a hint:

SummationLayer[] is equivalent to AggregationLayer[Total,All]:

as such, we can use this instead:

times = AggregationLayer[Times, All]
times[Range[5]]

Out[1] = 120.

• I tried this but encountered an error when incorporating that into NetChain - I'll add more details in an edit. – miggle Jan 31 at 1:40
• It should be noted that under "Properties & relations" there is an example stating that TotalLayer is equivalent to ThreadingLayer[Plus], so this is indeed the analogue for multiplication as requested in the original question. – C. E. Jan 31 at 1:45