本文主要是介绍Pytorch中高维度张量理解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Pytorch中高维度张量理解
- 创建一个tensor
- 获取第一个维度的第0个元素:
- 获取第二个维度的第0个元素:
- 获取第三个维度的第0个元素:
- 获取第四个维度的第0个元素:
- 其他情况
创建一个tensor
tensor = torch.rand(3,5,3,2)
结果如下:
```python
tensor([[[[0.3844, 0.9532],[0.0787, 0.4187],[0.4144, 0.9552]],[[0.0713, 0.5281],[0.0230, 0.8433],[0.1113, 0.5927]],[[0.0040, 0.1001],[0.3837, 0.6088],[0.1752, 0.3184]],[[0.2762, 0.8417],[0.5438, 0.4406],[0.0529, 0.5175]],[[0.1038, 0.7948],[0.4991, 0.5155],[0.4651, 0.8095]]],[[[0.0377, 0.0249],[0.2440, 0.8501],[0.1176, 0.7303]],[[0.9979, 0.6738],[0.2486, 0.4152],[0.5896, 0.8879]],[[0.3499, 0.6918],[0.4399, 0.5192],[0.1783, 0.5962]],[[0.3021, 0.4297],[0.9558, 0.0046],[0.9994, 0.1249]],[[0.8348, 0.7249],[0.1525, 0.3867],[0.8992, 0.6996]]],[[[0.5918, 0.9135],[0.8205, 0.5719],[0.8127, 0.3856]],[[0.1870, 0.6190],[0.2991, 0.9424],[0.5405, 0.4200]],[[0.9396, 0.8072],[0.0319, 0.6586],[0.4849, 0.6193]],[[0.5268, 0.2794],[0.7877, 0.9502],[0.6553, 0.9574]],[[0.4079, 0.4648],[0.6375, 0.8829],[0.6280, 0.1463]]]])
现在我想获取
tensor[0,0,0,0]
获取第一个维度的第0个元素:
[[[0.3844, 0.9532],[0.0787, 0.4187],[0.4144, 0.9552]],[[0.0713, 0.5281],[0.0230, 0.8433],[0.1113, 0.5927]],[[0.0040, 0.1001],[0.3837, 0.6088],[0.1752, 0.3184]],[[0.2762, 0.8417],[0.5438, 0.4406],[0.0529, 0.5175]],[[0.1038, 0.7948],[0.4991, 0.5155],[0.4651, 0.8095]]]
获取第二个维度的第0个元素:
[[0.3844, 0.9532],[0.0787, 0.4187],[0.4144, 0.9552]]
获取第三个维度的第0个元素:
[0.3844, 0.9532]
获取第四个维度的第0个元素:
0.3844
其他情况
tensor[-1]
获取第1个维度的最后一个元素:
[[[0.5918, 0.9135],[0.8205, 0.5719],[0.8127, 0.3856]],[[0.1870, 0.6190],[0.2991, 0.9424],[0.5405, 0.4200]],[[0.9396, 0.8072],[0.0319, 0.6586],[0.4849, 0.6193]],[[0.5268, 0.2794],[0.7877, 0.9502],[0.6553, 0.9574]],[[0.4079, 0.4648],[0.6375, 0.8829],[0.6280, 0.1463]]]
tensor[0,1]
获取第1个维度的第0个元素 :
[[[0.3844, 0.9532],[0.0787, 0.4187],[0.4144, 0.9552]],[[0.0713, 0.5281],[0.0230, 0.8433],[0.1113, 0.5927]],[[0.0040, 0.1001],[0.3837, 0.6088],[0.1752, 0.3184]],[[0.2762, 0.8417],[0.5438, 0.4406],[0.0529, 0.5175]],[[0.1038, 0.7948],[0.4991, 0.5155],[0.4651, 0.8095]]]
第2个维度的第1个元素:
[[0.0713, 0.5281],[0.0230, 0.8433],[0.1113, 0.5927]]
tensor[:,1,0,1]
获取第1个维度的所有元素:
[[[0.3844, 0.9532],[0.0787, 0.4187],[0.4144, 0.9552]],[[0.0713, 0.5281],[0.0230, 0.8433],[0.1113, 0.5927]],[[0.0040, 0.1001],[0.3837, 0.6088],[0.1752, 0.3184]],[[0.2762, 0.8417],[0.5438, 0.4406],[0.0529, 0.5175]],[[0.1038, 0.7948],[0.4991, 0.5155],[0.4651, 0.8095]]],[[[0.0377, 0.0249],[0.2440, 0.8501],[0.1176, 0.7303]],[[0.9979, 0.6738],[0.2486, 0.4152],[0.5896, 0.8879]],[[0.3499, 0.6918],[0.4399, 0.5192],[0.1783, 0.5962]],[[0.3021, 0.4297],[0.9558, 0.0046],[0.9994, 0.1249]],[[0.8348, 0.7249],[0.1525, 0.3867],[0.8992, 0.6996]]],[[[0.5918, 0.9135],[0.8205, 0.5719],[0.8127, 0.3856]],[[0.1870, 0.6190],[0.2991, 0.9424],[0.5405, 0.4200]],[[0.9396, 0.8072],[0.0319, 0.6586],[0.4849, 0.6193]],[[0.5268, 0.2794],[0.7877, 0.9502],[0.6553, 0.9574]],[[0.4079, 0.4648],[0.6375, 0.8829],[0.6280, 0.1463]]]
第2个维度的第1个元素:
[[0.0713, 0.5281],[0.0230, 0.8433],[0.1113, 0.5927]][[0.9979, 0.6738],[0.2486, 0.4152],[0.5896, 0.8879]][[0.1870, 0.6190],[0.2991, 0.9424],[0.5405, 0.4200]]
第3个维度的第0个元素:
[0.0713, 0.5281][0.9979, 0.6738][0.1870, 0.6190]
第4个维度的第1个元素:
0.52810.67380.6190
最终结果:
tensor([0.5281, 0.6738, 0.6190])
这篇关于Pytorch中高维度张量理解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!