百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

Day236:addmm()和addmm_()的用法详解

ztj100 2024-11-03 16:15 23 浏览 0 评论

函数解释

torch/_C/_VariableFunctions.py的有该定义,意义就是实现一下公式:

换句话说,就是需要传入5个参数mat里的每个元素乘以betamat1mat2进行矩阵乘法左行乘右列)后再乘以alpha,最后将这2个结果加在一起。但是这样说可能没啥概念,接下来博主为大家写上一段代码,大家就明白了~

    def addmm(self, beta=1, mat, alpha=1, mat1, mat2, out=None): # real signature unknown; restored from __doc__
        """
        addmm(beta=1, mat, alpha=1, mat1, mat2, out=None) -> Tensor
        
        Performs a matrix multiplication of the matrices :attr:`mat1` and :attr:`mat2`.
        The matrix :attr:`mat` is added to the final result.
        
        If :attr:`mat1` is a :math:`(n \times m)` tensor, :attr:`mat2` is a
        :math:`(m \times p)` tensor, then :attr:`mat` must be
        :ref:`broadcastable <broadcasting-semantics>` with a :math:`(n \times p)` tensor
        and :attr:`out` will be a :math:`(n \times p)` tensor.
        
        :attr:`alpha` and :attr:`beta` are scaling factors on matrix-vector product between
        :attr:`mat1` and :attr`mat2` and the added matrix :attr:`mat` respectively.
        
        .. math::
            out = \beta\ mat + \alpha\ (mat1_i \mathbin{@} mat2_i)
        
        For inputs of type `FloatTensor` or `DoubleTensor`, arguments :attr:`beta` and
        :attr:`alpha` must be real numbers, otherwise they should be integers.
        
        Args:
            beta (Number, optional): multiplier for :attr:`mat` (:math:`\beta`)
            mat (Tensor): matrix to be added
            alpha (Number, optional): multiplier for :math:`mat1 @ mat2` (:math:`\alpha`)
            mat1 (Tensor): the first matrix to be multiplied
            mat2 (Tensor): the second matrix to be multiplied
            out (Tensor, optional): the output tensor
        
        Example::
        
            >>> M = torch.randn(2, 3)
            >>> mat1 = torch.randn(2, 3)
            >>> mat2 = torch.randn(3, 3)
            >>> torch.addmm(M, mat1, mat2)
            tensor([[-4.8716,  1.4671, -1.3746],
                    [ 0.7573, -3.9555, -2.8681]])
        """
        pass

代码范例

1.先摆出代码,大家可以先复制粘贴运行一下,在之后会一一讲解

"""
@author:nickhuang1996
"""
import torch
 
rectangle_height = 3
rectangle_width = 3
inputs = torch.randn(rectangle_height, rectangle_width)
for i in range(rectangle_height):
    for j in range(rectangle_width):
        inputs[i] = i * torch.ones(rectangle_width)
'''
inputs and its transpose
-->inputs   =   tensor([[0., 0., 0.],
                        [1., 1., 1.],
                        [2., 2., 2.]])
-->inputs_t =   tensor([[0., 1., 2.],
                        [0., 1., 2.],
                        [0., 1., 2.]])
'''
print("inputs:\n", inputs)
inputs_t = inputs.t()
print("inputs_t:\n", inputs_t)
'''
inputs_t @ inputs_t    [[0., 1., 2.],       [[0., 1., 2.],          [[0., 3., 6.]
                    =   [0., 1., 2.],   @    [0., 1., 2.],     =     [0., 3., 6.]
                        [0., 1., 2.]]        [0., 1., 2.]]           [0., 3., 6.]]
'''
 
