当前位置:网站首页>Pytorch基础
Pytorch基础
2022-07-06 09:14:00 【小白地瓜】
安装Pytorch
pip install torch
一、什么是Pytorch
Pytorch是一个基于Numpy的科学计算包,向它的使用者提供了两大功能
- 作为Numpy的替代者,向用户提供使用GPU强大功能的能力
- 作为一个深度学习的平台,向用户提供最大的灵活性和速度
二、基本的元素操作
- Tensor张量:类似于Numpy中的ndarray数据结构,最大的区别在于Tensor可以利用GPU的加速功能
1、 创建矩阵的操作
- 创建一个没有初始化的矩阵
import torch
x = torch.empty(5, 3)
print(x)
- 创建一个有初始化的矩阵
值是符合标准高斯分布的
import torch
x = torch.rand(5, 3)
print(x)
注意: 对比有无初始化的矩阵,当声明一个未初始化的矩阵时,它本身不包含任何确切的值,当创建一个未初始化的矩阵时,分配给矩阵的内存中有什么值就赋值给这个矩阵,本质上是毫无意义的数据
- 创建一个全零矩阵并可指定数据元素的类型为long
import torch
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
- 通过数据创建张量
import torch
x = torch.tensor([2.5, 3.5, 2.5])
print(x)
- 通过已有的一个张量创建相同尺寸的新张量
x = torch.zeros(5, 3, dtype=torch.long)
# 利用new_ones方法得到一个张量
x = x.new_ones(5, 3, dtype=torch.double)
print(x)
# 利用randn_like方法得到相同张量尺寸的一个新张量,并且采用随机初始化来对其赋值
y = torch.randn_like(x, dtype=torch.float)
print(y)
- 得到张量的尺寸
x.size()
注意返回值为元组,支持一切元组操作
三、基本的运算操作
- 加法操作
result = torch.empty(5, 3)
x = torch.rand(5, 3)
y = torch.rand(5, 3)
print(x + y) # 第一种加法
print(torch.add(x, y)) # 第二种加法1
torch.add(x, y, out=result) # 第二种加法2
print(result)
y.add_(x) # 第三种加法
print(y)
结果都是一样的
- 张量的切片
x = torch.randn(4, 4)
print(x[:, :1])
- 改变张量的形状
x = torch.randn(4, 4)
# torch.view()操作需要保证数据元素的总数量不变
y = x.view(16)
# -1自动匹配个数
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())
- 如果张量中只有一个元素,可以用 .iem() 将值取出,作为python的number类型
x = torch.randn(1)
print(x)
print(x.item())
四、Torch Tensor和Numpy array之间的相互转换
- Torch Tensor和Numpy array共享底层的内存空间,因此改变其中一个值,另一个也会随之改变
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)
五、GPU和CPU之间转移
x = torch.randn(1)
# 如果服务器上安装了GPU和CUDA
if torch.cuda.is_available():
# 定义一个设备对象,这里指定成CUDA,即GPU
device = torch.device("cuda")
# 直接在GPU上创建一个Tensor
y = torch.ones_like(x, device=device)
# 将在CPU上面的x张量移动到GPU上面
x = x.to(device)
# x和y都在GPU上面,才能进行加法运算
z = x + y
# 此时的张量z在GPU上面
print(z)
# 也可以将z转移到CPU上面,并同时指定张量的元素类型
print(z.to("cpu",torch.double))
边栏推荐
- [recommended by bloggers] asp Net WebService background data API JSON (with source code)
- 01项目需求分析 (点餐系统)
- Other new features of mysql18-mysql8
- QT creator design user interface
- Solution: log4j:warn please initialize the log4j system properly
- Introduction and use of automatic machine learning framework (flaml, H2O)
- Use dapr to shorten software development cycle and improve production efficiency
- 图片上色项目 —— Deoldify
- Windows下安装MongDB教程、Redis教程
- [recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
猜你喜欢
Installation and use of MySQL under MySQL 19 Linux
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
Machine learning -- census data analysis
CSDN问答模块标题推荐任务(一) —— 基本框架的搭建
Learning question 1:127.0.0.1 refused our visit
AcWing 1298.曹冲养猪 题解
Basic use of redis
【博主推荐】SSM框架的后台管理系统(附源码)
CSDN question and answer module Title Recommendation task (II) -- effect optimization
软件测试与质量学习笔记3--白盒测试
随机推荐
windows下同时安装mysql5.5和mysql8.0
报错解决 —— io.UnsupportedOperation: can‘t do nonzero end-relative seeks
Navicat 導出錶生成PDM文件
One click extraction of tables in PDF
01项目需求分析 (点餐系统)
Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
[BMZCTF-pwn] 11-pwn111111
Navicat 导出表生成PDM文件
数据库高级学习笔记--SQL语句
记某公司面试算法题:查找一个有序数组某个数字出现的次数
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
Swagger, Yapi interface management service_ SE
CSDN markdown editor
MySQL master-slave replication, read-write separation
Ansible实战系列三 _ task常用命令
QT creator shape
Windows下安装MongDB教程、Redis教程
frp内网穿透那些事
AcWing 1294.樱花 题解
Postman Interface Association