久久精品国产亚洲高清|精品日韩中文乱码在线|亚洲va中文字幕无码久|伊人久久综合狼伊人久久|亚洲不卡av不卡一区二区|精品久久久久久久蜜臀AV|国产精品19久久久久久不卡|国产男女猛烈视频在线观看麻豆

    1. <style id="76ofp"></style>

      <style id="76ofp"></style>
      <rt id="76ofp"></rt>
      <form id="76ofp"><optgroup id="76ofp"></optgroup></form>
      1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機(jī)構(gòu)

        手機(jī)站
        千鋒教育

        千鋒學(xué)習(xí)站 | 隨時(shí)隨地免費(fèi)學(xué)

        千鋒教育

        掃一掃進(jìn)入千鋒手機(jī)站

        領(lǐng)取全套視頻
        千鋒教育

        關(guān)注千鋒學(xué)習(xí)站小程序
        隨時(shí)隨地免費(fèi)學(xué)習(xí)課程

        當(dāng)前位置:首頁(yè)  >  技術(shù)干貨  > torch.norm函數(shù)詳解

        torch.norm函數(shù)詳解

        來(lái)源:千鋒教育
        發(fā)布人:xqq
        時(shí)間: 2023-11-23 15:06:51 1700723211

        一、torch.norm函數(shù)

        torch.norm函數(shù)是PyTorch庫(kù)中的一個(gè)標(biāo)量計(jì)算函數(shù),用于在給定維度上計(jì)算輸入張量的范數(shù)(also known as vector length or magnitude)。

        常用的張量范數(shù)有L1范數(shù)和L2范數(shù),torch.norm默認(rèn)使用L2范數(shù),可以通過(guò)norm_type參數(shù)指定L1范數(shù)。

        
        import torch
        
        # 1D Tensor
        a = torch.tensor([1, 2, 3, 4, 5])
        print(torch.norm(a))       # output: tensor(7.4162)
        print(torch.norm(a, 1))    # output: tensor(15.)
        print(torch.norm(a, float('inf'))) # output: tensor(5.)
        
        # 2D Tensor
        b = torch.tensor([[1, 2, 3], [4, 5, 6]])
        print(torch.norm(b))       # output: tensor(9.5394)
        print(torch.norm(b, 1))    # output: tensor(15.)
        print(torch.norm(b, float('inf'))) # output: tensor(15.)
        

        二、torch.normal什么意思

        在PyTorch中,torch.normal函數(shù)用于從給定的均值和標(biāo)準(zhǔn)差中生成指定大小的正態(tài)分布(Gaussian distribution)樣本值的Tensor。

        對(duì)于一個(gè)mxn的Tensor,輸出的Tensor尺寸為mxn的且每個(gè)元素(i,j)都是從N(mean(i,j), std(i,j))中隨機(jī)取樣的值。

        
        import torch
        
        # generate 1D tensor with normal distribution
        torch.manual_seed(0)
        mean = torch.zeros(3)
        std = torch.ones(3)
        normal_samples = torch.normal(mean, std)
        print(normal_samples)    # output: tensor([ 1.5410, -0.2934, -2.1788])
        
        # generate 2D tensor with normal distribution
        torch.manual_seed(0)
        mean = torch.zeros(3, 2)
        std = torch.ones(3, 2)
        normal_samples = torch.normal(mean, std)
        print(normal_samples)    # output: tensor([[ 1.5410, -0.2934],
                                                [-2.8200,  0.5285],
                                                [ 0.5802,  0.5422]])
        

        三、torch.normal函數(shù)

        torch.normal函數(shù)的參數(shù)包括:均值mean和標(biāo)準(zhǔn)差std,以及生成隨機(jī)數(shù)的張量size。

        
        import torch
        
        mean = torch.arange(1., 5.)
        std = torch.arange(1, 2)
        size = (2, 2)
        
        normal_samples = torch.normal(mean, std, size)
        print(normal_samples)    # output: tensor([[ 1.4611,  1.9118],
                                                [ 2.0693,  4.1555]])
        

        四、torch.norm 兩個(gè)張量

        除了norm_type和dim參數(shù),torch.norm還可以針對(duì)兩個(gè)張量進(jìn)行計(jì)算。

        
        import torch
        
        a = torch.tensor([1, 2, 3])
        b = torch.tensor([4, 5, 6])
        
        # calculate L2 norm of two tensors
        print(torch.norm(a-b, p=2))    # output: tensor(5.1962)
        
        # calculate Frobenius norm of two matrices
        c = torch.tensor([[1, 2], [3, 4]])
        d = torch.tensor([[5, 6], [7, 8]])
        print(torch.norm(c-d))    # output: tensor(8.0000)
        

        五、小結(jié)

        在本文中,我們學(xué)習(xí)了如何使用torch.norm函數(shù)來(lái)計(jì)算張量的范數(shù)并了解了其常用的參數(shù)norm_type和dim。此外,我們還介紹了torch.normal函數(shù)用于從正態(tài)分布中生成隨機(jī)數(shù)據(jù)的方法。

        tags: torch.normal
        聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
        10年以上業(yè)內(nèi)強(qiáng)師集結(jié),手把手帶你蛻變精英
        請(qǐng)您保持通訊暢通,專屬學(xué)習(xí)老師24小時(shí)內(nèi)將與您1V1溝通
        免費(fèi)領(lǐng)取
        今日已有369人領(lǐng)取成功
        劉同學(xué) 138****2860 剛剛成功領(lǐng)取
        王同學(xué) 131****2015 剛剛成功領(lǐng)取
        張同學(xué) 133****4652 剛剛成功領(lǐng)取
        李同學(xué) 135****8607 剛剛成功領(lǐng)取
        楊同學(xué) 132****5667 剛剛成功領(lǐng)取
        岳同學(xué) 134****6652 剛剛成功領(lǐng)取
        梁同學(xué) 157****2950 剛剛成功領(lǐng)取
        劉同學(xué) 189****1015 剛剛成功領(lǐng)取
        張同學(xué) 155****4678 剛剛成功領(lǐng)取
        鄒同學(xué) 139****2907 剛剛成功領(lǐng)取
        董同學(xué) 138****2867 剛剛成功領(lǐng)取
        周同學(xué) 136****3602 剛剛成功領(lǐng)取
        相關(guān)推薦HOT
        高雄市| 理塘县| 九江县| 紫阳县| 麻江县| 从江县| 高碑店市| 惠水县| 视频| 贵德县| 满洲里市| 鄯善县| 金平| 逊克县| 呼伦贝尔市| 即墨市| 东乌| 南部县| 安阳县| 林口县| 嘉禾县| 疏勒县| 迁西县| 厦门市| 个旧市| 鹤庆县| 富顺县| 富源县| 凯里市| 新邵县| 隆林| 江山市| 怀化市| 眉山市| 青铜峡市| 涪陵区| 普兰店市| 章丘市| 高雄县| 宣威市| 罗山县|