網站首頁 編程語言 正文
一、普通用法 (手動調整size)
view()相當于reshape、resize,重新調整Tensor的形狀。
import torch a1 = torch.arange(0,16) print(a1) # tensor([ 0, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, 10, 11, 12, 13, 14, 15])
a2 = a1.view(8, 2) a3 = a1.view(2, 8) a4 = a1.view(4, 4) print(a2) #tensor([[ 0, ?1], # ? ? ? ?[ 2, ?3], # ? ? ? ?[ 4, ?5], # ? ? ? ?[ 6, ?7], # ? ? ? ?[ 8, ?9], # ? ? ? ?[10, 11], # ? ? ? ?[12, 13], # ? ? ? ?[14, 15]]) print(a3) #tensor([[ 0, ?1, ?2, ?3, ?4, ?5, ?6, ?7], # ? ? ? ?[ 8, ?9, 10, 11, 12, 13, 14, 15]]) print(a4) #tensor([[ 0, ?1, ?2, ?3], # ? ? ? ?[ 4, ?5, ?6, ?7], # ? ? ? ?[ 8, ?9, 10, 11], # ? ? ? ?[12, 13, 14, 15]])
二、特殊用法:參數-1 (自動調整size)
view中一個參數定為-1,代表自動調整這個維度上的元素個數,以保證元素的總數不變。
v1 = torch.arange(0,16) print(v1) # tensor([ 0, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, 10, 11, 12, 13, 14, 15]) v2 = v1.view(-1, 16) v2 # tensor([[ 0, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, 10, 11, 12, 13, 14, 15]]) v2 = v1.view(-1, 8) v2 # tensor([[ 0, ?1, ?2, ?3, ?4, ?5, ?6, ?7], # ? ? ? ? [ 8, ?9, 10, 11, 12, 13, 14, 15]]) v2 = v1.view(-1, 4) v2 #tensor([[ 0, ?1, ?2, ?3], # ? ? ? ?[ 4, ?5, ?6, ?7], # ? ? ? ?[ 8, ?9, 10, 11], # ? ? ? ?[12, 13, 14, 15]]) v2 = v1.view(-1, 2) v2 #tensor([[ 0, ?1], # ? ? ? ?[ 2, ?3], # ? ? ? ?[ 4, ?5], # ? ? ? ?[ 6, ?7], # ? ? ? ?[ 8, ?9], # ? ? ? ?[10, 11], # ? ? ? ?[12, 13], # ? ? ? ?[14, 15]])
v3 = v1.view(4*4, -1) v3 # tensor([[ 0], # ? ? ? ? [ 1], # ? ? ? ? [ 2], # ? ? ? ? [ 3], # ? ? ? ? [ 4], # ? ? ? ? [ 5], # ? ? ? ? [ 6], # ? ? ? ? [ 7], # ? ? ? ? [ 8], # ? ? ? ? [ 9], # ? ? ? ? [10], # ? ? ? ? [11], # ? ? ? ? [12], # ? ? ? ? [13], # ? ? ? ? [14], # ? ? ? ? [15]])
原文鏈接:https://blog.csdn.net/qq_36998053/article/details/123528858
相關推薦
- 2021-12-15 C語言數據結構與算法之圖的遍歷(二)_C 語言
- 2023-04-20 文本超出顯示省略號在項目中的使用
- 2023-07-13 css對盒模型的理解
- 2022-05-12 Echarts x軸標簽太長解決方案
- 2022-04-17 論一次 taro小程序分包優化經歷,小程序體積過大的優化
- 2022-09-16 Go語言實現UDP協議及TCP通訊_Golang
- 2022-04-14 Python中字典的相關操作介紹_python
- 2022-03-16 C#?程序通用結構_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支