Help protect the Great Barrier Reef with TensorFlow on Kaggle

tf.keras.backend.batch_dot

Batchwise dot product.

batch_dot is used to compute dot product of x and y when x and y are data in batch, i.e. in a shape of (batch_size, :). batch_dot results in a tensor or variable with less dimensions than the input. If the number of dimensions is reduced to 1, we use 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 Tuple or list of integers with target dimensions, or single integer. The sizes of x.shape[axes] and y.shape[axes] should be equal.

A tensor with shape equal to the concatenation of x's shape (less the dimension that was summed over) and y's shape (less the batch dimension and the dimension that was summed over). If the final rank is 1, we reshape it to (batch_size, 1).

Examples:

Assume x = [[1, 2], [3, 4]] and y = [[5, 6], [7, 8]] batch_dot(x, y, axes=1) = [, ] which is the main diagonal of x.dot(y.T), although we never have to calculate the off-diagonal elements.

• Pseudocode: inner_products = [] for xi, yi in zip(x, y): inner_products.append(xi.dot(yi)) result = stack(inner_products)

Shape inference: Let x's shape be (100, 20) and y's shape be (100, 30, 20). If axes is (1, 2), to find the output shape of resultant tensor, loop through each dimension in x's shape and y's shape:

• x.shape : 100 : append to output shape
• x.shape : 20 : do not append to output shape, dimension 1 of x has been summed over. (dot_axes = 1)
• y.shape : 100 : do not append to output shape, always ignore first dimension of y
• y.shape : 30 : append to output shape
• y.shape : 20 : do not append to output shape, dimension 2 of y has been summed over. (dot_axes = 2) output_shape = (100, 30)
x_batch = tf.keras.backend.ones(shape=(32, 20, 1))
y_batch = tf.keras.backend.ones(shape=(32, 30, 20))
xy_batch_dot = tf.keras.backend.batch_dot(x_batch, y_batch, axes=(1, 2))
tf.keras.backend.int_shape(xy_batch_dot)
(32, 1, 30)
[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }]
[{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]