網(wǎng)站首頁 編程語言 正文
Pytorch one_hot編碼函數(shù)解讀
one_hot編碼定義
在一個(gè)給定的向量中,按照設(shè)定的最值–可以是向量中包含的最大值(作為最高分類數(shù)),有也可以是自定義的最大值,設(shè)計(jì)one_hot編碼的長度:最大值+1【詳見舉的例子吧】。
然后按照最大值創(chuàng)建一個(gè)1*(最大值+1)的維度大小的全零零向量:[0, 0, 0, …] => 共最大值+1對應(yīng)的個(gè)數(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(等價(jià)最大值+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) ? # 默認(rèn)按照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編碼主要用于分類時(shí),作為一個(gè)類別的編碼–方便判別與相關(guān)計(jì)算;
1. 如同類別數(shù)統(tǒng)計(jì),只需要將one_hot編碼相加得到一個(gè)一維向量就知道了一批數(shù)據(jù)中所有類別的預(yù)測或真實(shí)的分布情況;
2. 相比于預(yù)測出具體的類別數(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ù)的作用就是對指定的維度進(jìn)行數(shù)值大小的改變。只能改變維大小為1的維,否則就會報(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ù)
沿著指定的維度,對原來的tensor進(jìn)行數(shù)據(jù)復(fù)制。這個(gè)函數(shù)和expand()還是有點(diǎn)區(qū)別的。expand()只能對維度為1的維進(jìn)行擴(kuò)大,而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é)
原文鏈接:https://blog.csdn.net/weixin_44604887/article/details/109523281
相關(guān)推薦
- 2022-04-24 Postman設(shè)置環(huán)境變量的實(shí)現(xiàn)示例_相關(guān)技巧
- 2022-12-24 C++中析構(gòu)函數(shù)為何是虛函數(shù)_C 語言
- 2024-03-18 為什么SpringBoot的jar可以直接運(yùn)行?
- 2022-11-10 react-native?父函數(shù)組件調(diào)用類子組件的方法(實(shí)例詳解)_React
- 2022-01-20 空值判斷運(yùn)算符 ? ?
- 2021-12-15 C/C++?Qt?數(shù)據(jù)庫與Chart歷史數(shù)據(jù)展示_C 語言
- 2022-08-01 flask上使用websocket的方法示例_python
- 2022-06-19 Rainbond使用Dockerfile構(gòu)建便捷應(yīng)用運(yùn)行流程_云其它
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支