亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Pytorch框架之one_hot編碼函數(shù)解讀

 更新時間:2023年02月01日 09:28:42   作者:NULL not error  
這篇文章主要介紹了Pytorch框架之one_hot編碼函數(shù)解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Pytorch one_hot編碼函數(shù)解讀

one_hot編碼定義

在一個給定的向量中,按照設(shè)定的最值–可以是向量中包含的最大值(作為最高分類數(shù)),有也可以是自定義的最大值,設(shè)計one_hot編碼的長度:最大值+1【詳見舉的例子吧】。

然后按照最大值創(chuàng)建一個1*(最大值+1)的維度大小的全零零向量:[0, 0, 0, …] => 共最大值+1對應(yīng)的個數(shù)

接著按照向量中的值,從第0位開始索引,將向量中值對應(yīng)的位置設(shè)置為1,其他保持為0.

eg:

假設(shè)設(shè)定one_hot長度為4(最大值) –

且當(dāng)前向量中值為1對應(yīng)的one_hot編碼:

[0, 1, 0, 0]

當(dāng)前向量中值為2對應(yīng)的one_hot編碼:

[0, 0, 1, 0]

eg:

假設(shè)設(shè)定one_hot長度為6(等價最大值+1) –

且當(dāng)前向量中值為4對應(yīng)的one_hot編碼:

[0, 0, 0, 0, 1, 0]

當(dāng)前向量中值為2對應(yīng)的one_hot編碼:

[0, 0, 1, 0, 0, 0]

eg:

targets = [4, 1, 0, 3] => max_value=4=>one_hot的長度為(4+1)

假設(shè)設(shè)定one_hot長度為5(最大值) –

且當(dāng)前向量中值為4對應(yīng)的one_hot編碼:

[0, 0, 0, 0, 1]

當(dāng)前向量中值為1對應(yīng)的one_hot編碼:

[0, 1, 0, 0, 0]

Pytorch中one_hot轉(zhuǎn)換

import torch

targets = torch.tensor([5, 3, 2, 1])

targets_to_one_hot = torch.nn.functional.one_hot(targets) ? # 默認按照targets其中的最大值+1作為one_hot編碼的長度
# result:?
# tensor(
# [0, 0, 0, 0, 0, 1],
# [0, 0, 0, 1, 0, 0],
# [0, 0, 1, 0, 0, 0],
# [0, 1, 0, 0, 0, 0]
#)

targets_to_one_hot = torch.nn.functional.one_hot(targets, num_classes=7) ?3# 指定one_hot編碼長度為7
# result:?
# tensor(
# [0, 0, 0, 0, 0, 1, 0],
# [0, 0, 0, 1, 0, 0, 0],
# [0, 0, 1, 0, 0, 0, 0],
# [0, 1, 0, 0, 0, 0, 0]
#)

總結(jié):one_hot編碼主要用于分類時,作為一個類別的編碼–方便判別與相關(guān)計算;

1. 如同類別數(shù)統(tǒng)計,只需要將one_hot編碼相加得到一個一維向量就知道了一批數(shù)據(jù)中所有類別的預(yù)測或真實的分布情況;

2. 相比于預(yù)測出具體的類別數(shù)–43等,用向量可以使用向量相關(guān)的算法進行時間上的優(yōu)化等等

Pytorch變量類型轉(zhuǎn)換及one_hot編碼表示

生成張量

y = torch.empty(3, dtype=torch.long).random_(5)

y = torch.Tensor(2,3).random_(10)

y = torch.randn(3,4).random_(10)

查看類型

y.type
y.dtype

類型轉(zhuǎn)化

tensor.long()/int()/float()
long(),int(),float() 實現(xiàn)類型的轉(zhuǎn)化

One_hot編碼表示

def one_hot(y):
? ? '''
? ? y: (N)的一維tensor,值為每個樣本的類別
? ? out:
? ? ? ? y_onehot: 轉(zhuǎn)換為one_hot 編碼格式
? ? '''
? ? y = y.view(-1, 1)
? ? # y_onehot = torch.FloatTensor(3, 5)
? ? # y_onehot.zero_()

? ? y_onehot = torch.zeros(3,5) ?# 等價于上面
? ? y_onehot.scatter_(1, y, 1)
? ? return y_onehot

y = torch.empty(3, dtype=torch.long).random_(5) #標簽

res = one_hot(y) ?# 轉(zhuǎn)化為One_hot類型

# One_hot類型標簽轉(zhuǎn)化為整數(shù)型列表的兩種方法

h = torch.argmax(res,dim=1)
_,h1 = res.max(dim=1)

expand()函數(shù)

這個函數(shù)的作用就是對指定的維度進行數(shù)值大小的改變。只能改變維大小為1的維,否則就會報錯。不改變的維可以傳入-1或者原來的數(shù)值。

a=torch.randn(1,1,3,768)

print(a.shape) #torch.Size([1, 1, 3, 768])
b=a.expand(2,-1,-1,-1)

print(b.shape) #torch.Size([2, 1, 3, 768])

c=a.expand(2,1,3,768)
print(c.shape) #torch.Size([2, 1, 3, 768])

repeat()函數(shù)

沿著指定的維度,對原來的tensor進行數(shù)據(jù)復(fù)制。這個函數(shù)和expand()還是有點區(qū)別的。expand()只能對維度為1的維進行擴大,而repeat()對所有的維度可以隨意操作。

a=torch.randn(2,1,768)
print(a)
print(a.shape) #torch.Size([2, 1, 768])
b=a.repeat(1,2,1)
print(b)
print(b.shape) #torch.Size([2, 2, 768])
c=a.repeat(3,3,3)
print(c)
print(c.shape) #torch.Size([6, 3, 2304])

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論