How to create a rank 3 tensor in Pytorch?

Please share the co

```
import torch
x=torch.rand(8)
print(x.size())
x=x.view(2,2,2)
print(x.size())
```

You can also use `.reshape()`

depending on the application.

@J_Johnson But when I checked the rank of the tensor that you provided using **torch.linalg.matrix_rank()** function it shows like **tensor([2, 2])**.

Here is the code

import torch

x=torch.rand(8)

print(x.size())

x=x.view(2,2,2)

print(x.size())

print(torch.linalg.matrix_rank(x))

Ideally, it should be **3** right if the rank is 3?

That’s not the **tensor** rank.

https://pytorch.org/docs/stable/generated/torch.linalg.matrix_rank.html

See also:

@J_Johnson

Thank you.

If so what is the corresponding torch function or method to see the rank of a tensor?

There is no built in method I’m aware of. But if you want the number of dims(i.e. tensor rank), you can just use `len(x.size())`

.

1 Like

`x.dim()`

Best regards

Thomas

2 Likes