PyTorch中tensor[..., 2:4]的實(shí)現(xiàn)示例
1. 動(dòng)機(jī)
在看YOLO v3-SPP源碼時(shí),看到tensor[..., a: b]的切片方式比較新奇,接下來(lái)進(jìn)行分析:
p = p.view(bs, self.na, self.no, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction
if self.training:
return p
p = p.view(m, self.no)
p[:, :2] = (torch.sigmoid(p[:, 0:2]) + grid) * ng # x, y
p[:, 2:4] = torch.exp(p[:, 2:4]) * anchor_wh # width, height
p[:, 4:] = torch.sigmoid(p[:, 4:])
p[:, 5:] = p[:, 5:self.no] * p[:, 4:5]
return p
2. 分析問(wèn)題
2.1list數(shù)組使用[..., a:b]方式切片
list_simple = [1, 2, 3, 4, 5]
list_complex = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
# 對(duì)list數(shù)組進(jìn)行切片
try: # ① list_simple
print(f"...: {list_simple[..., 2:4]}")
except Exception as e:
print(f"a_list_simple切片報(bào)錯(cuò),錯(cuò)誤為: {e}")
try: # ② list_complex
print(f"...: {list_complex[..., 2:4]}")
except Exception as e:
print(f"a_list_complex切片報(bào)錯(cuò),錯(cuò)誤為: {e}")
"""
a_list_simple切片報(bào)錯(cuò),錯(cuò)誤為: list indices must be integers or slices, not tuple
a_list_complex切片報(bào)錯(cuò),錯(cuò)誤為: list indices must be integers or slices, not tuple
"""
很明顯,Python的基礎(chǔ)數(shù)據(jù)類型list并不支持這樣的切片方式。
2.2 numpyarray使用[..., a:b]方式切片
import numpy as np
numpy_simple = np.array([1, 2, 3, 4, 5])
numpy_complex = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
print(f"numpy_simple: \n{numpy_simple}")
print(f"numpy_complex: \n{numpy_complex}")
print("\n-------------------------\n")
# 對(duì)numpy array進(jìn)行切片
try: # ① list_simple
print(f"...切片沒(méi)有報(bào)錯(cuò),結(jié)果為: {numpy_simple[..., 2:4]}")
except Exception as e:
print(f"numpy_simple切片報(bào)錯(cuò),錯(cuò)誤為: {e}")
try: # ② list_complex
print(f"...切片沒(méi)有報(bào)錯(cuò),結(jié)果為: {numpy_complex[..., 2:4]}")
except Exception as e:
print(f"numpy_complex切片報(bào)錯(cuò),錯(cuò)誤為: {e}")
"""
numpy_simple:
[1 2 3 4 5]
numpy_complex:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
-------------------------
...切片沒(méi)有報(bào)錯(cuò),結(jié)果為: [3 4]
...切片沒(méi)有報(bào)錯(cuò),結(jié)果為: [[ 3]
[ 6]
[ 9]
[12]]
"""
說(shuō)明使用[..., a:b]方式是可以對(duì)numpy array進(jìn)行切片的。
2.3 PyTorchtensor使用[..., a:b]方式切片
我們直接創(chuàng)建一個(gè)tensor進(jìn)行分析:
import torch
a = torch.rand([3, 112, 112])
print(f"...: {a[..., :2].shape}") # ...: torch.Size([3, 112, 2])
可以看到[...,a:b]中的...表示前n-1個(gè)維度,a:b表示直接對(duì)最后一個(gè)維度進(jìn)行切片
3. 總結(jié)
[..., a:b]是array/tensor特有的切片方式,表示直接對(duì)最后一個(gè)維度進(jìn)行切片。
到此這篇關(guān)于PyTorch中tensor[..., 2:4]的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)PyTorch tensor[..., 2:4]內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用PyTorch/TensorFlow搭建簡(jiǎn)單全連接神經(jīng)網(wǎng)絡(luò)
- PyTorch使用教程之Tensor包詳解
- 最新tensorflow與pytorch環(huán)境搭建的實(shí)現(xiàn)步驟
- pytorch?tensor合并與分割方式
- Pytorch實(shí)現(xiàn)tensor序列化和并行化的示例詳解
- PyTorch?TensorFlow機(jī)器學(xué)習(xí)框架選擇實(shí)戰(zhàn)
- pytorch中tensorboard安裝及安裝過(guò)程中出現(xiàn)的常見(jiàn)錯(cuò)誤問(wèn)題
- Pytorch之tensorboard無(wú)法啟動(dòng)和顯示問(wèn)題及解決
- Pytorch Dataset,TensorDataset,Dataloader,Sampler關(guān)系解讀
相關(guān)文章
基于Python編寫簡(jiǎn)單的網(wǎng)絡(luò)測(cè)試工具
這篇文章主要為大家詳細(xì)介紹了如何基于Python編寫一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)測(cè)試工具,可以測(cè)試網(wǎng)絡(luò)的下載速度,上傳速度和延遲,感興趣的可以了解下2025-02-02
Python3爬蟲ChromeDriver的安裝實(shí)例
在本篇文章里小編給大家整理的是一篇關(guān)于Python3爬蟲ChromeDriver的安裝實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-02-02
Python編程實(shí)現(xiàn)的圖片識(shí)別功能示例
這篇文章主要介紹了Python編程實(shí)現(xiàn)的圖片識(shí)別功能,涉及Python PIL模塊的安裝與使用技巧,需要的朋友可以參考下2017-08-08
Python實(shí)現(xiàn)網(wǎng)頁(yè)截圖(PyQT5)過(guò)程解析
這篇文章主要介紹了Python實(shí)現(xiàn)網(wǎng)頁(yè)截圖(PyQT5)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Python的Flask框架中集成CKeditor富文本編輯器的教程
在用Flask搭建網(wǎng)站時(shí)的后臺(tái)文章編輯器可以使用CKeditor,CKeditor所支持的文本樣式較多且開(kāi)源,這里我們就來(lái)看一下Python的Flask框架中集成CKeditor富文本編輯器的教程2016-06-06
python設(shè)計(jì)tcp數(shù)據(jù)包協(xié)議類的例子
今天小編就為大家分享一篇python設(shè)計(jì)tcp數(shù)據(jù)包協(xié)議類的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
Pytorch損失函數(shù)nn.NLLLoss2d()用法說(shuō)明
這篇文章主要介紹了Pytorch損失函數(shù)nn.NLLLoss2d()用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Python學(xué)習(xí)筆記之變量與轉(zhuǎn)義符
這篇文章主要介紹了Python學(xué)習(xí)筆記之變量與轉(zhuǎn)義符,本文從零開(kāi)始學(xué)習(xí)Python,知識(shí)點(diǎn)很細(xì),有共同目標(biāo)的小伙伴可以一起來(lái)學(xué)習(xí)2023-03-03

