Good way to down-dimension (extract) of a 3D tensor (or same as numpy)
I have some data stored in a certain 3D tensor
data1 = torch.ones(3, 3, 3, requires_grad=True, dtype=torch.float64)   
data2 = torch.zeros(3, 3, 3, requires_grad=True, dtype=torch.float64)   

When I perform the calculation
temp= data1[:,0,0]+data2[:,0,0]

I would like to see the result in form of size ([3])tensor instead of ([3,1,1])
So considering the performance,

I should extract from the data1, data2 or temp?
How to do this?


As @paime said, you can use single element slices instead of single indexes:
&gt;&gt;&gt; data1[:,:1,:1] + data2[:,:1,:1]
tensor([[[1.]],

        [[1.]],

        [[1.]]], dtype=torch.float64, grad_fn=&lt;AddBackward0&gt;)

Alternatively, there are different ways of unsqueezing multiple dimensions at the same time:

Using two torch.Tensor.unsqueeze
&gt;&gt;&gt; temp.unsqueeze(-1).unsqueeze(-1)


Reshaping the tensor with a view:
&gt;&gt;&gt; temp.view(*temp.shape, 1, 1)


Or with fancy indexing:
&gt;&gt;&gt; temp[..., None, None]



