• pytorch的matmul怎么广播


    1)如果是两个1维的,就向量内积;
    2)如果两个都是2维的,就矩阵相乘
    3)如果第一个是1维,第二个是2维;填充第一个使得能够和第二个参数相乘;如果第一个是2维,第二个是1维,就是矩阵和向量相乘;
    例:

    a = torch.zeros(7,)
    b= torch.zeros(7,8)

    >>> torch.matmul(a,b)
    tensor([0., 0., 0., 0., 0., 0., 0., 0.])

    如果a是5,8维,报错。

    例子2:

    a = torch.zeros(8,)
    b= torch.zeros(7,8)

    >>> torch.matmul(b,a)
    tensor([0., 0., 0., 0., 0., 0., 0.])

    如果a是5,7维,报错。

    4) 高维情况

    i)其中一个1维,另一个N维(N>2): 类似3),即需要靠近的那个维数相同,比如(7,)和(7,8,11),又比如(7,8,11)和(11,)

    ii)都高于2维,那么除掉最后两个维度之外(两个数的维数满足矩阵乘法,即,(m,p)和(p,n)),剩下的纬度满足pytorch的广播机制。比如:

    # can line up trailing dimensions
    >>> x=torch.empty(5,3,4,1)
    >>> y=torch.empty(  3,1,1)
    # x and y are broadcastable.
    # 1st trailing dimension: both have size 1
    # 2nd trailing dimension: y has size 1
    # 3rd trailing dimension: x size == y size
    # 4th trailing dimension: y dimension doesn't exist




    https://pytorch.org/docs/stable/notes/broadcasting.html#broadcasting-semantics


    https://pytorch.org/docs/stable/torch.html?highlight=matmul#torch.matmul

    torch.matmul(tensor1tensor2out=None) → Tensor:





    # can line up trailing dimensions >>> x=torch.empty(5,3,4,1) >>> y=torch.empty( 3,1,1) # x and y are broadcastable. # 1st trailing dimension: both have size 1 # 2nd trailing dimension: y has size 1 # 3rd trailing dimension: x size == y size # 4th trailing dimension: y dimension doesn't exist

    ###########################################################################需要除掉两个数的后两个维度,然后再看是否满足广播机制
    torch.matmul(tensor1tensor2out=None) → Tensor

    Matrix product of two tensors.

    The behavior depends on the dimensionality of the tensors as follows:

    • If both tensors are 1-dimensional, the dot product (scalar) is returned.

    • If both arguments are 2-dimensional, the matrix-matrix product is returned.

    • If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of the matrix multiply. After the matrix multiply, the prepended dimension is removed.

    • If the first argument is 2-dimensional and the second argument is 1-dimensional, the matrix-vector product is returned.

    • If both arguments are at least 1-dimensional and at least one argument is N-dimensional (where N > 2), then a batched matrix multiply is returned. If the first argument is 1-dimensional, a 1 is prepended to its dimension for the purpose of the batched matrix multiply and removed after. If the second argument is 1-dimensional, a 1 is appended to its dimension for the purpose of the batched matrix multiple and removed after. The non-matrix (i.e. batch) dimensions are broadcasted (and thus must be broadcastable). For example, if tensor1 is a (j imes 1 imes n imes m)(j×1×n×m)tensor and tensor2 is a (k imes m imes p)(k×m×p) tensor, out will be an (j imes k imes n imes p)(j×k×n×p) tensor.



  • 相关阅读:
    MySQL开启general_log并设置路径
    mysql日志文件开启及详解:General_log 和 Binlog
    mysql binary like_MYSQL的binary解决mysql数据大小写敏感问题的方法
    分布式系统回滚机制
    ubuntu 后台运行的几种方法!
    ubuntu磁盘分配和挂载
    .NET Core SDK在Windows系统安装后出现Failed to load the hostfxr.dll等问题的解决方法
    数组.html
    温习
    for练习.html
  • 原文地址:https://www.cnblogs.com/Wanggcong/p/11001119.html
Copyright © 2020-2023  润新知