Questions related to indexing nD or multidimensional arrays or tensors, in a similar spirit to the tag matrix-indexing
Questions tagged [tensor-indexing]
11 questions
4
votes
3 answers
Slice every item except every nth
In tensorflow it is possible to select every nth item with the slicing notation [::n].
But how to do the opposite? I want to select every item except every nth.
For example:
a = [1, 2, 3, 4, 5, 6, 7, 8]
a[2::3] would result in [3, 6]
Now I would…

Spenhouet
- 6,556
- 12
- 51
- 76
3
votes
2 answers
How to correctly access elements in a 3D-Pytorch-Tensor?
I am trying to access multiple elements in a 3D-Pytorch-Tensor, but the number of elements that are returned is wrong.
This is my code:
import torch
a = torch.FloatTensor(4,3,2)
print("a = {}".format(a))
print("a[:][:][0] =…

Anno
- 761
- 1
- 10
- 22
2
votes
1 answer
Pytorch: How to reorder the tensor by given sorted indices
Given a tensor A shape (d0, d1, ..., dn, dn+1) and a tensor of sorted indices I with shape (d0, d1, ..., dn) I want to reorder the indices of A using the sorted indices in I.
The first n dimensions of tensors A and I are equal, the (n+1)-th…

mattiavandi
- 23
- 3
2
votes
0 answers
PyTorch tensor indexing produce different results after same operation
Short version:
The same operation generates different result.
img1 = torch.zeros(3, img_size, img_size)
img2 = torch.zeros(3, img_size, img_size)
print(torch.allclose(img1, img2))
img1[:, pos[:, 1], pos[:, 0]] = color.T
img2[:, pos[:, 1], pos[:, 0]]…

Zhao Liu
- 21
- 1
1
vote
1 answer
Removing all indices that contain NaNs across a particular dimension in PyTorch Tensor
I have a torch tensor of the following form:
a = torch.tensor([[[2,3],
[3,4],
[4,5]],
[[3,6],
[6,2],
[2,-1]],
[[float('nan'), 1],
…

konstant
- 685
- 1
- 7
- 19
1
vote
1 answer
Pytorch: accessing a subtensor using lists of indices
I have a pair of tensors S and T of dimensions (s1,...,sm) and (t1,...,tn) with si < ti. I want to specify a list of indices in each dimensions of T to "embed" S in T. If I1 is a list of s1 indices in (0,1,...,t1) and likewise for I2 up to In, I…

Vincent L.
- 123
- 5
1
vote
4 answers
For a given condition, get indices of values in 2D tensor A, use those to index a 3D tensor B
For a given 2D tensor I want to retrieve all indices where the value is 1. I expected to be able to simply use torch.nonzero(a == 1).squeeze(), which would return tensor([1, 3, 2]). However, instead, torch.nonzero(a == 1) returns a 2D tensor (that's…

Bram Vanroy
- 27,032
- 24
- 137
- 239
1
vote
1 answer
How to read a tensor as a numpy array or list in Tensorflow?
I have a tensor x,
x={Tensor} Tensor("Cast:0", shape=(?,3), dtype=int32)
Now, I need to iterate over each triple of this tensor batch (say a triple is (a,b,c)) and fetch the first element (in this example, a) of that.
Then, I need to fetch all…

snelzb
- 157
- 3
- 16
1
vote
2 answers
Replace values in a 3d numpy array
There are those two numpy arrays:
a = np.array([
[
[1,2,3,0,0],
[4,5,6,0,0],
[7,8,9,0,0]
],
[
[1,3,5,0,0],
[2,4,6,0,0],
[1,1,1,0,0]
]
])
b = np.array([
[
[1,2],
…

VnC
- 1,936
- 16
- 26
0
votes
2 answers
Why the first index of a multidimensional matrix of Eigen::Tensor able to loop through all the members of the tensor?
Why the first index of a multidimensional matrix of Eigen::Tensor able to loop through all the members of the tensor successfully?
I am new to Eigen::Tensor library and still testing it to adopt it for my code.
#include
using…

mechieCoder
- 5
- 3
0
votes
2 answers
Slicing uneven columns from tensor array
I have an array like so:
([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]],
[[12, 13, 14],
[15, 16, 17]]])
If i want to slice the numbers 12 to 17 i would use:
arr[2, 0:2, 0:3]
but how would i go about slicing the array to get 12 to…

Raphael Estrada
- 1,109
- 11
- 18