免费可以看的无遮挡av无码|国产在线拍揄自揄视频网站|在线无码精品视频播放在|欧美亚洲国产成人精品,国产成人久久77777精品,亚洲欧美视频在线观看,色偷偷色噜噜狠狠网站久久

千鋒教育-做有情懷、有良心、有品質(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