当前位置:网站首页>1.PyTorch简介
1.PyTorch简介
2022-07-27 05:13:00 【派大星的最爱海绵宝宝】
一.发展
2002年Torch
2011年Torch7
2016.10发布PyTorch0.1,THNN后端
2018.12发布1.0,CAFFE2后端
2019.5发布1.1
二.同类框架
Google:keras,TensorFlow
Amazon:MXNet
Facebook:Caffe2,PyTorch
PyTorch和TensorFlow区别
1.动态图
类似于人的思维模式,每次直接用具体的值进行计算并得到结果。
PyTorch是动态图。
2.静态图
就像先定义一个公式,每次我们都会重新给定一个具体值,运行时会代入公式计算结果,创建公式和运行两段分离,静态体现在公式创建好后是不能更改的。
TensorFlow1是静态图,TensorFlow2是动态图和静态图结合。
3.总结
作为研究人员或者新手人员,推荐PyTorch,使用简单,把更多精力放在算法上。
作为工程师,推荐TensorFLow2,工业布局更好。
三.PyTorch简单功能
1.GPU加速
import torch
import time
print(torch.torch_version)
print(torch.cuda.is_available())
#两个矩阵
a = torch.randn(10000,1000)
b = torch.randn(1000,2000)
#使用cpu计算矩阵的乘法,并记录计算时间0
t0 = time.time()
c = torch.matmul(a,b)
t1 = time.time()
print(a.device,t1-t0,c.norm(2))
#使用GPU计算
device = torch.device('cuda')
a = a.to(device)
b = b.to(device)
t0 = time.time()
c = torch.matmul(a,b)
t1 = time.time()
print(a.device,t1-t0,c.norm(2))
#再计算一遍验证结果
t0 = time.time()
c = torch.matmul(a,b)
t1 = time.time()
print(a.device,t1-t0,c.norm(2))
采用CPU运行时间是 0.1456127166748047,采用GPU第一次运行时间是0.38901543617248535,比CPU还大,是因为第一次采用CUDA时会初始化环境,这个时间是不准确的计算时间,第二次可能是因为计算过快,直接显示0。
把矩阵变大一点:
a = torch.randn(10000,1000)
b = torch.randn(1000,8000)
结果为:

2.自动求导
import torch
from torch import autograd
x = torch.tensor(1.)
a = torch.tensor(1.,requires_grad=True)
b = torch.tensor(2.,requires_grad=True)
c = torch.tensor(3.,requires_grad=True)
y = a**2 * x + b * x + c
print('before:',a.grad,b.grad,c.grad)
#开始求导
grads = autograd.grad(y,[a,b,c])
print('after:',grads[0],grads[1],grads[2])
对于该公式y,对a求偏导得2ax,对b求偏导得x,对c求偏导得1.

3.常用网络层
nn.Linear
nn.Conv2d
nn.LSTM
nn.ReLU
nn.Sigmod
nn.Softmax
nn.CrossEntropyLoss
nn.MSE
边栏推荐
- Day10. Work organization and mental health problems in PhD students
- Rating and inquiry details of futures companies
- andorid检测GPU呈现速度和过度绘制
- GBASE 8C——SQL参考6 sql语法(5)
- 使用Docker部署Redis进行高可用主从复制
- 【好文种草】根域名的知识 - 阮一峰的网络日志
- What are alpha and beta tests?
- Inno setup package jar + H5 + MySQL + redis into exe
- DDD领域驱动设计笔记
- kettle如何处理文本数据传输为‘‘而不是null
猜你喜欢

Inno setup package jar + H5 + MySQL + redis into exe

我想不通,MySQL 为什么使用 B+ 树来作索引?

10.梯度、激活函数和loss

GBase 8c产品简介

根据文本自动生成UML时序图(draw.io格式)

Emoji表情符号用于文本情感分析-Improving sentiment analysis accuracy with emoji embedding

Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output

Graph node deployment

解决MySQL JDBC数据批量插入慢的问题

The written test questions of 25 large Internet companies are summarized, and I have encountered packages.
随机推荐
GBASE 8C——SQL参考6 sql语法(15)
golang中slice切片使用的误区
If you encounter oom online, how to solve it?
Minio fragment upload lifting fragment size limit - chunk size must be greater than 5242880
7.合并与分割
存储过程试炼1--爱的初相识
How to choose a good futures company for futures account opening?
【并发编程系列9】阻塞队列之PriorityBlockingQueue,DelayQueue原理分析
Minimum handling charges and margins for futures companies
MySQL快速比较数据库表数据
Day 11. Evidence for a mental health crisis in graduate education
我的大四
GBASE 8C——SQL参考 5 全文检索
inno setup 打包 jar + h5 + mysql + redis 成 exe
Seven enabling schemes of m-dao help Dao ecology move towards mode and standardization
Personal collection code cannot be used for business collection
How can seektiger go against the cold air in the market?
我想不通,MySQL 为什么使用 B+ 树来作索引?
Mysql分组后时并行统计数量
vscode打造golang开发环境以及golang的debug单元测试