Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Pytorch Tensor data Type

Shulou Source: shulou.com Published: 2022-06-02 15:56:38 09月22日 Update

Pytorch tensor data type example analysis, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Differences in data types between 1.python and pytorch

Strings cannot be displayed in PyTorch, so expressing strings needs to be converted to an encoded type, such as one_hot,word2vec, and so on.

two。 Tensor

In python, there will be a distinction between scalars, vectors, matrices, etc. But in PyTorch, these are collectively called tensor tensor, but the dimensions are different.

A scalar is a 0-dimensional tensor with only one number and no dimension.

A vector is an one-dimensional tensor, which is a sequential number, but there is no distinction between "rows" and "columns".

A matrix is a two-dimensional tensor with shapes, rows and columns.

By analogy, 3-D and 4-D tensors are also commonly used in PyTorch.

The specific ways to generate tensors and obtain related features are as follows:

① one-dimensional tensor

In PyTorch, there are no brackets, only one number, which is the one-dimensional tensor, which is the scalar in python.

You can view the dimensions of the data in different ways:

For the 0-dimensional tensor, when you look at the shape, it is 0.

② two-dimensional tensor

Through Pytorch, you can specify a specific tensor data directly, or you can randomly generate the data of the specified shape by specifying the shape of the tensor.

If the data is generated through numpy, it can be converted into a tensor through torch.from_numpy.

③ 3D tensor

In general, 3D tensors are used in RNN.

④ 4-dimensional tensor

In general, 3D tensors are used in CNN. For example, the four-dimensional tensor generated in the following picture can be understood as two pictures, three layers of color, and the length and width are both 28.

Above, you can generate the tensor of the desired dimension and view the relevant attributes in different ways.

After reading the above, have you mastered the method of example analysis of Pytorch tensor data types? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Tensors data that is generation types shapes methods dimensions different can be passed through numbers scalars examples analysis content only vectors characters strings more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Apple Shulou Technology NVidia MySQL