1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        從零開始深度學習Pytorch筆記(6)——張量的數(shù)學運算

        共 2118字,需瀏覽 5分鐘

         ·

        2020-01-20 23:20

        2a2135e126c1a8e8fd72f5a30f50340a.webp

        a660f7db108e9bb28755adb3c795128b.webp

        前文傳送門:

        從零開始深度學習Pytorch筆記(1)——安裝Pytorch

        從零開始深度學習Pytorch筆記(2)——張量的創(chuàng)建(上)

        從零開始深度學習Pytorch筆記(3)——張量的創(chuàng)建(下)

        從零開始深度學習Pytorch筆記(4)——張量的拼接與切分

        從零開始深度學習Pytorch筆記(5)——張量的索引與變換


        在該系列的上一篇,我們介紹了更多Pytorch中的張量的索引與變換,本文研究張量的數(shù)學運算。

        張量的加減乘除運算

        使用torch.add()張量相加

        torch.add(input,?other,?out=None)

        參數(shù):

        input:張量

        other:另一個張量或者數(shù)值

        以下是張量加上20(對應(yīng)位置都加上20)

        a?=?torch.randn(4)
        a

        d62f38414aa4b5e1d9446e1cf8900352.webp

        torch.add(a,?20)

        fcc8d65c4897101b8a400a925f7d4a70.webp

        a?=?torch.randn(4)
        a

        以下是兩個張量相加

        bdd0a5c1732c90c0c9240849ad83da5b.webp

        b?=?torch.randn(4)
        b

        e53eceb12f7932d6c1096213851a5f7c.webp

        torch.add(a,?b)

        5cffb464554d3ec228d7a169823d1fdd.webp

        使用torch.addcdiv()張量相加和相除

        torch.addcdiv(input,?value=1,?tensor1,?tensor2,?out=None)

        tensor2tensor1逐元素相除,然后乘以標量值value?并加到input

        總之意思用公式表達為:input+value*tensor1/tensor2

        t?=?torch.randn(1,?3)
        t1?=?torch.randn(3,?1)
        t2?=?torch.randn(1,?3)
        torch.addcdiv(t,?0.1,?t1,?t2)

        這個較為復雜,因為維度不一致也可以操作,我將它拆解出來,請看:

        t?=?torch.randn(1,?3)
        t

        69eb6a9b1e42b9a55825167426592243.webp


        t1?=?torch.randn(3,?1)
        t1

        0fdaaf170c48f82a8b5d7524a796db51.webp


        t2?=?torch.randn(3,?1)
        t2

        1c543ed3aea2082eafd296ad77a53322.webp

        t12?=?t1/t2
        t12

        11d470b4cca2988292fe75c9f53c40a1.webp

        t12*0.1

        212faf8b7e30dc004dc32ffcb53ffac8.webp

        #維度不同也可以相加
        t12*0.1+t

        703c97fa65767554213078f9fbc304e6.webp

        以上的操作,用torch.addcdiv()一行就可以搞定,請看:

        torch.addcdiv(t,?0.1,?t1,?t2)

        3af4f7965b52d583922352a3644f66a7.webp

        結(jié)果相同,你理解了對吧~

        torch.addcmul()張量相加和相乘

        addcmul(input,?value=1,?tensor1,?tensor2,?out=None)

        input+value*tensor1*tensor2

        類似上面的操作,只是把相除變成相乘了

        t?=?torch.randn(1,?3)
        t1?=?torch.randn(3,?1)
        t2?=?torch.randn(1,?3)
        torch.addcdiv(t,?0.1,?t1,?t2)

        6c988445f0c9a1cdd09dc1322a4f2d44.webp

        使用torch.sub()張量相減

        t1?=?torch.rand(2,3)
        t1

        01e48b08ba19067c212ae2c728632507.webp

        t2?=?torch.rand(2,3)
        t2

        cdc2694f872f4ef8e890d7bdd266e598.webp

        t?=?torch.sub(t1,?t2)
        print(t)

        9dd4441c6312be5a3b72d20612f3bad4.webp

        使用torch.mul()張量相乘

        t1?=?torch.rand(5,?5)
        t2?=?torch.rand(5,?5)
        t?=?torch.mul(t1,?t2)
        print(t)

        6ff6e79108dc0a91b5f8f80c29a62636.webp

        使用torch.div()張量相除

        t1?=?torch.rand(5,?5)
        t2?=?torch.rand(5,?5)
        t?=?torch.div(t1,?t2)
        print(t)

        a7b0bb41257c25dcaa4e55c25738c091.webp

        張量的對數(shù),指數(shù),冪函數(shù)運算

        torch.log(input,out=None)#計算input的自然對數(shù)
        torch.log10(input,out=None)#計算input的10為底的對數(shù)
        torch.log2(input,out=None)#計算input的2為底的對數(shù)
        torch.exp(input,out=None)#對輸入input按元素求e次冪值,并返回結(jié)果張量,冪值e可以為標量也可以是和input相同大小的張量
        torch.pow(input,out=None)#次方運算

        其實以上的都差不多,那就舉一個例子:

        a?=?torch.randn(5)
        a

        324f664a9e6a3e40ba6d136b9d4019c7.webp

        torch.log(a)

        a0d4a37530429e355ebc6589648c3cc8.webp

        張量的三角函數(shù)運算

        torch.abs(input,out=None)#計算張量的每個元素絕對值
        torch.acos(input,out=None)#返回一個新張量,包含輸入張量每個元素的反余弦
        torch.cosh(input,out=None)#返回一個新張量,包含輸入input張量每個元素的雙曲余弦
        torch.cos(input,out=None)#返回一個新張量,包含輸入input張量每個元素的余弦
        torch.asin(input,out=None)#返回一個新張量,包含輸入input張量每個元素的反正弦
        torch.atan(input,out=None)#返回一個新張量,包含輸入input張量每個元素的反正切
        torch.atan2(input1,?input2,?out=None)#返回一個新張量,包含兩個輸入張量input1和input2的反正切函數(shù)

        其實以上的都差不多,那就舉一個例子:

        a?=?torch.randn(5)
        a?

        fd89282b78cb0a3d54fd37141fcdfedf.webp

        torch.cos(a)

        7205f91f9c742e90946fc70f18dfa598.webp


        歡迎關(guān)注公眾號學習之后的深度學習連載部分~


        a401e9114a4983fe17aa39516788e9d3.webp喜歡記得點在看哦,證明你來看過~
        瀏覽 106
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            中国三级黄色大片 | 精品一区二区三区在线播放 | 天堂在线aaa | 夜操操 | 免费在线观看黄片 | 黑人性视 | 全黄一级裸片一60分钟 | 中国婬乱a一级毛片多女麻豆 | 日日夜夜肏 | 天天肏屄视频 |