主要内容

TensorProd

两个张量之间的s manbetx 845张量

描述

example

C = tensorprod (A,B,dimA,dimB)returns the张量产品张量AB。论点dimAdimBare vectors that specify which dimensions to contract inAB。The size of the output tensor is the size of the uncontracted dimensions ofAfollowed by the size of the uncontracted dimensions ofB

example

C = tensorprod (A,B,dim)specifies the same dimensions to contract in bothAB

example

C = tensorprod (A,B)returns the外产品tenso之间rsAB。This syntax is equivalent to using one of the previous syntaxes withdima = dimb = []或者dim = []。The size of the output tensor is[尺寸(a)尺寸(b)]

example

C = tensorprod (A,B,"all")returns theinner producttenso之间rsAB,必须是相同的大小。输出是标量。

example

C = tensorprod (___,NumDimensionsA=ndimsA)optionally specifies the number of dimensions in tensorAin addition to any of the input argument combinations in previous syntaxes. Use this option whenA有尾随的单例尺寸,预计将传递到输出。例如,TensorProd(A,B,NumDimensionsA=4)计算张量之间的外产物ABwhereA总共有四个维度。

例子

全部收缩

创建两个带有随机元素的3-D张量。

a = rand(3,2,5);b = rand(2,4,5);

计算张量产品AB,承包第二维Awith the first dimension ofB。结果张量包含未合同的尺寸A其次是未合同的尺寸B

c = tensorprod(a,b,2,1); size(C)
ans =1×43 5 4 5

To contract multiple dimensions in a tensor product, specify the dimensions to contract as vectors. Calculate another tensor product betweenAB,但这次合同二维:

  • 收缩第二维Awith the first dimension ofB

  • 签约第三维A与第三维B

D = tensorprod(A,B,[2 3],[1 3]); size(D)
ans =1×23 4

创建两个具有随机元素的4-D张量。

A = rand(7,3,6,5); B = rand(9,3,4,5);

计算张量产品AB,收缩每个张量的第二和第四维。检查结果的大小。

c = tensorprod(a,b,[2 4]); size(C)
ans =1×47 6 9 4

Create two tensors with random elements.

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

Calculate the outer product of the two tensors. Check the size of the result.

c = tensorprod(a,b); size(C)
ans =1×73 2 3 4 4 3 3

TensorProd乘以两个张量中元素的所有组合,因此所得张量的尺寸等于[尺寸(a)尺寸(b)]

Create two 4-D tensors of the same size with random elements.

a = rand(4,4,3,2);b = rand(4,4,3,2);

计算张量的内部产品,指定“全部”合同所有维度的选项。

c = tensorprod(a,b,“全部”)
C = 23.6148

创建两个具有随机元素的4-D张量。Ahas a trailing dimension of size 1, which MATLAB ignores by convention.

a = rand(3,4,5,1);b = rand(4,5,6,7);

计算张量产品AB,签约第二和第三维Awith the first and second dimensions ofB。Because MATLAB ignores the trailing singleton dimension ofA, the result has only three dimensions.

c = tensorprod(a,b,[2 3],[1 2]); size(C)
ans =1×33 6 7

保留单身尺寸A, use thenumdimensionsaoption to specify the number of dimensions inA。The result now has four dimensions.

D = tensorprod(A,B,[2 3],[1 2],NumDimensionsA=4); size(D)
ans =1×43 1 6 7

Input Arguments

全部收缩

输入张量, specified as arrays.TensorProddoes not conjugate complex inputs during its calculations. If conjugation is required, useconj(A)或者conj(B)to conjugate complex inputs before passing them toTensorProd

Data Types:single|double
Complex Number Support:Yes

收缩的维度AB, specified as vectors.dimAdimB必须具有相同的长度并且成对匹配。合同尺寸的尺寸也必须匹配,因此size(A,dimA)必须等于大小(b,dimb)

例子:TensorProd(A,B,[1 3],[2 4])contracts the first dimension ofA与第二维B, and the third dimension ofAwith the fourth dimension ofB

Data Types:double|single|int8|INT16|INT32|int64|uint8|UINT16|uint32|uint64

收缩的维度bothAB,指定为矢量。合同尺寸的尺寸必须匹配,因此size(A,dim)必须等于size(B,dim)

例子:tensorprod(a,b,[1 3])contracts the first dimension ofAwith the first dimension ofB, and the third dimension ofA与第三维B

Data Types:double|single|int8|INT16|INT32|int64|uint8|UINT16|uint32|uint64

尺寸的数量A,指定为标量。使用此选项时A有尾随的单例尺寸,预计将传递到输出。

例子:TensorProd(A,B,NumDimensionsA=4)计算张量之间的外产物ABwhereA总共有四个维度。

More About

全部收缩

Inner Product

The inner product of two tensors is a generalization of the dot product operation for vectors as calculated by。DOT产品操作(乘和总和)均在张量中的所有相应尺寸上执行,因此该操作返回标量值。对于此操作,张量必须具有相同的尺寸。

例如,if tensorsUVhave the samen方面(i,j,k,...,n), then the inner productTensorProd(U,V,"all")由总结给出:

n k j i U i , j , k , , n V i , j , k , , n = w

Outer Product

The outer product for tensors is a generalization of the Kronecker product for 2-D matrices as calculated by克朗。两个张量的外产物乘以其元素的所有组合。由于张量没有收缩的尺寸,因此输出是一个较大的张量。

例如,if tensorUhas dimensions(i, j, k, ...)和张量Vhas dimensions(l, m, n, ...), then the outer producttensorprod(u,v)是(谁)给的:

U i , j , k , V l , m , n , = W i , j , k , , l , m , n ,

Tensor Product

The tensor product combines the inner and outer product operations. You can specify pairs of dimensions with the same size in each tensor to becontracted通过内部产品彼此之间,这些尺寸被乘以并求和,以将尺寸降低到一个。在所有指定的维度均收缩后,其余的尺寸具有外部产品操作,将其元素的所有组合倍增。

例如,if tensorUhas dimensions(i,j,k)和张量Vhas dimensions(i,j,m), then the tensor productTensorProd(U,V,[1 2])contracts the first two dimensions of each tensor with each other and the result has dimensions(K,m):

j i U i , j , k V i , j , m = W k , m

Version History

在R2022a中引入