網(wǎng)站首頁 編程語言 正文
?torch.flatten(x)等于torch.flatten(x,0)默認(rèn)將張量拉成一維的向量,也就是說從第一維開始平坦化,torch.flatten(x,1)代表從第二維開始平坦化。
import torch x=torch.randn(2,4,2) print(x) z=torch.flatten(x) print(z) w=torch.flatten(x,1) print(w) 輸出為: tensor([[[-0.9814, 0.8251], [ 0.8197, -1.0426], [-0.8185, -1.3367], [-0.6293, 0.6714]], [[-0.5973, -0.0944], [ 0.3720, 0.0672], [ 0.2681, 1.8025], [-0.0606, 0.4855]]]) tensor([-0.9814, 0.8251, 0.8197, -1.0426, -0.8185, -1.3367, -0.6293, 0.6714, -0.5973, -0.0944, 0.3720, 0.0672, 0.2681, 1.8025, -0.0606, 0.4855]) tensor([[-0.9814, 0.8251, 0.8197, -1.0426, -0.8185, -1.3367, -0.6293, 0.6714] , [-0.5973, -0.0944, 0.3720, 0.0672, 0.2681, 1.8025, -0.0606, 0.4855] ])
?torch.flatten(x,0,1)代表在第一維和第二維之間平坦化
import torch x=torch.randn(2,4,2) print(x) w=torch.flatten(x,0,1) #第一維長度2,第二維長度為4,平坦化后長度為2*4 print(w.shape) print(w) 輸出為: tensor([[[-0.5523, -0.1132], [-2.2659, -0.0316], [ 0.1372, -0.8486], [-0.3593, -0.2622]], [[-0.9130, 1.0038], [-0.3996, 0.4934], [ 1.7269, 0.8215], [ 0.1207, -0.9590]]]) torch.Size([8, 2]) tensor([[-0.5523, -0.1132], [-2.2659, -0.0316], [ 0.1372, -0.8486], [-0.3593, -0.2622], [-0.9130, 1.0038], [-0.3996, 0.4934], [ 1.7269, 0.8215], [ 0.1207, -0.9590]])
對于torch.nn.Flatten(),因?yàn)槠浔挥迷谏窠?jīng)網(wǎng)絡(luò)中,輸入為一批數(shù)據(jù),第一維為batch,通常要把一個(gè)數(shù)據(jù)拉成一維,而不是將一批數(shù)據(jù)拉為一維。所以torch.nn.Flatten()默認(rèn)從第二維開始平坦化。
import torch #隨機(jī)32個(gè)通道為1的5*5的圖 x=torch.randn(32,1,5,5) model=torch.nn.Sequential( #輸入通道為1,輸出通道為6,3*3的卷積核,步長為1,padding=1 torch.nn.Conv2d(1,6,3,1,1), torch.nn.Flatten() ) output=model(x) print(output.shape) # 6*(7-3+1)*(7-3+1) 輸出為: torch.Size([32, 150])
總結(jié)
原文鏈接:https://blog.csdn.net/Super_user_and_woner/article/details/120782656
相關(guān)推薦
- 2022-08-07 pandas中pd.groupby()的用法詳解_python
- 2022-12-11 Django切換數(shù)據(jù)庫和遷移數(shù)據(jù)詳解_python
- 2022-07-02 react+typescript使用window.ethereum報(bào)錯(cuò)
- 2022-05-25 C語言三個(gè)函數(shù)的模擬實(shí)現(xiàn)詳解_C 語言
- 2022-07-01 Android?TextView跑馬燈實(shí)現(xiàn)原理及方法實(shí)例_Android
- 2022-06-25 C++鏈表類的封裝詳情介紹_C 語言
- 2022-05-01 Python類的定義和使用詳情_python
- 2022-06-29 python人工智能tensorflow函數(shù)tf.get_variable使用方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- 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)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支