Batch matrix multiplication for deep learning
dlC(:,:,i) = dlA(:,:,i) * dlB(:,:,i)
dlCis calculated as
dlC(:,:,i1,...,in) = dlA(:,:,i1,...,in) * dlB(:,:,i1,...,in)
dlBis a two-dimensional matrix, this matrix multiplies each page of the other input.
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
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
A = rand(15,20,3,128); dlA = dlarray(A);
Create a random matrix of size 20-by-15.
B = rand(20,15);
dlC = dlmtimes(dlA,B); size(dlC)
ans = 1×4 15 15 3 128
Operands, specified as scalars, vectors, matrices, or N-D arrays. At least one of
dlB must be a
dlB must not be formatted unless
dlB is an unformatted scalar.
The number of columns of
dlA must match the number of rows of
dlB. If one of
a two-dimensional matrix, this matrix multiplies each page of the other input.
Otherwise, the size of
dlB for each
dimension greater than two must match.
Product, returned as a scalar, vector, matrix, or an N-D array.
dlC has the same number of rows as input
dlA and the same number of columns as input
unless one of
dlB is a scalar. The size of
the other dimensions of
dlC match the size of the dimensions greater
than two of both
dlB is a matrix, the size of the other
dimensions matches the size of the other (non-matrix) input. If one of
dlB is a scalar,
has the same size as the non-scalar input.