麻豆黑色丝袜jk制服福利网站-麻豆精品传媒视频观看-麻豆精品传媒一二三区在线视频-麻豆精选传媒4区2021-在线视频99-在线视频a

千鋒教育-做有情懷、有良心、有品質的職業教育機構

手機站
千鋒教育

千鋒學習站 | 隨時隨地免費學

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

關注千鋒學習站小程序
隨時隨地免費學習課程

當前位置:首頁  >  技術干貨  > torch.norm函數詳解

torch.norm函數詳解

來源:千鋒教育
發布人:xqq
時間: 2023-11-23 15:06:51 1700723211

一、torch.norm函數

torch.norm函數是PyTorch庫中的一個標量計算函數,用于在給定維度上計算輸入張量的范數(also known as vector length or magnitude)。

常用的張量范數有L1范數和L2范數,torch.norm默認使用L2范數,可以通過norm_type參數指定L1范數。


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函數用于從給定的均值和標準差中生成指定大小的正態分布(Gaussian distribution)樣本值的Tensor。

對于一個mxn的Tensor,輸出的Tensor尺寸為mxn的且每個元素(i,j)都是從N(mean(i,j), std(i,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函數

torch.normal函數的參數包括:均值mean和標準差std,以及生成隨機數的張量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 兩個張量

除了norm_type和dim參數,torch.norm還可以針對兩個張量進行計算。


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)

五、小結

在本文中,我們學習了如何使用torch.norm函數來計算張量的范數并了解了其常用的參數norm_type和dim。此外,我們還介紹了torch.normal函數用于從正態分布中生成隨機數據的方法。

tags: torch.normal
聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
10年以上業內強師集結,手把手帶你蛻變精英
請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
免費領取
今日已有369人領取成功
劉同學 138****2860 剛剛成功領取
王同學 131****2015 剛剛成功領取
張同學 133****4652 剛剛成功領取
李同學 135****8607 剛剛成功領取
楊同學 132****5667 剛剛成功領取
岳同學 134****6652 剛剛成功領取
梁同學 157****2950 剛剛成功領取
劉同學 189****1015 剛剛成功領取
張同學 155****4678 剛剛成功領取
鄒同學 139****2907 剛剛成功領取
董同學 138****2867 剛剛成功領取
周同學 136****3602 剛剛成功領取
相關推薦HOT
主站蜘蛛池模板: 女偶像私下的y荡生活| 三上悠亚国产精品一区| 国产深夜福利在线观看网站| 色片免费观看| 再深点灬舒服灬在快点视频| 嗯啊公交车上被群j| 波多野吉衣一区二区| 星空无限传媒好闺蜜2| 波多野结衣护士系列播放| 丁香狠狠色婷婷久久综合| 动漫美女和男人羞羞漫画| 波多野结衣最新电影| 黑人在线观看| yy6080理论午夜一级毛片| 好骚导航| 日本漂亮继坶中文字幕| 性高湖久久久久久久久aaaaa| 波多野结衣bt| 国产私拍视频| 日本三级在线观看免费| 国色天香精品一卡2卡3卡| 狠狠色综合网站久久久久久久| 精品国产欧美一区二区| 成年女人黄小视频| 国产乱了真实在线观看| 亚洲蜜芽在线精品一区| 好男人www社区| 成人免费福利电影| 色播影院性播免费看| 爽爽日本在线视频免费| 美女扒开屁股让男人桶| 特级大片| 欧美伦理三级在线播放影院| 免费a级毛片无码| 玖玖99视频| 欧美www视频| 8av国产精品爽爽ⅴa在线观看| 亚洲国产高清美女在线观看| 色婷婷在线视频| 男人女人做30分爽爽视频| 天堂在线影院|