当前位置:网站首页>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
边栏推荐
- Four methods of unity ugui button binding events
- [note] usage model tree of the unity resource tree structure virtualizingtreeview
- Unity3d realizes Google Digital Earth
- Unity3d position the model, rotate, drag and zoom around the model to obtain the center point of the model
- 2021-06-17 solve the problem of QML borderless window stretching, window jitter and flicker when stretching and shrinking
- Unity script life cycle and execution sequence
- 力扣209. 长度最小的子数组
- On mask culling of unity
- z-index属性在什么情况下会失效?
- Pit of smoothstep node in shadergraph
猜你喜欢

力扣2049:统计最高分的节点数目

Redis cluster concept

Solution to Autowired annotation warning

LXC 和 LXD 容器总结

Unity scroll view element drag and drop to automatically adsorb centering and card effect

Records of problems encountered in unity + hololens development

Pycharm database tool

Procedural animation -- inverse kinematics of tentacles

Easyrecovery data recovery software recovers my photo and video data two years ago

Deeply understand the function calling process of C language
随机推荐
Easyrecovery data recovery software recovers my photo and video data two years ago
On mask culling of unity
Unity ontriggerenter does not call
Unrealeengine4 - about uobject's giant pit that is automatically GC garbage collected
Unity supports the platform # define instruction of script
svg和canvas的区别
C # Foundation
Tcp/ip protocol details Volume I (Reading Guide)
Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
Unity packaging and publishing webgl error reason exception: failed building webgl player
Chinese pycharm changed to English pycharm
Oculus quest2 development: (I) basic environment construction and guide package
Pycharm database tool
UnityEngine. JsonUtility. The pit of fromjason()
Force buckle 209 Minimum length subarray
Sourcetree usage
[notes] unity Scrollview button page turning
Unity application class and data file path
Unity + hololens publishing settings
Some problems encountered in unity steamvr