Get the App
SLTechnology News&Howtos  ›  Development  › 

How to extract a row of tensor in pytorch

Shulou Source: shulou.com Published: 2022-06-01 12:02:22 10月16日 Update

This article mainly introduces how to extract a tensor line from pytorch, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

The experimental code is as follows:

B=torch.Tensor ([10jue 2], [4jre 5], [7je 8], [[1jue 2], [4je 5], [7je 8]) print (b.size ()) (1jue 2,3,2) print (b […) , 0]) tensor ([10, 4, 7.], [1, 4, 7.]) print (b [... , 0]. Size () (1,2,3) print (b [... , 2]) Traceback (most recent call last): File ", line 1, inIndexError: index 2 is out of bounds for dimension 3 with size 2print (b [0, …]) Tensor ([[10, 2.], [4, 5.], [7, 8.]], [[1, 2.], [4, 5.], [7, 8.]]) print (b [0,...] .size () (2,3,2) print (b [0, … , 0]. Size () (2,3) print (b [0,... , 0]) tensor ([[10., 4., 7.], [1., 4., 7.]])

[... , 0] means that the first column of digits on the fourth axis of tensor b is extracted to form tensor, [0,...] Indicates that the first column of digits on the first axis of tensor b is composed of tensor, [0,... , 0] indicates that the first column of numbers on the first and fourth axes of b are extracted to form a tensor.

Another phenomenon was found.

Print (b [... , 0:]) tensor ([10, 2.], [4, 5.], [7, 8.]], [[1, 2.], [4, 5.], [7, 8.]) print (b [... , 1:]) tensor ([2.], [5.], [8.]], [[2.], [5.], [8.]) print (b [... , 2:]) tensor ([], size= (1,2,3,0))

Supplement: use cases of [..., 0] in PyTorch

1. Example 1import torcha = torch.rand ((17,24,8)) b = a [..., 0] c = a [:,:, 0] print (b = = c)

The result is True

two。 Example 2import torcha = torch.rand ((64,17,24,8)) b = a [..., 0] c = a [:, 0] print (b = = c)

The result is True

3. Conclusion

You can see [... , 0] is equivalent to [:,:,... :, 0]

Thank you for reading this article carefully. I hope the article "how to extract a tensor Line from pytorch" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Extraction articles numbers examples results one code value interest at the same time more friends cases phenomena knowledge conclusions editing industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Docker MariaDB Linux Xiaomi