dlmtimes

Batch matrix multiplication for deep learning

Description

example

dlC = dlmtimes(dlA,dlB) computes matrix multiplication for each page of dlA and dlB. For 3-D inputs dlA and dlB, dlC is calculated as

dlC(:,:,i) = dlA(:,:,i) * dlB(:,:,i)
Similarly, for n-dimensional inputs dlA and dlB, dlC is calculated as
dlC(:,:,i1,...,in) = dlA(:,:,i1,...,in) * dlB(:,:,i1,...,in)
If one of dlA or dlB is a two-dimensional matrix, this matrix multiplies each page of the other input.

Examples

collapse all

Create two 4-D arrays.

A = rand(3,4,8,2);
B = rand(4,5,8,2);

dlA = dlarray(A);
dlB = dlarray(B);

Calculate the batch matrix multiplication of dlA and dlB.

dlC = dlmtimes(dlA,dlB);
size(dlC)
ans = 1×4    
     3     5     8     2

If one of the inputs is a 2-D matrix, the function uses scalar expansion to expand this matrix to the same size as the other input in the third and higher dimensions. The function then performs batch matrix multiplication to the expanded matrix and the input array.

Create a random array of size 15-by-20-by-3-by-128. Convert to dlarray.

A = rand(15,20,3,128);
dlA = dlarray(A);

Create a random matrix of size 20-by-15.

B = rand(20,15);

Multiply dlA and B using dlmtimes.

dlC = dlmtimes(dlA,B);
size(dlC)
ans = 1×4    
    15    15     3   128

Input Arguments

collapse all

Operands, specified as scalars, vectors, matrices, or N-D arrays. At least one of dlA or dlB must be a dlarray. The inputs dlA or dlB must not be formatted unless one of dlA or dlB is an unformatted scalar.

The number of columns of dlA must match the number of rows of dlB. If one of dlA or dlB is a two-dimensional matrix, this matrix multiplies each page of the other input. Otherwise, the size of dlA and dlB for each dimension greater than two must match.

Output Arguments

collapse all

Product, returned as a scalar, vector, matrix, or an N-D array.

Array dlC has the same number of rows as input dlA and the same number of columns as input dlB, unless one of dlA or dlB is a scalar. The size of the other dimensions of dlC match the size of the dimensions greater than two of both dlA and dlB. If dlA or dlB is a matrix, the size of the other dimensions matches the size of the other (non-matrix) input. If one of dlA or dlB is a scalar, dlC has the same size as the non-scalar input.

Introduced in R2020a