您现在的位置是:热点 >>正文
详解PyTorch基本操作
热点52369人已围观
简介...
什么是 PyTorch?
PyTorch是一个基于Python的科学计算包,提供最大灵活性和速度的深度学习研究平台。
张量
张量类似于NumPy 的n 维数组,此外张量也可以在 GPU 上使用以加速计算。
让我们构造一个简单的张量并检查输出。首先让我们看看我们如何构建一个 5×3 的未初始化矩阵:
import torch x = torch.empty(5, 3) print(x)
输出如下:
tensor([[2.7298e+32, 4.5650e-41, 2.7298e+32], [4.5650e-41, 0.0000e+00, 0.0000e+00], [0.0000e+00, 0.0000e+00, 0.0000e+00], &nb详解sp; [0.0000e+00, 0.0000e+00, 0.0000e+00], [0.0000e+00, 0.0000e+00, 0.0000e+00]])
现在让我们构造一个随机初始化的矩阵:
x = torch.rand(5, 3) print(x)
输出:
tensor([[1.1608e-01, 9.8966e-01, 1.2705e-01], [2.8599e-01, 5.4429e-01, 3.7764e-01], [5.8646e-01, 1.0449e-02, 4.2655e-01], Py [2.2087e-01, 6.6702e-01, 5.1910e-01], [1.8414e-01, 2.0611e-01, 9.4652e-04]])
直接从数据构造张量:
x = torch.tensor([5.5, 3]) print(x)
输出:
tensor([5.5000, 3.0000])
创建一个统一的长张量。
x = torch.LongTensor(3, 4) x tensor([[94006673833344, 210453397554, 206158430253, 193273528374],  To; [ 214748364849, 210453397588, 249108103216, 223338299441], [ 210453397562, 197568495665, 206158430257, 240518168626]]) 「浮动张量。」 x = torch.FloatTensor(3, 4) x tensor([[-3.1152e-18, 3.0670e-41, 3.5032e-44, 0.0000e+00],  rc; [ nan, 3.0670e-41, 1.7753e+28, 1.0795e+27], [ 1.0899e+27, 2.6223e+20, 1.7465e+19, 1.8888e+31]]) 「在范围内创建张量」 torch.arange(10, dtype=torch.float) tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]) 「重塑张量」 x = torch.arange(10,&h基nbsp;dtype=torch.float) x tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
使用 .view重塑张量。
x.view(2, 5) tensor([[0., 1., 2., 3., 4.], [5., 6., 7., 8., 9.]])
-1根据张量的大小自动识别维度。
x.view(5, -1) tensor([[0., 1.], [2., 3.], &nbs本操p; [4., 5.], [6., 7.], [8., 9.]]) 「改变张量轴」
改变张量轴:两种方法view和permute
view改变张量的顺序,而permute只改变轴。
x1 = torch.tensor([[1., 2., 3.], [4., 5., 6.]]) print("x1: ", x1) print(" x1.shape: ", x1.shape) print(" x1.view(3, -1): ", x1.view(3 , -1)) print(" x1.permute(1, 0): ", x1.permute详解(1, 0)) x1: tensor([[1., 2., 3.], [4., 5., 6.]]) x1.shape: torch.Size([2, 3]) x1.view(3, -1): tensor([[1., 2.], [3., 4.], [5., 6.]]) x1.permute(1, 0): tensor([[1., 4.], &Pynbsp; [2., 5.], [3., 6.]]) 张量运算
在下面的示例中,我们将查看加法操作:
y = torch.rand(5, 3) print(x + y)
输出:
tensor([[0.5429, 1.7372, 1.0293], [0.5418, 0.6088, 1.0718], [1.3894, 0.5148, 1.2892], [0.9626,&nbTosp;0.7522, 0.9633], [0.7547, 0.9931, 0.2709]])
调整大小:如果你想调整张量的形状,你可以使用“torch.view”:
x = torch.randn(4, 4) y = x.view(16) # 大小-1是从其他维度推断出来的 z = x.view(-1, 8) print(x.size(), y.size(), z.size())
输出:
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8]) PyTorch 和 NumPy的转换
NumPy 是Python 编程语言的库,增加了对大型、多维数组和矩阵的支持,以及对这些数组进行操作的大量高级数学函数集合。
将Torcrch中Tensor 转换为 NumPy 数组,反之亦然是轻而易举的!
Torch Tensor 和 NumPy 数组将共享它们的底层内存位置 ,改变一个将改变另一个。
「将 Torch 张量转换为 NumPy 数组:」 a = torch.ones(5) print(a)
输出:tensor([1., 1., 1., 1., 1.])
b = a.numpy() print(b)
输出:[1., 1., 1., 1., 1.]
让我们执行求和运算并检查值的变化:
a.add_(1) print(a) print(b)
输出:
tensor([2., 2., 2., 2., 2.]) [2. 2. 2. 2. 2.] 「将 NumPy 数组转换为 Torch 张量:」 import numpy as no a = np.ones(5) b = torch.from_numpy(a)h基 np.add(a, 1, out=a) print(a) print(b)
输出:
[2. 2. 2. 2. 2.] tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
所以,正如你所看到的,就是这么简单!
接下来在这个 PyTorch 教程博客上,让我们看看PyTorch 的 AutoGrad 模块。
AutoGrad
该autograd包提供自动求导为上张量的所有操作。
它是一个按运行定义的框架,这意味着您的反向传播是由您的代码运行方式定义的,并且每次迭代都可以不同。
torch.autograd.function (函数的反向传播) torch.autograd.functional (计算图的反向传播) torch.autograd.gradcheck (数值梯度检查) torch.autograd.anomaly_mode (在自动求导时检测错误产生路径) torch.autograd.grad_mode (设置是否需要梯度)本操 model.eval() 与 torch.no_grad() torch.autograd.profiler (提供 function 级别的统计信息) 「下面使用 Autograd 进行反向传播。」
如果requires_grad=True,则 Tensor 对象会跟踪它是如何创建的。
x = torch.tensor([1., 2., 3.], requires_grad = True) print('x: ', x) y = torch.tensor([10., 20., 30.], requires_grad = True) print('y: ', y) z = x + y print(' z = x + y') print('z:', z) x: 详解; tensor([1., 2., 3.], requires_grad=True) y: tensor([10., 20., 30.], requires_grad=True) z = x + y z: tensor([11., 22., 33.], grad_fn=)
因为requires_grad=True,z知道它是通过增加两个张量的产生z = x + y。
s = z.sum() print(s) tensor(66., grad_fn=)
s是由它的数字总和创建的。当我们调用.backward(),反向传播从s开始运行。然后可以计算梯度。
s.backward() print('x.grad: ', x.grad) print('y.grad: ', Py;y.grad) x.grad: tensor([1., 1., 1.]) y.grad: tensor([1., 1., 1.])
下面例子是计算log(x)的导数为1 / x
import torch x = torch.tensor([0.5, 0.75], requires_grad=True) # 1 / x y = torch.log(x[0] * x[1]) y.backward() x.grad # tensor([2.0000, 1.3333])
Tags:
相关文章
谷歌母公司Alphabet将于4月28日发布2021年第一季度财报
热点新浪科技讯 北京时间4月12日下午消息,谷歌母公司Alphabet(NASDAQ: GOOG, GOOGL)宣布,将于美国东部时间4月27日股市收盘后(北京时间4月28日)发布2021年第一季度财报。...
【热点】
阅读更多OpenAI 计划将与微软收入分成比例大幅降低,预计新增 500 亿美元收入
热点根据美国科技媒体《The Information》的报道,人工智能公司 OpenAI 正在寻求调整与其主要投资者微软之间的财务协议。这一调整的主要目的是希望能够保留更多的收入,以应对其日益增长的计算开...
【热点】
阅读更多标签: 网易云音乐 黑胶VIP 不正当竞争 88VIP
热点云梦级12月1日云梦级消息,人民法院报 11 月 28 日公布一则案例。近日,湖北省武汉东湖新技术开发区人民法院审结了一起不正当竞争案,认定某店铺低价转售某音乐平台 VIP 会员权益的行为构成不正当竞...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.xbiao.com/search/index?tpf&wd=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,0dr
- https://www.lauben.com/en/?action=SEARCH&s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,Y60
- https://www.abbreviations.com/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】pEf
- https://www.sbaad.no/search/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,gdL
- https://www.nslcleaders.org/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,fiG
- https://www.metaltix.com/?view=tours&filter=search&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,yGr
- https://www.toy-people.com/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,P3q
- https://www.randjleather.co.uk/index.php?route=product/search&tag=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,et9
- https://www.whatsonstage.com/search?cat=64&term=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,LVu
- https://jihoon471.cafe24.com/product/search.html?banner_action=&keyword=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】ycv
- https://www.wmfe.org/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,rhA
- https://www.schenckprocess.com/search?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,wIX
- https://www.quarterjacksurgery.co.uk/?AT=3rBy&s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,RIU
- https://www.wheels24.gr/?action=CHECK&s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,cIn
- https://www.synonyms.com/antonyms/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,HWr
- https://www.sportbiketracktime.com/index.php?route=product/search&tag=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,9pl
- https://www.stonerealestate.com.au/?cat=100&s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,Lo3
- https://www.unclefossil.com/search/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,big
- https://www.nctsn.org/search?cat=191&query=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,mtL
- https://www.whirlpool.com.hk/chinese/catalogsearch/result?cat=30&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,26H
- https://www.monroecounty.gov/search/?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,zej
- https://www.localfootdoc.com/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,a9n
- https://www.so.com/s?ie=utf-8&src=360sou_home&_re=0&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,FoF
- https://www.nss.nhs.scot/search?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,jr7
- https://www.tahiti-infos.com/search/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,qqP
- https://www.remit.co.jp/vi/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,OSv
- https://www.reginatangoshoes.com/en/catalogsearch/result/index/?T=1686132113407&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,9US
- https://www.pinkpanda.ro/magazin/ingrijirea-tenului?page=87&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,o6d
- https://www.yli.ro/catalogsearch/result/?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,1qW
- https://www.universal-music.co.jp/?ID=1sIcckCTeBS8FjzCshAW9w53s8gr691o&s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,43B
- https://www.urmc.rochester.edu/news/search?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,cmA
- https://www.springfieldlibrary.org/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,903
- https://www.lee.com/search?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,0qY
- https://www.zisha.com/shop/list.shtml?wap_keyword=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,HNE
- https://www.simcoemuskokahealth.org/SearchResults.aspx?SearchQuery=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,b66
- https://www.kyuhoshi.com/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,907
- https://www.zalora.sg/catalog/?cat=112&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,1aF
- https://www.supersauna.de/catalogsearch/result/?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,gex
- https://www.umc.edu/search.aspx?ID=I4gnDKRS991MFcIDDzoYze4aWSAszAm7&q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,lrZ
- https://www.pearson.com/en-us/search.html?aq=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,Nqg
- https://www.dba.dk/soeg/?soeg=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】ebA
- https://www.welshmountainzoo.org/shop/index.php?routeproduct/search&tag=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,H5D
- https://www.tumblr.com/tagged/谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,LUz
- https://www.southmeadows.co.uk/2019/07/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,Eqh
- https://www.therapeuticassociates.com/?cat=5&s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,0bc
- https://www.qidulp.com/search?query=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,dMA
- https://www.tailoy.com.pe/catalogsearch/result/?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,NpZ
- https://www.sic-info.org/vi/?s=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,jxb
- https://dict.naver.com/dict.search?dicQuery&query=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】5BP
- https://www.sweat.com/collections/vendors?q=谷歌霸屏24小时见效-实现思路【tg:seogoogle987】,i8v