網(wǎng)站首頁 編程語言 正文
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) #標(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ù)
這個函數(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é)
原文鏈接:https://blog.csdn.net/weixin_44604887/article/details/109523281
相關(guān)推薦
- 2022-10-03 go?Antlr重構(gòu)腳本解釋器實現(xiàn)示例_Golang
- 2023-10-14 ORACLE存在就修改 不存在就新增(注意更新和新增語法不同于常規(guī)語法)
- 2022-04-26 Python中Enum使用的幾點注意事項_python
- 2022-10-18 Go項目怎么使用枚舉_Golang
- 2022-04-07 Android中絕對音量和相對音量設(shè)置_Android
- 2022-03-03 關(guān)于使用iview table 組件中使用 tooltip 樣式覆蓋的問題
- 2022-11-02 Python嵌套函數(shù)與nonlocal使用詳細介紹_python
- 2022-03-25 Redis分布式鎖如何實現(xiàn)續(xù)期_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(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)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支