'''a, b, c and d = 1 * inputs + 1 * (inputs_t @ inputs_t)'''
a = torch.addmm(input=inputs, mat1=inputs_t, mat2=inputs_t)
b = inputs.addmm(mat1=inputs_t, mat2=inputs_t)
c = torch.addmm(input=inputs, beta=1, mat1=inputs_t, mat2=inputs_t, alpha=1)
d = inputs.addmm(beta=1, mat1=inputs_t, mat2=inputs_t, alpha=1)
'''e and f = 1 * inputs + 1 * (inputs_t @ inputs_t)'''
e = torch.addmm(inputs, inputs_t, inputs_t)
f = inputs.addmm(inputs_t, inputs_t)
'''1 * inputs + 1 * (inputs_t @ inputs_t)'''
g = inputs.addmm(1, inputs_t, inputs_t)
'''2 * inputs + 1 * (inputs_t @ inputs_t)'''
g2 = inputs.addmm(2, inputs_t, inputs_t)
'''h = 1 * inputs + 1 * (inputs_t @ inputs_t)'''
h = inputs.addmm(1, 1, inputs_t, inputs_t)
'''h12 = 1 * inputs + 2 * (inputs_t @ inputs_t)'''
h12 = inputs.addmm(1, 2, inputs_t, inputs_t)
'''h21 = 2 * inputs + 1 * (inputs_t @ inputs_t)'''
h21 = inputs.addmm(2, 1, inputs_t, inputs_t)
print("a:\n", a)
print("b:\n", b)
print("c:\n", c)
print("d:\n", d)
 
print("e:\n", e)
print("f:\n", f)
 
print("g:\n", g)
print("g2:\n", g2)
 
print("h:\n", h)
print("h12:\n", h12)
print("h21:\n", h21)
print("inputs:\n", inputs)
'''inputs = 1 * inputs - 2 * (inputs @ inputs_t)'''
'''
inputs @ inputs_t       [[0., 0., 0.],       [[0., 1., 2.],          [[0., 0., 0.]
                    =    [1., 1., 1.],   @    [0., 1., 2.],     =     [0., 3., 6.]
                         [2., 2., 2.]]        [0., 1., 2.]]           [0., 6., 12.]]
'''
inputs.addmm_(1, -2, inputs, inputs_t)  # In-place
print("inputs:\n", inputs)

2.其中

inputs是一个3×3的矩阵,为

tensor([[0., 0., 0.],
        [1., 1., 1.],
        [2., 2., 2.]])

inputs_t也是一个3×3的矩阵,是inputs转置矩阵,为

tensor([[0., 1., 2.],
        [0., 1., 2.],
        [0., 1., 2.]])

* inputs_t @ inputs_t

'''
inputs_t @ inputs_t    [[0., 1., 2.],       [[0., 1., 2.],          [[0., 3., 6.]
                    =   [0., 1., 2.],   @    [0., 1., 2.],     =     [0., 3., 6.]
                        [0., 1., 2.]]        [0., 1., 2.]]           [0., 3., 6.]]
'''

3.代码中abcd展示的是完全形式,即标明了位置参数传入参数。可以看到input这个位置参数可以写在函数的前面,即

torch.addmm(input, mat1, mat2) = inputs.addmm(mat1, mat2)

完成的公式为:

1 × inputs + 1 ×(inputs_t @ inputs_t)

'''a, b, c and d = 1 * inputs + 1 * (inputs_t @ inputs_t)'''
a = torch.addmm(input=inputs, mat1=inputs_t, mat2=inputs_t)
b = inputs.addmm(mat1=inputs_t, mat2=inputs_t)
c = torch.addmm(input=inputs, beta=1, mat1=inputs_t, mat2=inputs_t, alpha=1)
d = inputs.addmm(beta=1, mat1=inputs_t, mat2=inputs_t, alpha=1)	
a:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
b:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
c:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
d:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])

4.下面的例子更好了说明了input参数的位置可变性,并且betaalpha缺省了:

完成的公式为:

1 × inputs + 1 ×(inputs_t @ inputs_t)

'''e and f = 1 * inputs + 1 * (inputs_t @ inputs_t)'''
e = torch.addmm(inputs, inputs_t, inputs_t)
f = inputs.addmm(inputs_t, inputs_t)
e:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
f:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])

