5
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ 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)? $\endgroup$ – MarcoB Jan 31 at 1:45
  • $\begingroup$ SummationLayer - thanks for catching that, I will correct the post. $\endgroup$ – miggle Jan 31 at 1:46
6
$\begingroup$
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.
$\endgroup$
2
  • $\begingroup$ I tried this but encountered an error when incorporating that into NetChain - I'll add more details in an edit. $\endgroup$ – miggle Jan 31 at 1:40
  • 3
    $\begingroup$ 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. $\endgroup$ – C. E. Jan 31 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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