# Access z-axis from tensor 4d

Hello everyone!
How can i get values from z-axis of a tensor 4d (example 32,128,4,4).
In this case i would like to get 128 value (planes) from the first 4x4 matrix and so on.
Anyone known the right operations to do that?

I’m not sure I understand the use case correctly.
To access values in a tensor, you can use indexing as:

``````x = torch.randn(32, 128, 4, 4)
print(x[0, 0])
``````

This would print the “first” `4x4` tensor.

Could you give an example, how your output should look like, please?

1 Like

Ok, i try to explain myself correctly.
In the image below, i would like to extract a tensor from each N plane (red points) starting from the first column/rown and so on. So, in this way i have to obtain X tensors composed by each planes (conv2). Is it possible with pytorch?

This is a sample:

``````import torch

x = torch.tensor(
[
[
[[1, 2], [3, 4], ],
[[5, 6], [7, 8], ],
[[9, 10], [11, 12], ],
],
[
[[13, 14], [15, 16], ],
[[17, 18], [19, 20], ],
[[21, 22], [23, 24], ],
]
]
)

print(x.shape)
# torch.Size([2, 3, 2, 2])

print(x.reshape(2, 3, 4).transpose(2, 1).reshape(8, 3))
# tensor([[ 1,  5,  9],
#         [ 2,  6, 10],
#        [ 3,  7, 11],
#        [ 4,  8, 12],
#       [13, 17, 21],
#      [14, 18, 22],
#     [15, 19, 23],
#    [16, 20, 24]])

``````

generally:

``````a, b, c, d = x.shape
x.reshape(a, b, c*d).transpose(2, 1).reshape(a*c*d, b)
``````
1 Like

perfect, thanks a lot!

1 Like