Pytorch中高维度张量理解

Pytorch中高维度张量理解

创建一个tensor

python 复制代码
tensor = torch.rand(3,5,3,2)

结果如下:

python 复制代码
```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]]]])

现在我想获取

python 复制代码
tensor[0,0,0,0]

获取第一个维度的第0个元素:

python 复制代码
		[[[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个元素:

python 复制代码
		[[0.3844, 0.9532],
		  [0.0787, 0.4187],
		  [0.4144, 0.9552]]

获取第三个维度的第0个元素:

python 复制代码
		[0.3844, 0.9532]

获取第四个维度的第0个元素:

python 复制代码
		0.3844

其他情况

tensor[-1]

获取第1个维度的最后一个元素:

python 复制代码
		[[[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个元素 :

python 复制代码
		[[[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个元素:

python 复制代码
 		[[0.0713, 0.5281],
          [0.0230, 0.8433],
          [0.1113, 0.5927]]

tensor[:,1,0,1]

获取第1个维度的所有元素:

python 复制代码
		[[[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个元素:

python 复制代码
 		[[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个元素:

python 复制代码
		[0.0713, 0.5281]
		[0.9979, 0.6738]
		[0.1870, 0.6190]

第4个维度的第1个元素:

python 复制代码
		 0.5281
		 0.6738
		 0.6190

最终结果:

python 复制代码
tensor([0.5281, 0.6738, 0.6190])
相关推荐
靴子学长27 分钟前
基于字节大模型的论文翻译(含免费源码)
人工智能·深度学习·nlp
梧桐树04291 小时前
python常用内建模块:collections
python
AI_NEW_COME1 小时前
知识库管理系统可扩展性深度测评
人工智能
Dream_Snowar1 小时前
速通Python 第三节
开发语言·python
海棠AI实验室2 小时前
AI的进阶之路:从机器学习到深度学习的演变(一)
人工智能·深度学习·机器学习
hunteritself2 小时前
AI Weekly『12月16-22日』:OpenAI公布o3,谷歌发布首个推理模型,GitHub Copilot免费版上线!
人工智能·gpt·chatgpt·github·openai·copilot
IT古董3 小时前
【机器学习】机器学习的基本分类-强化学习-策略梯度(Policy Gradient,PG)
人工智能·机器学习·分类
centurysee3 小时前
【最佳实践】Anthropic:Agentic系统实践案例
人工智能
mahuifa3 小时前
混合开发环境---使用编程AI辅助开发Qt
人工智能·vscode·qt·qtcreator·编程ai
四口鲸鱼爱吃盐3 小时前
Pytorch | 从零构建GoogleNet对CIFAR10进行分类
人工智能·pytorch·分类