当前位置:网站首页>Pytoch Foundation
Pytoch Foundation
2022-07-06 11:28:00 【Sweet potato】
install Pytorch
pip install torch
One 、 What is? Pytorch
Pytorch It's based on Numpy Scientific computing package for , It provides two functions to its users
- As Numpy The replacement of , Provide users with access to GPU Powerful capabilities
- As a platform for deep learning , Provide users with maximum flexibility and speed
Two 、 Basic element operations
- Tensor tensor : Be similar to Numpy Medium ndarray data structure , The biggest difference is Tensor You can use GPU The acceleration function of
1、 Operation of creating matrix
- Create an uninitialized matrix
import torch
x = torch.empty(5, 3)
print(x)
- Create a matrix with initialization
The value conforms to the standard Gaussian distribution
import torch
x = torch.rand(5, 3)
print(x)
Be careful : Compare whether there is an initialized matrix , When declaring an uninitialized matrix , It itself does not contain any exact value , When creating an uninitialized matrix , Any value in the memory allocated to the matrix is assigned to the matrix , Essentially meaningless data
- Create an all zero matrix and specify the type of data element as long
import torch
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
- Create tensors from data
import torch
x = torch.tensor([2.5, 3.5, 2.5])
print(x)
- Create a new tensor of the same size from an existing tensor
x = torch.zeros(5, 3, dtype=torch.long)
# utilize new_ones Method to get a tensor
x = x.new_ones(5, 3, dtype=torch.double)
print(x)
# utilize randn_like Method to get a new tensor with the same tensor size , And use random initialization to assign values
y = torch.randn_like(x, dtype=torch.float)
print(y)
- Get the size of the tensor
x.size()
Note that the return value is a tuple , Support all tuple operations
3、 ... and 、 Basic operation
- Addition operation
result = torch.empty(5, 3)
x = torch.rand(5, 3)
y = torch.rand(5, 3)
print(x + y) # The first addition
print(torch.add(x, y)) # The second addition 1
torch.add(x, y, out=result) # The second addition 2
print(result)
y.add_(x) # Third addition
print(y)
The results are the same
- Slice of tensor
x = torch.randn(4, 4)
print(x[:, :1])
- Change the shape of the tensor
x = torch.randn(4, 4)
# torch.view() The operation needs to ensure that the total number of data elements remains unchanged
y = x.view(16)
# -1 Number of automatic matches
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())
- If there is only one element in the tensor , It can be used .iem() Take out the value , As python Of number type
x = torch.randn(1)
print(x)
print(x.item())
Four 、Torch Tensor and Numpy array Mutual conversion between
- Torch Tensor and Numpy array Share the underlying memory space , So change one of the values , The other will change with it
a = torch.ones(5)
print(a)
b = a.numpy()
print(b, type(b))
a = numpy.ones(5)
b = torch.from_numpy(a)
print(a)
print(b)
5、 ... and 、GPU and CPU Transfer between
x = torch.randn(1)
# If installed on the server GPU and CUDA
if torch.cuda.is_available():
# Define a device object , This is designated as CUDA, namely GPU
device = torch.device("cuda")
# Directly in GPU Create a Tensor
y = torch.ones_like(x, device=device)
# Will be in CPU above x Tensor moves to GPU above
x = x.to(device)
# x and y All in GPU above , In order to add
z = x + y
# The tensor at this time z stay GPU above
print(z)
# Can also be z Transferred to the CPU above , And specify the element type of the tensor
print(z.to("cpu",torch.double))
边栏推荐
- Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
- ES6 let 和 const 命令
- 02-项目实战之后台员工信息管理
- Codeforces Round #753 (Div. 3)
- L2-001 emergency rescue (25 points)
- 常用正则表达式整理
- L2-006 树的遍历 (25 分)
- MySQL与c语言连接(vs2019版)
- Use dapr to shorten software development cycle and improve production efficiency
- Knowledge Q & A based on Apache Jena
猜你喜欢
{one week summary} take you into the ocean of JS knowledge
QT creator test
Knowledge Q & A based on Apache Jena
机器学习笔记-Week02-卷积神经网络
Machine learning -- census data analysis
How to build a new project for keil5mdk (with super detailed drawings)
Picture coloring project - deoldify
安装numpy问题总结
AcWing 1298. Solution to Cao Chong's pig raising problem
Vs2019 use wizard to generate an MFC Application
随机推荐
Base de données Advanced Learning Notes - - SQL statements
QT creator specify editor settings
AcWing 179. Factorial decomposition problem solution
小L的试卷
Deoldify project problem - omp:error 15:initializing libiomp5md dll,but found libiomp5md. dll already initialized.
QT creator shape
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_ s instead
nodejs连接Mysql
{one week summary} take you into the ocean of JS knowledge
PHP - whether the setting error displays -php xxx When PHP executes, there is no code exception prompt
Aborted connection 1055898 to db:
误删Path变量解决
[BSidesCF_2020]Had_a_bad_day
引入了junit为什么还是用不了@Test注解
搞笑漫画:程序员的逻辑
[Bluebridge cup 2020 preliminary] horizontal segmentation
牛客Novice月赛40
[蓝桥杯2020初赛] 平面切分
Face recognition_ recognition
wangeditor富文本引用、表格使用问题