python机器学习pytorch 张量基础教程.docx

上传人:太** 文档编号:72759734 上传时间:2023-02-13 格式:DOCX 页数:9 大小:173.85KB
返回 下载 相关 举报
python机器学习pytorch 张量基础教程.docx_第1页
第1页 / 共9页
python机器学习pytorch 张量基础教程.docx_第2页
第2页 / 共9页
点击查看更多>>
资源描述

《python机器学习pytorch 张量基础教程.docx》由会员分享,可在线阅读,更多相关《python机器学习pytorch 张量基础教程.docx(9页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、.腾龙娱乐开户(3253661767)正文 1 .初始化张量O 1. 1直接从列表数据初始化O 1.2用NumPy数组初始化O 1.3从另一个张量初始化O 1.4使用随机值或常量值初始化2.张量的属性 3 .张量运算O 3.1标准的类似numpy的索引和切片:o 3.2连接张量O 3. 3算术运算o 3. 4 单元素张量 Single-element tensors3. 5 In-place 操作 4.张量和NumPy桥接o 4. 1张量到NumPy数组o 4. 2 NumPy数组到张量t - tensor (2, 2,9 2二 22. dtypeHtorch. f 10at64)三22222

2、3D arraID arrayID array72910axis 0shape: (4,)2D arrayshape: (2, 3)shape: (4,:正文张量是一种特殊的数据结构,与数组和矩阵非常相似。在PyTorch中,我们使用张量对模 型的输入和输出以及模型的参数进行编码。张量类似于NumPy的ndarray,除了张量可以在GPU或其他硬件加速器上运行。事实上, 张量和NumPy数组通常可以共享相同的底层内存,从而无需复制数据(请参阅Bridge with NumPy) o张量也针对自动微分进行了优化(我们将在稍后的Autograd局部中看到更多相 关内容)。如果您熟悉ndarrays

3、,那么您对Tensor API会很快熟悉。#导入需要的包import torchimport numpy as np.初始化张量张量可以以各种方式初始化。请看以下例如:1.1 直接从列表数据初始化 张量可以直接从列表数据中创立。数据类型是自动推断的。#创立python list数据 data = 1, 2, 3, 4#初始化张量 . 注意:In-place操作可以节省一些内存,但在计算导数时可能会出现问题,因为会立即丧失历史 记录。因此,不鼓励使用它们。print (f,zt: t)#初始化张量 . 注意:In-place操作可以节省一些内存,但在计算导数时可能会出现问题,因为会立即丧失历史

4、记录。因此,不鼓励使用它们。print (f,zt: t)x_data = torch.tensor(data)L 2用NumPy数组初始化张量可以从NumPy数组创立(反之亦然-请参阅Bridge with NumPy) o#创立numpy数组np_array = np. array(data)# from numpy初始化张量x np = torch, from numpy(np array)1 . 3从另一个张量初始化新张量保存原张量的属性(形状shape、数据类型datatype),除非显式覆盖。#创立和x data (形状shape、数据类型datatype) 一样的张量并全部初始化

5、为1 x_ones = torch. ones_like(x_data) # retains the properties of x_dataprint (fz/0nes Tensor: n x ones) n)#创立和x_data (形状shape) 一样的张量并随机初始化,覆盖其数据类型datatype为t orch. floatx rand = torch. rand like(x data, dtype=torch. float) # overrides the datatype ofOnes Tensor:tensor (1, 1,I i, 1疥Random Tensor:tenso

6、r ( 0. 5001, 0. 2973,0. 8085, 0.9395)Ones Tensor:tensor(1., 1., 1.1,1., 1., 1.)Zeros Tensor:tensor ( 0., 0., 0.1,0., 0., 0.)2 .张量的属性张量属性描述了它们的形状Shape、数据类型Datatype和存储它们的设备Device。tensor = torch, rand(3, 4)print (fz/Shape of tensor: tensor, shape),z)print (f/zDatatype of tensor: (tensor, dtype/z)print

7、(fz/Device tensor is stored on: tensor, device)z/)Shape of tensor: torch. Size(3, 4)Datatype of tensor: torch. float32Device tensor is stored on: cpuif torch, cuda. is available():tensor 二 tensor. to(/,cuda,/)尝试列表中的一些操作。如果您熟悉NumPy API,您会发现Tensor API使用起来轻而易举。3.1标准的类似numpy的索引和切片:tensor = torch, ones(4

8、, 4)print (f/zFirst row: tensor 0,z)print (fz,First column: tensor :, 0)print (fz,Last column: tensor . . , - 1)tensor :,1 = 0print (tensor)First row: tensor(1. , 1. , 1., 1.1)First column: tensor(1. , 1. , 1. , 1.1)Last column: tensor(1. , 1. , 1. , 1.)tensor(1.) 0. , 1. , 1., 1. , 0. , 1. , 1. , 1

9、., 0. , 1. , 1.1,1., 0., 1., 1., L, 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1, 1., 1., 0., 1., 1., 1., 0., 1, 1, 1., 0., 1, 1., 1., 0., 1., 1.)3. 3算术运算This computes the matrix multiplication between two tensors, yl, y2, y3 will have the same valueyl 二 tensor tensor.Ty2 = tensor, matmul(t

10、ensor. T)y3 = torch, rand like(yl)torch. matmul(tensor, tensor. T, out=y3)IIThis computes the element-wise product, zl, z2, z3 will have the same valuezl = tensor * tensorIIz2 = tensor, mul(tensor)z3 = torch. rand_like(tensor)torch, mul(tensor, tensor, out=z3)tensor(1., 0., 1, , 1.,1., 0., 1., 1., 1

11、. , 0., 1. , 1.,如果您有一个单元素张量,例如通过将张量的所有值聚合为一个值,您可以使用以下方法将其转换为Python数值item。:agg = tensor. sum()agg item = agg. item()print (agg item, type (agg item)12.0 3. 5 In-place 操作将结果存储到操作数中的操作称为In-place操作。它们由一后缀表示。例如:X. copy_(y), X. t_(),会变 x。print (fz,tensor) n)tensor. add(5)print (tensor)tensor(1., 0., 1, ,

12、1.,1., 0., 1., 1.1,1., 0., 1., 1.6., 5,6,6.)4.张量和NumPy桥接CPU和NumPy数组上的张量可以共享它们的底层内存位置,改变一个会改变另一个。4. 1张量到NumPy数组t = torch.ones(5)print (fz,t: t)n = t. numpy ()print (fn: n)t: tensor(1., 1., 1. , 1., 1.1)n: 1. 1. 1. 1. 1.张量的变化反映在NumPy数组中。t: tensor (2., 2., 2. , 2., 2.) n: 2. 2. 2. 2. 2.4. 2 NumPy数组到张量

13、n = np. ones (5)torch. from numpy (n)NumPy数组的变化反映在张量中。np. add (n, 1, out=n)Ix data print (f/zRandom Tensor: n x rand n)1.4使用随机值或常量值初始化shape是张量维度的元组。在下面的函数中,它决定了输出张量的维度。#定义形状元组shape = (2, 3,)#随机初始化rand_tensor = torch, rand(shape)#全部初始化为1ones_tensor = torch.ones(shape)#全部初始化为0zeros tensor = torch.zero

14、s(shape) IIprint (f,zRandom Tensor: n rand tensor n)print (fz,0nes Tensor: n ones_tensor n)print (f/zZeros Tensor: n zeros_tensor/z)Random Tensor:tensor (0. 0032, 0. 5302, 0. 2832,0. 0826, 0. 3679, 0. 8727)3.张量运算这里全面介绍了超过100种张量运算,包括算术、线性代数、矩阵操作(转置、索引、切 片)、采样等。这些操作中的每一个都可以在GPU上运行(通常以比CPU更高的速度)。默认情况下,

15、张量是在CPU上创立的。我们需要使用.to方法明确地将张量移动到GPU (在检查GPU可用性之后)。请记住,跨设备复制大张量在时间和内存方面可能会很昂贵!# We move our tensor to the GPU if available1., 0., 1., 1.)3. 2连接张量您可以用来torch, cat沿给定维度连接一系列张量另请参阅torch, stack,另一个与 torch, cat.tl = torch.cat(tensor, tensor, tensor, dim=l)print (tl)1., o., 1., 1.)3. 4 单元素张量 Single-element tensors

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 解决方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com