Pytorch深度学习addmm()和addmm_()函数用法解析
作者:悲恋花丶无心之人 发布时间:2021-01-02 04:04:25
一、函数解释
在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://nickhuang1996.blog.csdn.net/article/details/90638449
猜你喜欢
- 本文以实例形式展示了Python获取电脑硬件信息及状态的实现方法,是Python程序设计中很有实用价值的技巧。分享给大家供大家参考之用。具体
- 验证码制作#string模块自带数字、字母、特殊字符变量集合,不需要我们手写集合import stringimport randomimpo
- 这个问题让我查了许多天才解决,为了避免后面的人重复走弯路,记录下来。问题描述:我在ubuntu 下编译安装了caffe ,在命令行模式下可以
- Python编程中raise可以实现报出错误的功能,而报错的条件可以由程序员自己去定制。在面向对象编程中,可以先预留一个方法接口不实现,在其
- 继上篇博客Python实现简易通讯录后,我就想写一个复杂点的学生信息管理系统,这次实现的功能有1.学生信息的录入管理;2.学生选课操作;3.
- 可在图片连接后面加上宽和高,如:width="150" height="50"<h
- 本文实例讲述了vue+php实现的微博留言功能。分享给大家供大家参考,具体如下:html部分:<!doctype html>&l
- 目录socket概念socket基本用法创建tcp套接字创建udp套接字socket内建方法实现端口扫描总结socket概念socket又称
- 前些日子在SmashingMagazine看到一篇关于CSS3新技术不错的文章,它详细介绍了CSS3的新特性和它的使用方法,它包括:浏览器专
- 前言django,web开发中,用django-debug-toolbar来调试请求的接口,无疑是完美至极。 可能本人,见识博浅,才说完美至
- 本文实例讲述了python使用Flask框架获取用户IP地址的方法。分享给大家供大家参考。具体如下:下面的代码包含了html页面和pytho
- 在Python中有时会碰到需要一个一维列向量(n*1)与另一个一维列向量(n*1)的转置(1*n)相乘,得到一个n*n的矩阵的情况。但是在p
- python是很容易上手的编程语言,但是有些时候使用python编写的程序并不能保证其运行速度(例如:while 和 for),这个时候我们
- 功能很简单,代码也很简洁,这里就不多废话了。package mainimport ( "fmt
- re.findall()在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。语法格式:re.find
- 本文主要讲如何不依赖TenserFlow等高级API实现一个简单的神经网络来做分类,所有的代码都在下面;在构造的数据(通过程序构造)上做了验
- 本文采用拉普拉斯算子计算影像的模糊程度,小于阈值的影像被认为是模糊的,从而被移动到专门存放模糊影像的文件夹。本文只使用cv2和shutil库
- 因一些特殊需求需要以参数的形式获取字典 * 定的值,网上搜了一下并没有特别好的实现(并没有太认真去找~),所以自己实现了一个,以供大家参考:)
- 我使用的是anaconda安装的环境,其中有一个是h5py,自动安装的是2.7.0的版本,这个版本会导致保存模型时python奔溃。cond
- 本文采用os.walk()和os.listdir()两种方法,获取指定文件夹下的文件名。一、os.walk()模块os中的walk()函数可