最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pytorch深度學習addmm()和addmm_()函數(shù)用法解析

 更新時間:2022年06月10日 09:12:41   作者:悲戀花丶無心之人  
這篇文章主要為大家介紹了Pytorch中addmm()和addmm_()函數(shù)用法解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

一、函數(shù)解釋

在torch/_C/_VariableFunctions.py的有該定義,意義就是實現(xiàn)一下公式:

換句話說,就是需要傳入5個參數(shù),mat里的每個元素乘以beta,mat1和mat2進行矩陣乘法(左行乘右列)后再乘以alpha,最后將這2個結(jié)果加在一起。但是這樣說可能沒啥概念,接下來博主為大家寫上一段代碼,大家就明白了~

    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的轉(zhuǎn)置矩陣,為

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展示的是完全形式,即標明了位置參數(shù)和傳入?yún)?shù)??梢钥吹絠nput這個位置參數(shù)可以寫在函數(shù)的前面,即

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參數(shù)的位置可變性,并且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.加一個參數(shù),實際上是添加了beta這個參數(shù)

完成的公式為:

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.再加一個參數(shù),實際上是添加了alpha這個參數(shù)

完成的公式為:

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()函數(shù)功能相同,區(qū)別就是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.]])

三、代碼運行結(jié)果

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.]])

以上就是Pytorch中addmm()和addmm_()函數(shù)用法解析的詳細內(nèi)容,更多關(guān)于Pytorch函數(shù)addmm() addmm_()的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python try except異常捕獲機制原理解析

    Python try except異常捕獲機制原理解析

    這篇文章主要介紹了Python try except異常捕獲機制原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Django+vue+vscode前后端分離搭建的實現(xiàn)

    Django+vue+vscode前后端分離搭建的實現(xiàn)

    本文以一個非常簡單的demo為例,介紹了利用django+drf+vue的前后端分離開發(fā)模式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2023-08-08
  • 淺析Django中關(guān)于session的使用

    淺析Django中關(guān)于session的使用

    這篇文章主要介紹了Django下關(guān)于session的使用,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • Python使用VIF實現(xiàn)檢測多重共線性

    Python使用VIF實現(xiàn)檢測多重共線性

    多重共線性是指多元回歸模型中有兩個或兩個以上的自變量,它們之間具有高度的相關(guān)性,本文主要介紹了如何使用VIF實現(xiàn)檢測多重共線性,需要的可以參考下
    2023-12-12
  • 使用Python統(tǒng)計相同像素值個數(shù)

    使用Python統(tǒng)計相同像素值個數(shù)

    這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)統(tǒng)計相同像素值個數(shù),文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2025-01-01
  • python?pandas?query的使用方法

    python?pandas?query的使用方法

    這篇文章主要介紹了python?pandas?query的使用方法,文章通過圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-09-09
  • 解決pycharm下os.system執(zhí)行命令返回有中文亂碼的問題

    解決pycharm下os.system執(zhí)行命令返回有中文亂碼的問題

    今天小編就為大家分享一篇解決pycharm下os.system執(zhí)行命令返回有中文亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python中time.ctime()實例用法

    python中time.ctime()實例用法

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python中time.ctime()實例用法內(nèi)容,有興趣的朋友們可以跟著學習參考下。
    2021-02-02
  • Python光學仿真實現(xiàn)波長與顏色之間對應關(guān)系示例解析

    Python光學仿真實現(xiàn)波長與顏色之間對應關(guān)系示例解析

    這篇文章主要為大家介紹了Python光學仿真實現(xiàn)波長與顏色之間對應關(guān)系的示例解析,有需要的我朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • 使用Python中的tkinter模塊作圖的方法

    使用Python中的tkinter模塊作圖的方法

    Python是一種解釋型、面向?qū)ο?、動態(tài)數(shù)據(jù)類型的高級程序設計語言。tkinter模塊(“Tk 接口”)是Python的標準Tk GUI工具包的接口。接下來通過本文給大家介紹Python中的tkinter模塊作圖教程,需要的朋友參考下
    2017-02-02

最新評論

东方市| 新疆| 定结县| 游戏| 龙井市| 河东区| 东城区| 同江市| 钟山县| 舟山市| 临夏县| 宁都县| 客服| 南川市| 龙州县| 涟水县| 宜都市| 颍上县| 江华| 湘潭市| 凤城市| 佛坪县| 上林县| 偏关县| 崇信县| 东光县| 五常市| 酉阳| 义乌市| 奉新县| 南漳县| 庆元县| 兴安县| 乌鲁木齐市| 炎陵县| 隆安县| 塔河县| 崇信县| 茶陵县| 克什克腾旗| 邯郸县|