Pytorch study notes (1) Recognition and generation of data types

I. Introduction  

         I used the yolov5 code written by pytorch to do projects when I graduated from undergraduate school, but I never systematically learned the knowledge of this deep learning framework. Forget, record it again. The content learned in this article is all based on the course content of teacher Long Liangqu from the National University of Singapore and some of my own understanding. 

       Disclaimer: The relevant tables in this article come from the courseware of Teacher Long Liangqu.

2. Data type

       As shown in the table below, all torch data exists in the form of tensors, including 8-bit, 16-bit, 32-bit, and 64-bit integer tensors and 16-bit, 32-bit, and 64-bit floating-point tensors. But torch does not have data of type str.

 

 

 

 3. Tensor data generation and attribute viewing

1. Data generation

[In]:a=torch.tensor(1)   #生成dim=0的标量
[Out]:tensor(1)

[In]:a=torch.tensor([1])   #生成dim=1,size=Size[1]的tenosr
[Out]:tensor([1])

[In]:a=torch.tensor([1,3,4])   #生成dim=1,size=Size[3]的tenosr
[Out]:tensor([1,3,4])

[In]:a=torch.tensor([[1,3,4]])   #生成dim=2,size=Size[1,3]的tenosr
[Out]:tensor([[1,3,4]])

[In]:a=torch.tensor([[1,3,4],[2,5,6]])   #生成dim=2,size=Size[2,3]的tenosr
[Out]:tensor([[1,3,4],[2,5,6]])


[In]:a=torch.randn(2,4,5,6)   #随机生成 mean=0,std=1,dim=4,size=Size[2,4,5,6]的tenosr
[Out]:...

[In]:a=torch.rand(2,4,5,6)   #作用同上,在0~1之间生成随机tenosr
[Out]:...

[In]:a=torch.zeros(2,3)   #随机生成dim=2,size=Size[2,3]的全零tenosr
[Out]:tensor([[0,0,0],[0,0,0]])

[In]:a=torch.ones(2,3)   #随机生成dim=2,size=Size[2,3]的全1 tenosr
[Out]:tensor([[1,1,1],[1,1,1]])

2. View properties

a = torch.randn(2,3)

[In]:a.dim()               #查看a tenosr的维数
[Out]:2

[In]:a.type()              #查看a tensor的数据类型
[Out]:torch.FloatTensor

[In]:a.shape/a.size()      #查看a tensor的形状/大小
[Out]:torch.Size([2,3])

[In]:a.numel()             
[Out]:6                    #6 = 2*3

Four. Summary

        Tensor is similar to the generation of arrays, so it is easier to get started. This article mainly introduces the most basic data knowledge of pytorch. The above content is purely hand-written. If there is any mistake, please correct me. The following will introduce the indexing and slicing operations of data. Personally, I still have a headache about this content, and I am not particularly proficient. Over a mountain is another mountain, see you on the next mountain.

Guess you like

Origin blog.csdn.net/qq_40691868/article/details/120627997