Why does tensor indexing x[i][j][k] return a different value as x[i,j,k] ? #816
-
Here is my code -
This is what my output looks like -
Shouldn't both |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Using [:] is like using a wild character or in simple language using None in python. Thus, x[:] means selecting all and not decreasing dimensionally. ![]() |
Beta Was this translation helpful? Give feedback.
-
The two notations do not have the same meaning, though can look like they are the same with some usages. With Lets take away the 3rd dimension from your tensor for simplicity, so now you have a more traditional matrix. In this notation with indexing as a sequence of operations (e.g. x[:][2]) you can see that x[:] is exactly the same as x, so x[:][2] is the exact same as x[2]. This means you're getting the 3rd array (index 2) in the array of arrays. In [35]: mat
Out[35]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [36]: mat[:]
Out[36]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [37]: mat[:][2]
Out[37]: array([7, 8, 9])
In [38]: mat[2]
Out[38]: array([7, 8, 9]) In this example we show that the comma based notation is more like a linear algebra representation that indicates which portion of the matrix is to be returned, first specifying rows and columns. Using In [39]: mat[:,:]
Out[39]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [40]: mat[:,:2]
Out[40]:
array([[1, 2],
[4, 5],
[7, 8]])
In [41]: mat[:,2]
Out[41]: array([3, 6, 9]) ` |
Beta Was this translation helpful? Give feedback.
The two notations do not have the same meaning, though can look like they are the same with some usages.
With
x[i][j][k]
you should think about it as indexing into a nested list of arrays, while thex[i, j, k]
is more like specifying what ranges you would like in each dimension for a tensor. At first thought these seem equivalent but they are not.Lets take away the 3rd dimension from your tensor for simplicity, so now you have a more traditional matrix.
In this notation with indexing as a sequence of operations (e.g. x[:][2]) you can see that x[:] is exactly the same as x, so x[:][2] is the exact same as x[2]. This means you're getting the 3rd array (index 2) in the array of arrays.