5.加一个参数,实际上是添加了beta这个参数

完成的公式为:

g = 1 × inputs + 1 ×(inputs_t @ inputs_t)

g2 = 2 × inputs + 1 ×(inputs_t @ inputs_t)

'''1 * inputs + 1 * (inputs_t @ inputs_t)'''
g = inputs.addmm(1, inputs_t, inputs_t)
'''2 * inputs + 1 * (inputs_t @ inputs_t)'''
g2 = inputs.addmm(2, inputs_t, inputs_t)
g:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
g2:
tensor([[ 0.,  3.,  6.],
        [ 2.,  5.,  8.],
        [ 4.,  7., 10.]])

6.再加一个参数,实际上是添加了alpha这个参数

完成的公式为:

h = 1 × inputs + 1 ×(inputs_t @ inputs_t)

h12 = 1 × inputs + 2 ×(inputs_t @ inputs_t)

h21 = 2 × inputs + 1 ×(inputs_t @ inputs_t)

'''h = 1 * inputs + 1 * (inputs_t @ inputs_t)'''
h = inputs.addmm(1, 1, inputs_t, inputs_t)
'''h12 = 1 * inputs + 2 * (inputs_t @ inputs_t)'''
h12 = inputs.addmm(1, 2, inputs_t, inputs_t)
'''h21 = 2 * inputs + 1 * (inputs_t @ inputs_t)'''
h21 = inputs.addmm(2, 1, inputs_t, inputs_t)
h:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
h12:
tensor([[ 0.,  6., 12.],
        [ 1.,  7., 13.],
        [ 2.,  8., 14.]])
h21:
tensor([[ 0.,  3.,  6.],
        [ 2.,  5.,  8.],
        [ 4.,  7., 10.]])

7.当然,以上的步骤inputs没有变化,还是为

inputs:
tensor([[0., 0., 0.],
        [1., 1., 1.],
        [2., 2., 2.]])

*8.addmm_()的操作和addmm()函数功能相同,区别就是addmm_()inplace的操作,也就是在原对象基础上进行修改,即把改变之后的变量再赋给原来的变量。例如:

inputs的值变成了改变之后的值,不用再去写 某个变量=addmm_() ,因为inputs就是改变之后的变量

*inputs@ inputs_t

'''
inputs @ inputs_t       [[0., 0., 0.],       [[0., 1., 2.],          [[0., 0., 0.]
                    =    [1., 1., 1.],   @    [0., 1., 2.],     =     [0., 3., 6.]
                         [2., 2., 2.]]        [0., 1., 2.]]           [0., 6., 12.]]
'''

完成的公式为:

inputs = 1 × inputs - 2 ×(inputs @ inputs_t)

'''inputs = 1 * inputs - 2 * (inputs @ inputs_t)'''
inputs.addmm_(1, -2, inputs, inputs_t)  # In-place
inputs:
tensor([[  0.,   0.,   0.],
        [  1.,  -5., -11.],
        [  2., -10., -22.]])

三、代码运行结果

inputs:
tensor([[0., 0., 0.],
        [1., 1., 1.],
        [2., 2., 2.]])
inputs_t:
tensor([[0., 1., 2.],
        [0., 1., 2.],
        [0., 1., 2.]])
a:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
b:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
c:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
d:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
e:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
f:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
g:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
g2:
tensor([[ 0.,  3.,  6.],
        [ 2.,  5.,  8.],
        [ 4.,  7., 10.]])
h:
tensor([[0., 3., 6.],
        [1., 4., 7.],
        [2., 5., 8.]])
h12:
tensor([[ 0.,  6., 12.],
        [ 1.,  7., 13.],
        [ 2.,  8., 14.]])
h21:
tensor([[ 0.,  3.,  6.],
        [ 2.,  5.,  8.],
        [ 4.,  7., 10.]])
