当前位置:网站首页>Installation and getting started with pytoch
Installation and getting started with pytoch
2022-06-30 05:09:00 【Students who don't want to be bald】
Pytorch Installation
The goal is
- Know how to install pytorch
1. Pytorch Introduction to
Pytorch Is a facebook Published in-depth learning framework , Because of its ease of use , Friendliness , Favored by the majority of users .
2. Pytorch Version of

3. Pytorch Installation
Installation address introduction :https://pytorch.org/get-started/locally/
belt GPU Installation steps :
conda install pytorch torchvision cudatoolkit=9.0 -c pytorch
No GPU Installation steps
conda install pytorch-cpu torchvision-cpu -c pytorch
Open after installation ipython
Input :
In [1]:import torch
In [2]: torch.__version__
Out[2]: '1.0.1'
Be careful : When installing the module, you install pytorch , But in the code, we use torch
There was an article before the detailed introduction , You can go there by yourself if necessary
The first step of deep learning (anaconda、pytorch install )
Pytorch To get started with
The goal is
- Know tensor sum Pytorch The tensor in
- know pytorch How to create tensors in
- know pytorch in tensor Common ways to do it
- know pytorch in tensor Data type of
- know pytorch How to realize tensor stay cpu and cuda Medium transformation
1. tensor Tensor
Tensor is a general term , There are many types :
- 0 Order tensor : Scalar 、 constant ,0-D Tensor
- 1 Order tensor : vector ,1-D Tensor
- 2 Order tensor : matrix ,2-D Tensor
- 3 Order tensor
- …
- N Order tensor
2. Pytorch Create tensors in
Use python Create a list or sequence in tensor
torch.tensor([[1., -1.], [1., -1.]]) tensor([[ 1.0000, -1.0000], [ 1.0000, -1.0000]])Use numpy Array creation in tensor
torch.tensor(np.array([[1, 2, 3], [4, 5, 6]])) tensor([[ 1, 2, 3], [ 4, 5, 6]])Use torch Of api establish tensor
torch.empty(3,4)establish 3 That's ok 4 The column is empty tensor, Will be filled with useless datatorch.ones([3,4])establish 3 That's ok 4 Column All for 1 Of tensortorch.zeros([3,4])establish 3 That's ok 4 Column All for 0 Of tensortorch.rand([3,4])establish 3 That's ok 4 Column Random value Of tensor, The interval of random values is[0, 1)>>> torch.rand(2, 3) tensor([[ 0.8237, 0.5781, 0.6879], [ 0.3816, 0.7249, 0.0998]])torch.randint(low=0,high=10,size=[3,4])establish 3 That's ok 4 Column Random integers Of tensor, The interval of random values is[low, high)>>> torch.randint(3, 10, (2, 2)) tensor([[4, 5], [6, 7]])torch.randn([3,4])establish 3 That's ok 4 Column random number Of tensor, The distributed mean of random values is 0, The variance of 1
3. Pytorch in tensor The common method of
obtain tensor Data in ( When tensor Only one element is available in ):
tensor.item()In [10]: a = torch.tensor(np.arange(1)) In [11]: a Out[11]: tensor([0]) In [12]: a.item() Out[12]: 0Turn into numpy Array
In [55]: z.numpy() Out[55]: array([[-2.5871205], [ 7.3690367], [-2.4918075]], dtype=float32)Get shape :
tensor.size()In [72]: x Out[72]: tensor([[ 1, 2], [ 3, 4], [ 5, 10]], dtype=torch.int32) In [73]: x.size() Out[73]: torch.Size([3, 2])Shape change :
tensor.view((3,4)). similar numpy Medium reshape, It's a shallow copy , Just a change in shapeIn [76]: x.view(2,3) Out[76]: tensor([[ 1, 2, 3], [ 4, 5, 10]], dtype=torch.int32)Get order :
tensor.dim()In [77]: x.dim() Out[77]: 2Get the maximum :
tensor.max()In [78]: x.max() Out[78]: tensor(10, dtype=torch.int32)Transposition :
tensor.t()In [79]: x.t() Out[79]: tensor([[ 1, 3, 5], [ 2, 4, 10]], dtype=torch.int32)tensor[1,3]obtain tensor The value of the first row and the third column intensor[1,3]=100Yes tensor The position of the first row and the third column in the is assigned 100tensor The section of
In [101]: x
Out[101]:
tensor([[1.6437, 1.9439, 1.5393],
[1.3491, 1.9575, 1.0552],
[1.5106, 1.0123, 1.0961],
[1.4382, 1.5939, 1.5012],
[1.5267, 1.4858, 1.4007]])
In [102]: x[:,1]
Out[102]: tensor([1.9439, 1.9575, 1.0123, 1.5939, 1.4858])
4. tensor Data type of
tensor There are many data types in , The common types are as follows :

