Vector similarity
Compare two learned vector representations by contracting their feature axis.
Keras · Class
keras.layers.Dot computes a dot product between two input tensors over selected axes. It can optionally normalize the inputs first, producing cosine proximity.
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.
keras.layers.Dot(axes, normalize=False, **kwargs)| Name | Type | Description |
|---|---|---|
axes | Integer or tuple of integers | Axis 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. |
normalize | Boolean, default false | Whether to L2-normalize samples along the dot-product axis before computing the result. |
**kwargs | Keyword arguments | Standard Keras layer keyword arguments. |
A tensor containing the dot product of the samples from the inputs.
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.
Compare two learned vector representations by contracting their feature axis.
Combine two branches when their matching dimensions should be reduced through multiplication and summation.
Measure directional similarity while ignoring vector magnitude.
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.
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.
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.Addkeras.layers.Averagekeras.layers.Concatenatekeras.layers.Multiply