当前位置:网站首页>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]: 0
Turn 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]: 2
Get 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]=100
Yes 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.dtype
In [80]: x.dtype Out[80]: torch.int32
Specify 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.cuda
This module adds a pair of CUDA tensor Support for , In the cpu and gpu Use the same method on tensoradopt
.to
Method 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
边栏推荐
- Sourcetree usage
- Unit asynchronous jump progress
- Detailed explanation of sorting sort method of JS array
- Tensorflow2 of ubantu18.04 X installation
- Unity project hosting platform plasticscm (learn to use 1)
- Photon pun refresh hall room list
- Chinese pycharm changed to English pycharm
- Singleton mode in unity
- [notes] unity Scrollview button page turning
- 0 basic unity course. Bricklaying
猜你喜欢
MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field
Unity3d realizes Google Digital Earth
Generate a slice of mesh Foundation
Unity 3D model operation and UI conflict Scrollview
力扣977. 有序数组的平方
Unity3d Google Earth
Procedural animation -- inverse kinematics of tentacles
Unity project hosting platform plasticscm (learn to use 2)
中文版PyCharm改为英文版PyCharm
Oculus quest2 development: (I) basic environment construction and guide package
随机推荐
Records of some problems encountered during unity development (continuously updated)
[learning notes] AssetBundle, xlua, hot update (use steps)
Display steerable 3D model in front of unity UI
Unity automatic pathfinding
MinGW-w64下载文件失败the file has been downloaded incorrectly!
LxC and LXD container summary
Nestjs configures static resources, template engine, and post examples
Some books you should not miss when you are new to the workplace
Unity call Exe program
Unity animator does not clip animation to play animation in segments
Unity 3D model operation and UI conflict Scrollview
Chapter 11 advanced data management of OpenGL super classic (version 7)
Nestjs introduction and environment construction
Four methods of unity ugui button binding events
Force buckle 704 Binary search
Postman 做测试的 6 个常见问题
svg和canvas的区别
Easyrecovery data recovery software recovers my photo and video data two years ago
力扣(LeetCode)180. 连续出现的数字(2022.06.29)
2021-03-16