numpy array 以及pytorch tensor 的索引(切片索引,整型索引)

本文探讨了Python中变量赋值与拷贝的区别,并详细解释了Numpy与PyTorch中视图(view)和拷贝(copy)的概念。通过对比不同索引方式的影响,帮助读者了解何时会产生原始数据的引用,何时会创建新副本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概括

我们知道,在python中,变量的赋值‘=’其实是对原始变量的引用,是一种绑定。因此有时我们为了创建新的变量而不影响原始数据,需要使用拷贝(copy),这也是赋值和拷贝的不同。

numpy中的索引有切片索引(slice array indexing)和整型索引(integer array indxing)

其中,numpy array中的切片索引(slice)是对原始数组的一个view,会影响原始数组的。而后者情况比较复杂,不过是basic indexing,则依然是原始数组的一个view,如果是advanced indexig,会用原始数组创建一个新的数组,不会影响原始数据。

x[1, 3:8], x[2:5, 6:9]是slice索引,而x[1,2]是基本integer索引, x[[1,2], [1,4]]是高级integer索引。

针对pytorch tensor可以通过data_ptr()查看第一个元素地址是否相同:

>>> t = torch.rand(4, 4)
>>> b = t.view(2, 8)
>>> t.storage().data_ptr() == b.storage().data_ptr()  # `t` and `b` share the same underlying data.
True
# Modifying view tensor changes base tensor as well.
>>> b[0][0] = 3.14
>>> t[0][0]
tensor(3.14)

Pytorch中

更加详细的解释:https://pytorch.org/docs/stable/tensor_view.html

For reference, here’s a full list of view ops in PyTorch:

It’s also worth mentioning a few ops with special behaviors:

  • reshape()reshape_as() and flatten() can return either a view or new tensor, user code shouldn’t rely on whether it’s view or not.

  • contiguous() returns itself if input tensor is already contiguous, otherwise it returns a new contiguous tensor by copying data.

Numpy中

Pytorch的行为是模仿Numpy的,numpy提供了详细的说明,什么时候是view,什么时候是copy:

https://numpy.org/doc/stable/reference/arrays.indexing.html

Advanced indexing is triggered when the selection object, obj, is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool). There are two types of advanced indexing: integer and Boolean.

Advanced indexing always returns a copy of the data (contrast with basic slicing that returns a view).

具体说明参加上面链接

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值