﻿# Matrix multiplication and elementwise products inside neural layers

A worked comparison of matrix multiplication and elementwise multiplication in neural networks, explaining contractions, learned projections and gating.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/neural-matmul-elementwise-products/
Language: en

The multiplication symbol hides different computations in neural-network diagrams. A projection combines features through a learned matrix, while a gate can multiply corresponding coordinates. Distinguishing these operations explains both the model’s mathematics and the shapes expected by its implementation.

## A projection contracts a feature dimension

For a linear layer, each output coordinate receives a weighted sum of input coordinates. The shared feature axis is contracted: its separate entries contribute to a sum rather than remain visible as an output dimension. The weight matrix determines which mixtures are learned. Batch and sequence axes can remain untouched around this operation. Thinking in terms of the contracted axis makes a shape expression meaningful: the layer learns a feature transformation at each position instead of simply scaling the entire input tensor.

## A gate acts on aligned coordinates

Elementwise multiplication combines corresponding entries after any documented broadcasting. A learned gate can reduce, amplify or reverse selected coordinates depending on its range and activation function. It does not inherently mix information between coordinates. If a later sum combines those products, that sum is a separate operation with its own axis. This distinction helps explain gated feed-forward layers: learned projections construct candidate features and gate values, then coordinate-wise multiplication modulates them before another projection combines the resulting features.

## Use distinguishable values in a small calculation

Consider input coordinates [2, 3] and weights [4, 5] for one output. Their dot product is 23 because the products are added. Elementwise multiplication gives [8, 15], which is a vector until an additional reduction occurs. These are illustrative values. The difference survives even when surrounding dimensions make the outputs look superficially similar. A tiny handwritten example therefore catches an accidental multiplication operator more directly than observing that a large tensor passed through the model without an exception.

## Check the operation together with its broadcasting rules

A matrix operation may accept leading batch dimensions, while an elementwise operation may broadcast singleton axes. Both conveniences can conceal a semantic mistake if a dimension happens to match. Write down the expected output coordinates and the input coordinates that contribute to each one. Then compare the implementation against that dependency map. Gradients follow the actual operation performed, so autograd can faithfully differentiate the wrong program. Correct differentiation is useful only after the intended algebra, axis alignment and reduction have been established.

## Primary research and implementation references

- [PyTorch: Broadcasting Semantics](https://docs.pytorch.org/docs/2.15/notes/broadcasting.html).

- [PyTorch: Tensor Views](https://docs.pytorch.org/docs/2.15/tensor_view.html).

- [PyTorch: Matrix Multiplication](https://docs.pytorch.org/docs/2.15/generated/torch.matmul.html).

## Related engineering context

[Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/); [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/).