In the picture above Tensor types This kind of type Of tensor Is an example
obtain tensor Data type of :
tensor.dtypeIn [80]: x.dtype Out[80]: torch.int32Specify the type when creating data
In [88]: torch.ones([2,3],dtype=torch.float32) Out[88]: tensor([[9.1167e+18, 0.0000e+00, 7.8796e+15], [8.3097e-43, 0.0000e+00, -0.0000e+00]])Type modification
In [17]: a Out[17]: tensor([1, 2], dtype=torch.int32) In [18]: a.type(torch.float) Out[18]: tensor([1., 2.]) In [19]: a.double() Out[19]: tensor([1., 2.], dtype=torch.float64)
5. tensor Other operations of
tensor and tensor Add up
In [94]: x = x.new_ones(5, 3, dtype=torch.float) In [95]: y = torch.rand(5, 3) In [96]: x+y Out[96]: tensor([[1.6437, 1.9439, 1.5393], [1.3491, 1.9575, 1.0552], [1.5106, 1.0123, 1.0961], [1.4382, 1.5939, 1.5012], [1.5267, 1.4858, 1.4007]]) In [98]: torch.add(x,y) Out[98]: tensor([[1.6437, 1.9439, 1.5393], [1.3491, 1.9575, 1.0552], [1.5106, 1.0123, 1.0961], [1.4382, 1.5939, 1.5012], [1.5267, 1.4858, 1.4007]]) In [99]: x.add(y) Out[99]: tensor([[1.6437, 1.9439, 1.5393], [1.3491, 1.9575, 1.0552], [1.5106, 1.0123, 1.0961], [1.4382, 1.5939, 1.5012], [1.5267, 1.4858, 1.4007]]) In [100]: x.add_(y) # The underlined method will be correct for x Make local modifications Out[100]: tensor([[1.6437, 1.9439, 1.5393], [1.3491, 1.9575, 1.0552], [1.5106, 1.0123, 1.0961], [1.4382, 1.5939, 1.5012], [1.5267, 1.4858, 1.4007]]) In [101]: x #x Change Out[101]: tensor([[1.6437, 1.9439, 1.5393], [1.3491, 1.9575, 1.0552], [1.5106, 1.0123, 1.0961], [1.4382, 1.5939, 1.5012], [1.5267, 1.4858, 1.4007]])Be careful : Underlined methods ( such as :
add_) Would be right tensor Make local modificationstensor And digital operation
In [97]: x +10 Out[97]: tensor([[11., 11., 11.], [11., 11., 11.], [11., 11., 11.], [11., 11., 11.], [11., 11., 11.]])CUDA Medium tensor
CUDA(Compute Unified Device Architecture), yes NVIDIA The new computing platform . CUDA It's a kind of NVIDIA General parallel computing architecture , The architecture enables GPU Able to solve complex computing problems .
torch.cudaThis module adds a pair of CUDA tensor Support for , In the cpu and gpu Use the same method on tensoradopt
.toMethod can put a tensor Transfer to another device ( For instance from CPU go to GPU)#device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") if torch.cuda.is_available(): device = torch.device("cuda") # cuda device object y = torch.ones_like(x, device=device) # Create one in cuda Upper tensor x = x.to(device) # How to use x To cuda Of tensor z = x + y print(z) print(z.to("cpu", torch.double)) # .to Method can also set the type >>tensor([1.9806], device='cuda:0') >>tensor([1.9806], dtype=torch.float64)
Through the previous study , You can find torch The various operations of are almost the same as numpy equally
Video learning website :pytorch Getting started video
边栏推荐
- 很紧张,第一天做软件测试,需要做什么?
- Unity packaging and publishing webgl error reason exception: failed building webgl player
- Pit of smoothstep node in shadergraph
- 力扣2049:统计最高分的节点数目
- Nestjs中控制器和路由的配置使用
- Writing unityshader with sublimetext
- 2021-03-16
- Redis cluster concept
- Does the tester need to analyze the cause of the bug?
- 力扣704. 二分查找
猜你喜欢

Harbor API 2.0 query

Unity + hololens2 performance test

One command to run rancher

Unity3d Google Earth

LxC and LXD container summary

Unity packaging and publishing webgl error reason exception: failed building webgl player

Network communication problem locating steps

Writing unityshader with sublimetext

力扣704. 二分查找

Unity is associated with vs. there is a compiler problem when opening
随机推荐
Force buckle 27 Removing Elements
One command to run rancher
Unity + hololens publishing settings
Unity supports the platform # define instruction of script
Read and save txt files
JPA composite primary key usage
Tensorflow2 of ubantu18.04 X installation
Postman 做测试的 6 个常见问题
Four methods of unity ugui button binding events
Under what conditions does the Z-index attribute expire?
Unity limited time use limited trial time and use times
炒美原油的国际交易平台如何能保障资金安全呢?
svg和canvas的区别
QT connecting external libraries
Some problems encountered in unity steamvr
力扣977. 有序数组的平方
Brew install NVM command not found solution
Unity download and installation website
Unity/ue reads OPC UA and OPC Da data (UE4)
力扣704. 二分查找