Tensor的常见操作有哪些,具体用法是怎样的
Admin 2022-07-06 群英技术资讯 481 次浏览
Tensors are similar to NumPy's ndaeeays,不同的是可以在GPU上使用和加速计算。
导入包
from __future__ import print_function import torch
x = torch.empty(5,3) print(x)
out
tensor([[ 1.4395e-36, 4.5848e-41, 1.4395e-36], [ 4.5848e-41, 1.4395e-36, 4.5848e-41], [ 1.4395e-36, 4.5848e-41, 2.8026e-45], [-1.9501e+00, 8.5165e+23, 0.0000e+00], [ 2.5223e-43, 0.0000e+00, 0.0000e+00]])
x = torch.rand(5,3) print(x)
out
tensor([[ 0.8074, 0.9175, 0.8109], [ 0.3313, 0.5902, 0.9179], [ 0.6562, 0.3283, 0.9798], [ 0.8218, 0.0817, 0.4454], [ 0.5934, 0.0040, 0.3411]])
... x = torch.zeros(5,3,dtype = torch.long) print(x) ...
out
tensor([[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0]])
x = torch.tensor([5.5,3]) print(x)
out
tensor([ 5.5000, 3.0000])
在原有tnesor的基础上形成新的tensor,会继承原有tensor的shapee和dtype等属性,当然我么也可以修改这些属性
x = x.new_ones(5,3,dtype = torch.double) print(x) x = torch.randn_like(x,dype = torch.float) print(x)
out
tensor([[ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.]], dtype=torch.float64) tensor([[-0.0730, -0.0716, -0.8259], [-1.7004, 0.8790, -0.0659], [-0.8969, 0.8736, -0.6035], [-0.1539, -2.9178, -0.7456], [-0.0245, 0.4075, 1.4904]])
print(x.size())
out
torch.Size([5, 3])
torch.size是一个元组,支持所有元组(tuple)的操作
方法一L
print(x+y)
方法二
print(torch.add(x,y))
方法三:输出给额外的tensor
result = torch.empty(5,3) torch.add(x,y ,out= result) print (result)
方法四:原地替换-结果存放在y中
print(y)
所有原地替换tensor的操作都有后缀,比如x.copy(y),会改变x
print(x[:1]
out
tensor([-0.0716, 0.8790, 0.8736, -2.9178, 0.4075])
x = torch.randn(4,4) y = x.view(16) z = x.view(-1,8) # the size -1 is inferred from other dimensions print (x.size(),y.xize(),z.size())
out
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
x = torch.rnadn(1) print(x) print(x.item())
a = torch.ones(5) print (a)
out
tensor([ 1., 1., 1., 1., 1.])
并且改变tensor的值会同时改变numpy的值
a.add_(1) print(a) print(b)
out
tensor([ 2., 2., 2., 2., 2.]) [ 2. 2. 2. 2. 2.]
import numpy as np a = np.ones(5) b = torch.from_numpy(a) np.add(a,1,out = a ) print(a) print(b)
out
[ 2. 2. 2. 2. 2.] tensor([ 2., 2., 2., 2., 2.], dtype=torch.float64)
所有在cpu上的tensor都支持numpy转化,除了char形的tensor
Tensors 可以被移动到其他设备使用.to的方法
... if torch.cuda.is_avaulable(): device = torch.device(“cuda”) y = torch.ones_like(x,device = devcie) x= x.to(device) z = x+y print(z) print(z.to(“cpu”,torch.double)) ...
out
tensor([-1.0620], device='cuda:0') tensor([-1.0620], dtype=torch.float64)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
NumPy(Numerical Python)是Python的一种开源的数值计算扩展。这种工具可用来存储和处理大型矩阵。但其实NumPy还可以绘制图画,本文将为大家介绍如何通过NumPy绘制彩色图画,感兴趣的小伙伴可以了解一下
python中temp是什么?根据操作的过程进行临时保存的文件技术。python中临时文件及文件夹使用。使用的是tempfile包
这篇文章主要介绍了使用Python读写多个sheet文件,本文即介绍多个sheet文件的读入及处理数据后写出到新excel文件的操作过程,可以提高大家工作效率,需要的小伙伴可以参考一下
在训练yolo网络检测目标时,需要根据待检测目标的位置大小分布情况对anchor进行调整,使其检测效果尽可能提高,下面这篇文章主要给大家介绍了关于YOLOv5目标检测之anchor设定的相关资料,需要的朋友可以参考下
这篇文章给大家分享的是有关opencv图形绘制的内容,opencv是一个强大的图像处理和计算机视觉库,我们使用python opencv可以绘制圆形,矩形,线段等多种常用图形。下面我们就一起来看看怎么实现opencv图形绘制吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008