How to use torch.cat in PyTorch
This article mainly introduces how to use torch.cat in PyTorch. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
Brief introduction of 1.toych
The package torch contains multidimensional data structures and a variety of mathematical operations based on it.
Torch contains the data structure of multi-dimensional tensor and a variety of mathematical operations based on it. In addition, it provides a variety of utilities, some of which can serialize tensors and any type more effectively.
It has a corresponding implementation of CUDA and can perform tensor operations on NVIDIA GPU (computing power > = 3.0).
two。 Tensor Tensors
Torch.is_tensor (obj): returns True if obj is a pytorch tensor
Torch.is_storage (obj): returns True if obj is a pytorch storage object
Torch.numel (input): returns the number of elements in the input tensor.
3.torch.cata = torch.ones ([1jue 2]) b = torch.ones ([1jue 2]) z = torch.cat ([a dint b], 1) aOut [47]: tensor ([[1.1,1.1.1.1.]]) AOut [48]: tensor ([[1, 1]])
If the second parameter is 1 torch.Size torch.cat, you will put ameme b together in columns with the size of 1 (1) 4). If the second parameter is 0, press the line
The rows are put together and the size is torch.Size ([2,2]).
Literally: torch.cat is to splice two tensors (tensor) together, and cat is the meaning of concatenate, that is, splicing, linked together.
Example understanding:
Import torchA = torch.ones (2, 2) A#tensor ([[1, 1, 1.], # [1, 1, 1.]) B=2*torch.ones (4 ~ #) B#tensor ([[2, 2, 2.], # [2, 2, 2.], # [2, 2, 2.], # [2, 2, 2.]) C = torch.cat ((AMague B) 0) # concatenate C#tensor ([[1, 1, 1.], # [1, 1, 1.], # [2, 2, 2.], # [2, 2, 2.], # [2, 2, 2.], # [2, 2, 2.]) D = 2*torch.ones (2, 2, 2.]) M = torch.cat (A) D), 1) # concatenate M#tensor by dimension 1 (column) ([[1, 1, 1, 2, 2, 2.], # [1, 1, 1, 2, 2.]) M.size () # torch.Size ([2, 7])
When using torch.cat ((A _ Magi B), dim), the values of other dimensions need to be the same except that the values of the splicing dimension dim can be different before they can be aligned.
The above is all the contents of the article "how to use torch.cat in PyTorch". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!