当前位置:网站首页>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))
边栏推荐
- Vs2019 desktop app quick start
- QT creator specifies dependencies
- Ansible practical series I_ introduction
- 打开浏览器的同时会在主页外同时打开芒果TV,抖音等网站
- [BSidesCF_2020]Had_a_bad_day
- 解决安装Failed building wheel for pillow
- PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy‘
- Punctual atom stm32f103zet6 download serial port pin
- 保姆级出题教程
- Did you forget to register or load this tag
猜你喜欢
Introduction and use of automatic machine learning framework (flaml, H2O)
图片上色项目 —— Deoldify
快来走进JVM吧
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
MySQL与c语言连接(vs2019版)
AcWing 1298. Solution to Cao Chong's pig raising problem
QT creator shape
[number theory] divisor
AI benchmark V5 ranking
Valentine's Day flirting with girls to force a small way, one can learn
随机推荐
AcWing 179.阶乘分解 题解
Mtcnn face detection
Double to int precision loss
Rhcsa certification exam exercise (configured on the first host)
C语言读取BMP文件
vs2019 第一个MFC应用程序
QT creator support platform
DICOM: Overview
数据库高级学习笔记--SQL语句
机器学习--人口普查数据分析
软件测试-面试题分享
小L的试卷
AcWing 1294.樱花 题解
安全测试涉及的测试对象
保姆级出题教程
Codeforces Round #771 (Div. 2)
Why can't I use the @test annotation after introducing JUnit
Codeforces Round #753 (Div. 3)
Learn winpwn (3) -- sEH from scratch
L2-001 紧急救援 (25 分)