inputs:
tensor([[0., 0., 0.],
        [1., 1., 1.],
        [2., 2., 2.]])
inputs:
tensor([[  0.,   0.,   0.],
        [  1.,  -5., -11.],
        [  2., -10., -22.]])

原文:https://blog.csdn.net/qq_36556893/article/details/90638449

相关推荐

能量空间物质相互转化途径(能量与空间转换相对论公式)

代码实现<!DOCTYPEhtml><htmllang="zh"><head>...

从零开始的Flex布局掌握(flex布局实战)

前言在现代网页设计中,布局是一个至关重要的环节,在过去的一段时间里,页面的布局还都是通过table...

flex布局在css中的使用,一看就会!

1.认识flex布局我们在写前端页面的时候可能会遇到这样的问题:同样的一个页面在1920x1080的大屏幕中显示正常,但是在1366x768的小屏幕中却显示的非常凌乱。...

前端入门——弹性布局(Flex)(web前端弹性布局)

前言在css3Flex技术出现之前制作网页大多使用浮动(float)、定位(position)以及显示(display)来布局页面,随着互联网快速发展,移动互联网的到来,已无法满足需求,它对于那些...

CSS Flex 容器完整指南(css flex-shrink)

概述CSSFlexbox是现代网页布局的强大工具。本文详细介绍用于flex容器的CSS属性:...

Centos 7 network.service 启动失败

执行systemctlrestartnetwork重启网络报如下错误:Jobfornetwork.servicefailedbecausethecontrolprocessex...

CentOS7 执行systemctl start iptables 报错:...: Unit not found.

#CentOS7执行systemctlstartiptables报错:Failedtostartiptables.service:Unitnotfound.在CentOS7中...

systemd入门6:journalctl的详细介绍

该来的总会来的,逃是逃不掉的。话不多说,man起来:manjournalctl洋洋洒洒几百字的描述,是说journalctl是用来查询systemd日志的,这些日志都是systemd-journa...

Linux上的Systemctl命令(systemctl命令详解)

LinuxSystemctl是一个系统管理守护进程、工具和库的集合,用于取代SystemV、service和chkconfig命令,初始进程主要负责控制systemd系统和服务管理器。通过Syste...

如何使用 systemctl 管理服务(systemctl添加服务)

systemd是一个服务管理器,目前已经成为Linux发行版的新标准。它使管理服务器变得更加容易。了解并利用组成systemd的工具将有助于我们更好地理解它提供的便利性。systemctl的由来...

内蒙古2024一分一段表(文理)(内蒙古考生2020一分一段表)

分数位次省份...

2016四川高考本科分数段人数统计,看看你有多少竞争对手

昨天,四川高考成绩出炉,全省共220,196人上线本科,相信每个考生都查到了自己的成绩。而我们都清楚多考1分就能多赶超数百人,那你是否知道,和你的分数一样的人全省有几个人?你知道挡在你前面的有多少人?...

难怪最近电脑卡爆了,微软确认Win11资源管理器严重BUG

近期,Win11操作系统的用户普遍遭遇到了一个令人头大的问题:电脑卡顿,CPU占用率异常增高。而出现该现象的原因竟然与微软最近的一次补丁更新有关。据报道,微软已经确认,问题源于Win11资源管...

微软推送Win11正式版22621.1702(KB5026372)更新

IT之家5月10日消息,微软今天推送了最新的Win11系统更新,21H2正式版通道推送了KB5026368补丁,版本号升至22000.1936,22H2版本推送了KB50263...

骗子AI换脸冒充亲戚,女子转账10万元后才发现异常……

“今天全靠你们,不然我这被骗的10万元肯定就石沉大海了。”7月19日,家住石马河的唐女士遭遇了“AI”换脸诈骗,幸好她报警及时,民警对其转账给骗子的钱成功进行止付。当天13时许,唐女士收到一条自称是亲...

取消回复欢迎 发表评论: