Search code examples
torch

Torch7 Access one element from a Tensor as a Tensor


I'm working on Torch7 to train some neural nets and I've got a Tensor of dim 1 (vector) and I want to access the element i in this vector. Unfortunately, it gives me an integer instead of a Tensor of size 1.

I got this :

matrix = torch.Tensor{1,2}
>  1
   2
  [torch.DoubleTensor of size 2]
matrix[1]
> 1

I want this :

matrix[1]
> 1 
  [torch.DoubleTensor of size 1]

I'm obliged to do this :

torch.Tensor{matrix[1]}
> 1 
  [torch.DoubleTensor of size 1]   

Solution

  • You can use torch indexing operator as follow:

    > t = matrix[{ {1} }]
    > = t
     1
    [torch.DoubleTensor of size 1]