# N dim tensor in pytorch

Hi everyone,

I want to make a tensor T of dim n (arbitrary) which is something like `T_{i1,...,in}` such that each of i can be 0, 1, or 2 which must be something like ` T = torch.tensor(3, 3, ..., 3)`. Do you have any idea how can I do this?

I don’t fully understand the use case.
Would you like to set the size as 0, 1, or 2 or the input values?
The former case would be possible via the factory methods, e.g.: `torch.randn(3)`, while the latter would work via e.g. `tensor.fill_(value)` or `torch.full(...)`.

Thanks for the help, the use case is a scientific program that I am working on.
BTW, I need to make an n-dimensional tensor so it has n subscript for example
`T_ 00...001 ` is one of them, `T_ 0....002 `, `T_ 0...010 ` and etc. Naively thinking about this problem is to use n layers of loops, `for i in range(3): `, but I thought maybe tf or torch can help with this. In the end, I will input some value for each of these T.

Could you post an example, how a potential tensor could look like, i.e. what shape it has, which values it contains, and maybe even the unwanted for loop?

Yes of course. let’s take a small n = 2, then I can initialize my tensor with
` T = torch.ones(3,3)`
and I can assign anything I want, as an example:
` For i in range(3):`
`For j in range(3):`
`T[i][j] = j*i`

but if I want to put n = 3;
`T = torch.ones(3,3,3)`
and if I want to do something like what I did for n=2 I need 3 loops.
but what I really want to do is for n= 100 and it is basically impossible this way.

Thanks for spending time on this.

Thanks for the update.
Based on your provided code you could use broadcasting and multiply the input tensor as:

``````T = torch.ones(3,3)

for i in range(3):
for j in range(3):
T[i][j] = j*i

print(T)
> tensor([[0., 0., 0.],
[0., 1., 2.],
[0., 2., 4.]])

a = torch.arange(3)
res = a * a.unsqueeze(1)
print(res)
> tensor([[0, 0, 0],
[0, 1, 2],
[0, 2, 4]])

a = torch.arange(100)
res = a * a.unsqueeze(1)
print(res)
> tensor([[   0,    0,    0,  ...,    0,    0,    0],
[   0,    1,    2,  ...,   97,   98,   99],
[   0,    2,    4,  ...,  194,  196,  198],
...,
[   0,   97,  194,  ..., 9409, 9506, 9603],
[   0,   98,  196,  ..., 9506, 9604, 9702],
[   0,   99,  198,  ..., 9603, 9702, 9801]])
``````