-2

I wanna know how to get the shape of this tensor in Python ? I have tried this :

> len(x)

But this prints 1, why ? I want to print the number of tuples here which is 3. Using len(x) prints only 1.

What's the problem ?

Here's the tensor :

 (x=array([[[[ 0.07499999,  0.        ],
         [ 0.0703125 ,  0.        ],
         [ 0.0703125 ,  0.        ],
         [ 0.09218752,  0.        ],
         [ 0.1953125 ,  0.        ],
         [ 0.05312502,  0.        ],
         [ 0.2890625 ,  0.        ],
         [ 0.015625  ,  0.        ],
         [ 0.32656252,  0.        ],
         [ 0.09218752,  0.        ],
         [ 0.23906249,  0.        ],
         [ 0.09218752,  0.        ],
         [ 0.22812498,  0.        ],
         [ 0.06406248,  0.        ],
         [ 0.19062501,  0.        ],
         [ 0.02031249,  0.        ],
         [ 0.17343748,  0.        ]],

        [[ 0.06875002,  0.        ],
         [ 0.06875002,  0.        ],
         [ 0.06875002,  0.        ],
         [ 0.09062499,  0.        ],
         [ 0.19375002,  0.        ],
         [ 0.05781251,  0.        ],
         [ 0.2921875 ,  0.        ],
         [ 0.01406252,  0.        ],
         [ 0.325     ,  0.        ],
         [ 0.08437502,  0.        ],
         [ 0.23124999,  0.        ],
         [ 0.09531248,  0.        ],
         [ 0.22031248,  0.        ],
         [ 0.06406248,  0.        ],
         [ 0.18906248,  0.        ],
         [ 0.02031249,  0.        ],
         [ 0.171875  ,  0.        ]],

        [[ 0.06718749,  0.        ],
         [ 0.06093752,  0.        ],
         [ 0.07187498,  0.        ],
         [ 0.078125  ,  0.        ],
         [ 0.18593752,  0.        ],
         [ 0.03437501,  0.        ],
         [ 0.2765625 ,  0.        ],
         [-0.00312501,  0.        ],
         [ 0.29843748,  0.        ],
         [ 0.078125  ,  0.        ],
         [ 0.21718752,  0.        ],
         [ 0.078125  ,  0.        ],
         [ 0.21249998,  0.        ],
         [ 0.07187498,  0.        ],
         [ 0.19062501,  0.        ],
         [ 0.13749999,  0.        ],
         [ 0.1796875 ,  0.        ]]]], dtype=float32), 0)
Mejdi Dallel
  • 573
  • 9
  • 24
  • 3
    Because the shape of your array is `(1, 3, 17, 2)` – Ch3steR May 14 '20 at 16:51
  • 4
    `len` & numpy's `.shape` are quite different things. `len` is for the first-hand length of a sequence. It has no knowledge of potential sub-sequences – rdas May 14 '20 at 16:57
  • Actually it's a tensor and not an array so x[0] will the print the first shape of the tensor which is 1 here. – Mejdi Dallel May 14 '20 at 17:02
  • Does this answer your question? [PyTorch: How to get the shape of a Tensor as a list of int](https://stackoverflow.com/questions/46826218/pytorch-how-to-get-the-shape-of-a-tensor-as-a-list-of-int) – Tomerikoo May 14 '20 at 17:08
  • Actually it dosen't. – Mejdi Dallel May 14 '20 at 17:13
  • Here's a similar question : https://discuss.pytorch.org/t/is-there-anyway-to-get-the-first-element-of-a-tensor-as-a-scalar/2097 But the answer give me the same result. – Mejdi Dallel May 14 '20 at 17:31
  • For your [mre] you should post code that *creates* an example then ppl that have the rquired libraries can help you. – wwii May 14 '20 at 17:56

2 Answers2

1

It looks like your 3 tuples are located within the first (and only) index of x. In this case, len(x[0]) yields 3.

lsterzinger
  • 687
  • 5
  • 22
0

As pointed out by @rdas:

len is for the first-hand length of a sequence. It has no knowledge of potential sub-sequences

The tensor has 1 list inside it ( and this list has nested lists ). Hence the length of the tensor is 1.

Kashinath Patekar
  • 1,083
  • 9
  • 23