Reputation: 1821
I have a tensor X of the shape
(2,[...])
where the three dots can be any vector of dimensions (i.e: 4,4)
I would like to transform this tensor to a tensor whose shape is
(1,[...])
In other words, I want to reduce the dimensionality of the first dimension. Of course, I will loose the information indexed by 2 but it does not matter in this case. The problem is not trivial as I don't know the number of dimensions of the tensors. But at least it's greater or equal to 2. A th code is written below:
th> x = torch.Tensor(2, 4, 4):fill(1)
th> y = x[1]
th> z = torch.Tensor()
th> z[1] = y -- bad argument #1 to '?' (empty tensor at /home/ubuntu/torch/pkg/torch/generic/Tensor.c:684)
Do you have any ideas how to do it?
Many thanks in advance
Upvotes: 1
Views: 747
Reputation: 2160
This code does what you need:
y = x[{{1, 1}, {}, {}}]
The unknown number of dimensions? Not a problem, you don't have to specify them all:
y = x[{{1, 1}}]
(1,.,.) =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
[torch.DoubleTensor of size 1x4x4]
Upvotes: 1
Reputation: 16121
I want to reduce the dimensionality of the first dimension
If by reducing you mean narrow down then you can do e.g.:
x = torch.Tensor(2, 4, 4)
x[1]:fill(0)
x[2]:fill(1)
-- this creates tensors of size 1x4x4
-- the first dimension is narrowed, one index at a time
a = x:narrow(1, 1, 1)
b = x:narrow(1, 2, 1)
This gives:
> print(a)
(1,.,.) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
[torch.DoubleTensor of size 1x4x4]
> print(b)
(1,.,.) =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
[torch.DoubleTensor of size 1x4x4]
Upvotes: 2