当前位置:网站首页>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))
边栏推荐
- [number theory] divisor
- QT creator specifies dependencies
- Install mongdb tutorial and redis tutorial under Windows
- Postman environment variable settings
- Asp access Shaoxing tourism graduation design website
- Machine learning -- census data analysis
- Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
- Navicat 導出錶生成PDM文件
- 连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
- Django running error: error loading mysqldb module solution
猜你喜欢

引入了junit为什么还是用不了@Test注解

CSDN blog summary (I) -- a simple first edition implementation
![[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)](/img/b7/aae35f049ba659326536904ab089cb.png)
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)

A trip to Macao - > see the world from a non line city to Macao

解决:log4j:WARN Please initialize the log4j system properly.

Leetcode 461 Hamming distance

MySQL主从复制、读写分离

Install mongdb tutorial and redis tutorial under Windows

【博主推荐】SSM框架的后台管理系统(附源码)

PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy‘
随机推荐
Introduction to the easy copy module
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)
FRP intranet penetration
Development of C language standard
[recommended by bloggers] C WinForm regularly sends email (with source code)
Other new features of mysql18-mysql8
CSDN问答模块标题推荐任务(二) —— 效果优化
Remember a company interview question: merge ordered arrays
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
安装numpy问题总结
Ansible实战系列二 _ Playbook入门
Tcp/ip protocol (UDP)
软件测试-面试题分享
Armv8-a programming guide MMU (2)
When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
MySQL完全卸载(Windows、Mac、Linux)
Did you forget to register or load this tag
SSM integrated notes easy to understand version
MySQL other hosts cannot connect to the local database
One click extraction of tables in PDF