Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

layers.Dot

keras.layers.Dot computes a dot product between two input tensors over selected axes. It can optionally normalize the inputs first, producing cosine proximity.

How it works

Dot accepts exactly two tensors and contracts the axes specified by axes. With normalize=True, each sample is L2-normalized along the contraction axis first, so the result represents cosine proximity rather than an unnormalized dot product.

Syntax

keras.layers.Dot(axes, normalize=False, **kwargs)

Parameters

NameTypeDescription
axesInteger or tuple of integersAxis or axes over which to compute the dot product. An integer applies the same axis convention; a tuple supplies one axis for each input. Selected axis sizes must match, and axis 0 is not allowed.
normalizeBoolean, default falseWhether to L2-normalize samples along the dot-product axis before computing the result.
**kwargsKeyword argumentsStandard Keras layer keyword arguments.

Returns

A tensor containing the dot product of the samples from the inputs.

Key points

  • Dot performs an element-wise dot product between two tensors.
  • The call must receive a two-item input list.
  • axes may be one integer or a two-integer tuple.
  • For a tuple, its first value addresses the first input and its second addresses the second.
  • normalize optionally applies L2 normalization before the contraction.
  • With normalization enabled, the output expresses cosine proximity.

Common mistakes

  • Do not include the batch axis in axes.
  • The contracted dimensions must have equal sizes.

Example: Dot product across selected axes

import numpy as np
import keras

x = np.arange(10).reshape(1, 5, 2)
y = np.arange(10, 20).reshape(1, 2, 5)
result = keras.layers.Dot(axes=(1, 2))([x, y])
print(result.shape)  # (1, 1)

This computes the dot product by contracting axis 1 of the first input with axis 2 of the second.

When to use it

Vector similarity

Compare two learned vector representations by contracting their feature axis.

Two-branch feature interaction

Combine two branches when their matching dimensions should be reduced through multiplication and summation.

Cosine proximity

Measure directional similarity while ignoring vector magnitude.

Interview questions

QHow does layers.Dot determine where to contract the inputs?

It takes two inputs and computes their dot product over the axis or pair of axes supplied through axes. A tuple specifies one axis from each input, in input order.

Follow-up: What restriction applies to those axes? The selected dimensions must match, and axis 0 cannot be selected because it is the batch axis.

QWhat does normalize=True do?

normalize controls whether both samples are L2-normalized before contraction. When enabled, the output is cosine proximity rather than the ordinary dot product.

Follow-up: Why can normalization change the result? It removes vector-magnitude effects, leaving similarity determined by direction.

QWhat input and output structure does Dot use?

The layer expects a list containing two input tensors. It returns a tensor containing the dot product of corresponding samples over the requested axes.

Follow-up: How would you use it in a model? Apply it to two branches, such as two Dense outputs, and pass the pair as a list.

  • keras.layers.Add
  • keras.layers.Average
  • keras.layers.Concatenate
  • keras.layers.Multiply

Source: Dot layer - Keras. Checked 2026-10-15.