火炬补充索引操作

发布于 2025-02-03 05:04:16 字数 431 浏览 3 评论 0原文

我有一个张量说t = [1、2、3、4、5、6、7、8、9]和我想删除的索引列表,例如d = [0 ,2,5]

t [d]给我带有我要删除的元素的子镜。

我如何使用d来获取所有元素 d索引的子镜头。必须有t [〜d]之类的东西,对吗?

像Numpy的 numpy.delete.delete.delete

I have a tensor say t = [1, 2, 3, 4, 5, 6, 7, 8, 9] and a list of indices I want to remove like d = [0, 2, 5].

Doing t[d] gives me the subtensor with the elements I want to remove.

How can I used d to get the subtensor with all the elements except the ones indexed in d. There must be something like t[~d], right?

Something like numpy's numpy.delete.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏月而来 2025-02-10 05:04:16

delete使用逻辑,例如

mask= np.ones(t.shape, dtype=bool)
mask[d] = False
res = t[mask]

mask是我们要保留的所有索引的bool。这是简单的1D版本。

In [3]: t=np.arange(1,10); d= np.array([0,2,5])

In [4]: mask = np.ones(t.shape, bool)
In [5]: mask[d] = False
In [6]: mask
Out[6]: array([False,  True, False,  True,  True, False,  True,  True,  True])

In [7]: t[mask]
Out[7]: array([2, 4, 5, 7, 8, 9])

delete uses logic like

mask= np.ones(t.shape, dtype=bool)
mask[d] = False
res = t[mask]

mask is a bool that is true for all indices we want to keep. This is the simple 1d version.

In [3]: t=np.arange(1,10); d= np.array([0,2,5])

In [4]: mask = np.ones(t.shape, bool)
In [5]: mask[d] = False
In [6]: mask
Out[6]: array([False,  True, False,  True,  True, False,  True,  True,  True])

In [7]: t[mask]
Out[7]: array([2, 4, 5, 7, 8, 9])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文