Day236:addmm()和addmm_()的用法详解
ztj100 2024-11-03 16:15 24 浏览 0 评论
函数解释
在torch/_C/_VariableFunctions.py的有该定义,意义就是实现一下公式:
换句话说,就是需要传入5个参数,mat里的每个元素乘以beta,mat1和mat2进行矩阵乘法(左行乘右列)后再乘以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.代码中a,b,c和d展示的是完全形式,即标明了位置参数和传入参数。可以看到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参数的位置可变性,并且beta和alpha都缺省了:
完成的公式为:
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
相关推荐
- Sublime Text 4 稳定版 Build 4113 发布
-
IT之家7月18日消息知名编辑器SublimeText4近日发布了Build4113版本,是SublimeText4的第二个稳定版。IT之家了解到,SublimeTe...
- 【小白课程】openKylin便签贴的设计与实现
-
openKylin便签贴作为侧边栏的一个小插件,提供便捷的文本记录和灵活的页面展示。openKylin便签贴分为两个部分:便签列表...
- 壹啦罐罐 Android 手机里的 Xposed 都装了啥
-
这是少数派推出的系列专题,叫做「我的手机里都装了啥」。这个系列将邀请到不同的玩家,从他们各自的角度介绍手机中最爱的或是日常使用最频繁的App。文章将以「每周一篇」的频率更新,内容范围会包括iOS、...
- 电气自动化专业词汇中英文对照表(电气自动化专业英语单词)
-
专业词汇中英文对照表...
- Python界面设计Tkinter模块的核心组件
-
我们使用一个模块,我们要熟悉这个模块的主要元件。如我们设计一个窗口,我们可以用Tk()来完成创建;一些交互元素,按钮、标签、编辑框用到控件;怎么去布局你的界面,我们可以用到pack()、grid()...
- 以色列发现“死海古卷”新残片(死海古卷是真的吗)
-
编译|陈家琦据艺术新闻网(artnews.com)报道,3月16日,以色列考古学家发现了死海古卷(DeadSeaScrolls)新残片。新出土的羊皮纸残片中包括以希腊文书写的《十二先知书》段落,这...
- 鸿蒙Next仓颉语言开发实战教程:订单列表
-
大家上午好,最近不断有友友反馈仓颉语言和ArkTs很像,所以要注意不要混淆。今天要分享的是仓颉语言开发商城应用的订单列表页。首先来分析一下这个页面,它分为三大部分,分别是导航栏、订单类型和订单列表部分...
- 哪些模块可以用在 Xposed for Lollipop 上?Xposed 模块兼容性解答
-
虽然已经有了XposedforLollipop的安装教程,但由于其还处在alpha阶段,一些Xposed模块能不能依赖其正常工作还未可知。为了解决大家对于模块兼容性的疑惑,笔者尽可能多...
- 利用 Fluid 自制 Mac 版 Overcast 应用
-
我喜爱收听播客,健身、上/下班途中,工作中,甚至是忙着做家务时。大多数情况下我会用MarcoArment开发的Overcast(Freemium)在iPhone上收听,这是我目前最喜爱的Po...
- 浅色Al云食堂APP代码(三)(手机云食堂)
-
以下是进一步优化完善后的浅色AI云食堂APP完整代码,新增了数据可视化、用户反馈、智能推荐等功能,并优化了代码结构和性能。项目结构...
- 实战PyQt5: 121-使用QImage实现一个看图应用
-
QImage简介QImage类提供了独立于硬件的图像表示形式,该图像表示形式可以直接访问像素数据,并且可以用作绘制设备。QImage是QPaintDevice子类,因此可以使用QPainter直接在图...
- 滚动条隐藏及美化(滚动条隐藏但是可以滚动)
-
1、滚动条隐藏背景/场景:在移动端,滑动的时候,会显示默认滚动条,如图1://隐藏代码:/*隐藏滚轮*/.ul-scrool-box::-webkit-scrollbar,.ul-scrool...
- 浅色AI云食堂APP完整代码(二)(ai 食堂)
-
以下是整合后的浅色AI云食堂APP完整代码,包含后端核心功能、前端界面以及优化增强功能。项目采用Django框架开发,支持库存管理、订单处理、财务管理等核心功能,并包含库存预警、数据导出、权限管理等增...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)