文章目录
- list_simple = [1, 2, 3, 4, 5] list_complex = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] # 对list数组进行切片 try: # ① list_simple print(f”…: {list_simple[…, 2:4]}”) except Exception as e: print(f”a_list_simple切片报错,错误为: {e}”) try: # ② list_complex print(f”…: {list_complex[…, 2:4]}”) except Exception as e: print(f”a_list_complex切片报错,错误为: {e}”) “”” a_list_simple切片报错,错误为: list indices must be integers or slices, not tuple a_list_complex切片报错,错误为: list indices must be integers or slices, not tuple “”” 很明显,Python的基础数据类型list并不支持这样的切片方式。
- import numpy as np numpy_simple = np.array([1, 2, 3, 4, 5]) numpy_complex = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) print(f”numpy_simple: n{numpy_simple}”) print(f”numpy_complex: n{numpy_complex}”) print(“n————————-n”) # 对numpy array进行切片 try: # ① list_simple print(f”…切片没有报错,结果为: {numpy_simple[…, 2:4]}”) except Exception as e: print(f”numpy_simple切片报错,错误为: {e}”) try: # ② list_complex print(f”…切片没有报错,结果为: {numpy_complex[…, 2:4]}”) except Exception as e: print(f”numpy_complex切片报错,错误为: {e}”) “”” numpy_simple: [1 2 3 4 5] numpy_complex: [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]] ————————- …切片没有报错,结果为: [3 4] …切片没有报错,结果为: [[ 3] [ 6] [ 9] [12]] “”” 说明使用[…, a:b]方式是可以对numpy array进行切片的。
- 我们直接创建一个tensor进行分析: import torch a = torch.rand([3, 112, 112]) print(f”…: {a[…, :2].shape}”) # …: torch.Size([3, 112, 2]) 可以看到[…,a:b]中的…表示前n-1个维度,a:b表示直接对最后一个维度进行切片
目录
- 1. 动机
- 2. 分析问题
- 2.1list数组使用[…, a:b]方式切片
- 2.2 numpyarray使用[…, a:b]方式切片
- 2.3 PyTorchtensor使用[…, a:b]方式切片
- 3. 总结
在看YOLO v3-SPP源码时,看到tensor[..., a: b]的切片方式比较新奇,接下来进行分析:
p = p.view(bs, self.na, self.no, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction
if self.training:
return p
p = p.view(m, self.no)
p[:, :2] = (torch.sigmoid(p[:, 0:2]) + grid) * ng # x, y
p[:, 2:4] = torch.exp(p[:, 2:4]) * anchor_wh # width, height
p[:, 4:] = torch.sigmoid(p[:, 4:])
p[:, 5:] = p[:, 5:self.no] * p[:, 4:5]
return p
list_simple = [1, 2, 3, 4, 5]
list_complex = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
# 对list数组进行切片
try: # ① list_simple
print(f"...: {list_simple[..., 2:4]}")
except Exception as e:
print(f"a_list_simple切片报错,错误为: {e}")
try: # ② list_complex
print(f"...: {list_complex[..., 2:4]}")
except Exception as e:
print(f"a_list_complex切片报错,错误为: {e}")
"""
a_list_simple切片报错,错误为: list indices must be integers or slices, not tuple
a_list_complex切片报错,错误为: list indices must be integers or slices, not tuple
"""
很明显,Python的基础数据类型list并不支持这样的切片方式。
import numpy as np
numpy_simple = np.array([1, 2, 3, 4, 5])
numpy_complex = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
print(f"numpy_simple: n{numpy_simple}")
print(f"numpy_complex: n{numpy_complex}")
print("n-------------------------n")
# 对numpy array进行切片
try: # ① list_simple
print(f"...切片没有报错,结果为: {numpy_simple[..., 2:4]}")
except Exception as e:
print(f"numpy_simple切片报错,错误为: {e}")
try: # ② list_complex
print(f"...切片没有报错,结果为: {numpy_complex[..., 2:4]}")
except Exception as e:
print(f"numpy_complex切片报错,错误为: {e}")
"""
numpy_simple:
[1 2 3 4 5]
numpy_complex:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
-------------------------
...切片没有报错,结果为: [3 4]
...切片没有报错,结果为: [[ 3]
[ 6]
[ 9]
[12]]
"""
说明使用[..., a:b]方式是可以对numpy array进行切片的。
我们直接创建一个tensor进行分析:
import torch
a = torch.rand([3, 112, 112])
print(f"...: {a[..., :2].shape}") # ...: torch.Size([3, 112, 2])
可以看到[...,a:b]中的...表示前n-1个维度,a:b表示直接对最后一个维度进行切片
[..., a:b]是array/tensor特有的切片方式,表示直接对最后一个维度进行切片。
到此这篇关于PyTorch中tensor[…, 2:4]的实现示例的文章就介绍到这了,更多相关PyTorch tensor[…, 2:4]内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!
您可能感兴趣的文章:
- 使用PyTorch/TensorFlow搭建简单全连接神经网络
- PyTorch使用教程之Tensor包详解
- 最新tensorflow与pytorch环境搭建的实现步骤
- pytorch tensor合并与分割方式
- Pytorch实现tensor序列化和并行化的示例详解
- PyTorch TensorFlow机器学习框架选择实战
- pytorch中tensorboard安装及安装过程中出现的常见错误问题
- Pytorch之tensorboard无法启动和显示问题及解决
- Pytorch Dataset,TensorDataset,Dataloader,Sampler关系解读