欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

one_hot編碼定義

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

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

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

eg:

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

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

[0, 1, 0, 0]

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

[0, 0, 1, 0]

eg:

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

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

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

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

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

eg:

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

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

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

[0, 0, 0, 0, 1]

當(dāng)前向量中值為1對(duì)應(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) ? # 默認(rèn)按照targets其中的最大值+1作為one_hot編碼的長(zhǎng)度
# 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編碼長(zhǎng)度為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編碼主要用于分類時(shí),作為一個(gè)類別的編碼–方便判別與相關(guān)計(jì)算;

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

2. 相比于預(yù)測(cè)出具體的類別數(shù)–43等,用向量可以使用向量相關(guān)的算法進(jìn)行時(shí)間上的優(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() 實(shí)現(xiàn)類型的轉(zhuǎn)化

One_hot編碼表示

def one_hot(y):
? ? '''
? ? y: (N)的一維tensor,值為每個(gè)樣本的類別
? ? 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) ?# 等價(jià)于上面
? ? y_onehot.scatter_(1, y, 1)
? ? return y_onehot

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

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

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

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

expand()函數(shù)

這個(gè)函數(shù)的作用就是對(duì)指定的維度進(jìn)行數(shù)值大小的改變。只能改變維大小為1的維,否則就會(huì)報(bào)錯(cuò)。不改變的維可以傳入-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ù)

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

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é)

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

相關(guān)文章

最新評(píng)論