|TensorFlow 2.0 version||View source on GitHub|
Batchwise dot product.
tf.keras.backend.batch_dot( x, y, axes=None )
batch_dot is used to compute dot product of
y are data in batch, i.e. in a shape of
batch_dot results in a tensor or variable with less dimensions
than the input. If the number of dimensions is reduced to 1,
expand_dims to make sure that ndim is at least 2.
x: Keras tensor or variable with
ndim >= 2.
y: Keras tensor or variable with
ndim >= 2.
axes: list of (or single) int with target dimensions. The lengths of
axesshould be the same.
A tensor with shape equal to the concatenation of
(less the dimension that was summed over) and
(less the batch dimension and the dimension that was summed over).
If the final rank is 1, we reshape it to
x = [[1, 2], [3, 4]] and
y = [[5, 6], [7, 8]]
batch_dot(x, y, axes=1) = [[17, 53]] which is the main diagonal
x.dot(y.T), although we never have to calculate the off-diagonal
x's shape be
(100, 20) and
y's shape be
(100, 30, 20).
axes is (1, 2), to find the output shape of resultant tensor,
loop through each dimension in
x's shape and
x.shape: 100 : append to output shape
x.shape: 20 : do not append to output shape, dimension 1 of
xhas been summed over. (
y.shape: 100 : do not append to output shape, always ignore first dimension of
y.shape: 30 : append to output shape
y.shape: 20 : do not append to output shape, dimension 2 of
yhas been summed over. (
>>> x_batch = K.ones(shape=(32, 20, 1)) >>> y_batch = K.ones(shape=(32, 30, 20)) >>> xy_batch_dot = K.batch_dot(x_batch, y_batch, axes=[1, 2]) >>> K.int_shape(xy_batch_dot) (32, 1, 30)