torch 的 unsqueeze用法
用法 torch
2023-09-14 09:15:52 时间
import torch
a=torch.tensor([[3.0000, 3.0000],
[3.0000, 4.0000],
[3.6000, 3.0000],
[3.5000, 3.0000]])
个人以为这个unsqueeze方法有点废话,完全可以用reshape 实现,鸡肋!
a.reshape([a.shape[0],1,a.shape[1]])==a.unsqueeze(1)
a.reshape([1,a.shape[0],1,a.shape[1]])==a.unsqueeze(0)
a.reshape([a.shape[0],a.shape[1] ,1])==a.unsqueeze(2)
a.reshape([a.shape[0],1,a.shape[1]])==a.unsqueeze(1)
'''
0
m ,n------> 1,m,n
1
m ,n------> m,1,n
2
m ,n------> m,n,2
k
m ,.....,n------> m,...,k,k+1,n
'''
print('a=',a)
print('a.shape=',a.shape)
print('-'*30+'分割线'+'-'*30)
print('a.unsqueeze(0).shape=',a.unsqueeze(0).shape)
print('a.unsqueeze(0)=',a.unsqueeze(0))
print('-'*30+'分割线'+'-'*30)
print('a.unsqueeze(1).shape=',a.unsqueeze(1).shape)
print('a.unsqueeze(1)=',a.unsqueeze(1))
print('-'*30+'分割线'+'-'*30)
print('a.unsqueeze(2).shape=',a.unsqueeze(2).shape)
print('a.unsqueeze(2)=',a.unsqueeze(2))
在这里是一个二位矩阵 可以在[0,1,2]三个维度上
a= tensor([[3.0000, 3.0000],
[3.0000, 4.0000],
[3.6000, 3.0000],
[3.5000, 3.0000]])
a.shape= torch.Size([4, 2])
------------------------------分割线------------------------------
a.unsqueeze(0).shape= torch.Size([1, 4, 2])
a.unsqueeze(0)= tensor([[[3.0000, 3.0000],
[3.0000, 4.0000],
[3.6000, 3.0000],
[3.5000, 3.0000]]])
------------------------------分割线------------------------------
a.unsqueeze(1).shape= torch.Size([4, 1, 2])
a.unsqueeze(1)= tensor([[[3.0000, 3.0000]],
[[3.0000, 4.0000]],
[[3.6000, 3.0000]],
[[3.5000, 3.0000]]])
------------------------------分割线------------------------------
a.unsqueeze(2).shape= torch.Size([4, 2, 1])
a.unsqueeze(2)= tensor([[[3.0000],
[3.0000]],
[[3.0000],
[4.0000]],
[[3.6000],
[3.0000]],
[[3.5000],
[3.0000]]])
一个是顺序,一个逆序,形成一个闭环
'''
-3 -2 -1
0 1 2
'''
print('a.unsqueeze(-3)==a.unsqueeze(0)',a.unsqueeze(-3)==a.unsqueeze(0))
print('a.unsqueeze(-2)==a.unsqueeze(1)',a.unsqueeze(-2)==a.unsqueeze(1))
print('a.unsqueeze(-1)==a.unsqueeze(2)',a.unsqueeze(-1)==a.unsqueeze(2))
a.unsqueeze(-3)==a.unsqueeze(0) tensor([[[True, True],
[True, True],
[True, True],
[True, True]]])
a.unsqueeze(-2)==a.unsqueeze(1) tensor([[[True, True]],
[[True, True]],
[[True, True]],
[[True, True]]])
a.unsqueeze(-1)==a.unsqueeze(2) tensor([[[True],
[True]],
[[True],
[True]],
[[True],
[True]],
[[True],
[True]]])
相关文章
- mysql timestampdiff>_MySQL TIMESTAMPDIFF()用法及代码示例
- jquery.tmpl 基础用法
- js indexOf()用法
- c语言中位运算符_位运算符的用法
- C++ const用法详解
- MySQL查询技巧:左连接查询的用法详解(mysql左连接查询)
- MySQL中的h参数了解其作用和用法(mysql中-h)
- 学习mysqlping函数的用法,提高MySQL数据库连接可靠性(mysql_ping用法)
- Redis的订阅功能演示(redis订阅用法)
- C#时间格式化(Datetime)用法详解
- Js中setTimeout()和setInterval()何时被调用执行的用法
- c语言中static的用法详细示例分析
- Jar打包用法详解