Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic data types of PyTorch

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what are the basic data types of PyTorch, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1) Tensor (tensor)

The most basic operation object in Pytorch is Tensor (tensor), which represents a multi-dimensional matrix and has matrix-related operations. It corresponds to numpy in use, and the only difference between pytorch and numpy is that pytorch can run on GPU, while numpy cannot. Therefore, we can also use Tensor instead of numpy. Of course, the two can also be converted to each other.

There are five basic data types for Tensor:

32-bit floating point type: torch.FloatTensor. This type is the default for pyorch.Tensor ().

64-bit integer: torch.LongTensor.

32-bit integer: torch.IntTensor.

16-bit integer: torch.ShortTensor.

64-bit floating point type: torch.DoubleTensor.

So how do you define the Tensor tensor? In fact, it is defined in the same way as numpy, which can be passed directly into the corresponding matrix. A matrix of three rows and two columns is defined below:

Import torch# guide package a = torch.Tensor ([[1,2], [3,4], [5,6]]) print (a)

However, in a project, it is more common to initialize a matrix with special or random values, such as the following:

Import torch # defines a matrix with three rows and two columns with all zeros b = torch.zeros ((3,2)) # defines a random value matrix with three rows and two columns c = torch.randn ((3,2)) # defines a matrix d = torch.ones ((3,2)) print (b) print (c) print (d)

Tensor and numpy.ndarray can also be converted to each other in the following ways:

Convert Numpy to Tensor:torch.from_numpy (numpy matrix)

Tensor is converted to numpy:Tensor matrix. Numpy ()

Examples are as follows:

Import torchimport numpy as np # defines a matrix with three rows and two columns, b = torch.randn ((3,2)) # tensor into numpynumpy_b = b.numpy () print (numpy_b) # numpy into tensornumpy_e = np.array ([[1,2], [3,4], [5,6]]) torch_e = torch.from_numpy (numpy_e) print (numpy_e) print (torch_e)

As mentioned earlier, the biggest difference between numpy and Tensor is the support for GPU. Tensor only needs to call the cuda () function to convert it to a type that can run on GPU.

We can use the torch.cuda.is_available () function to determine whether the current environment supports GPU, and if so, returns True. Therefore, to be on the safe side, the "judge first, then use" strategy is generally adopted in the project code to ensure the normal operation of the code, and its basic structure is as follows:

Import torch # defines an all-zero matrix tmp = torch.randn ((3,2)) # if GPU is supported, it is defined as the GPU type if torch.cuda.is_available (): inputs = tmp.cuda () # otherwise, it is defined as the general Tensor type else: inputs = tmp

2) Variable (variable)

The function of Variable type data in Pytorch is more powerful, which is equivalent to a shell in the outer layer of Tensor, which gives forward propagation, back propagation, automatic derivation and other functions, which plays a very important role in the construction of computing graph. The structure of Variable is as follows:

The two most important attributes are data and grad. Data represents the actual data saved by the variable, the original tensor type saved by it can be accessed through this attribute, and the gradient of the variable (variable) is accumulated onto .grad.

You need to import from torch.autograd when using Variable. Let's take a look at its automatic derivation process through an example:

Import torchfrom torch.autograd import Variable # defines three Variable variables x = Variable (torch.Tensor ([1,2,3]), requires_grad=True) w = Variable (torch.Tensor ([2,3,4]), requires_grad=True) b = Variable (torch.Tensor ([3,4,5]), requires_grad=True) # to build a calculation graph, the formula is: y = w * x ^ 2 + by = w * x * x + b # automatic derivation Calculate the gradient y.backward (torch.Tensor ([1,1,1])) print (x.grad) print (w.grad) print (b.grad)

The calculation diagram of the above code is y = w * x ^ 2 + b. The partial derivatives of x ^ w and b are as follows: x.grad = 2wx ·grad = x ^ 2, b.grad=1. The value test shows that the calculation result is correct.

The above is all the content of the article "what are the basic data types of PyTorch". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report