千家信息网

Pytorch中的Tensor常用操作有哪些

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要为大家展示了"Pytorch中的Tensor常用操作有哪些",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Pytorch中的Tensor常用操作
千家信息网最后更新 2026年11月01日Pytorch中的Tensor常用操作有哪些

这篇文章主要为大家展示了"Pytorch中的Tensor常用操作有哪些",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Pytorch中的Tensor常用操作有哪些"这篇文章吧。

1、创建Tensor

import torch#经典方式device = torch.device("cuda:0")x = torch.tensor([1,2],dtype = torch.float32,device = device,requires_grad=True)w = sum(2 * x)w.backward()print(x.device)print(x.dtype)print(x.grad)#Tensory = torch.Tensor([1,2,3])#等价于y = torch.FloatTensor([1,2,3])#32位浮点型#后者声明打开梯度y.requires_grad = True#还有其他类型,常用的torch.LongTensor(2,3)torch.shortTensor(2,3)torch.IntTensor(2,3)w = sum(2 * y)w.backward()print(y.grad)print(y.dtype)

输出:

cuda:0
torch.float32
tensor([2., 2.], device='cuda:0')
tensor([2., 2., 2.])
torch.float32

和numpy类似的创建方法

x = torch.linspace(1,10,10,dtype = torch.float32,requires_grad = True)y = torch.ones(10)z = torch.zeros((2,4))w = torch.randn((2,3))#从标准正态分布(均值为0,方差为1)上随机采用,高斯噪声点,而rand相当于在0,1间随机采样#torch.normal()????print(x)print(y)print(z)print(w)

输出

tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.], requires_grad=True)
tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.]])
tensor([[-0.6505, 1.3897, 2.2265],
[-1.7815, -1.8194, -0.4143]])

从numpy转换

np_data = np.arange(2,13,2).reshape((2,3))torch_data = torch.from_numpy(np_data)#numpy转tensorprint('numpy',np_data)print('torch',torch_data)

输出

numpy [[ 2 4 6]
[ 8 10 12]]

torch tensor([[ 2, 4, 6],
[ 8, 10, 12]], dtype=torch.int32)

2、组合

import torchx = torch.arange(0,10,1).reshape(2,-1)#size=(2,5)y = torch.ones(10).reshape(2,-1)#size=(2,5)print(x)print(y)w = torch.cat((x,y),dim = 0)#默认从size最左边开始,这里结果为:(2+2,5)z = torch.cat((x,y),dim = 1)#(2,5+5)print(w,w.size())print(z,z.size())#还有种stack()

输出:

tensor([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
tensor([[0., 1., 2., 3., 4.],
[5., 6., 7., 8., 9.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]]) torch.Size([4, 5])
tensor([[0., 1., 2., 3., 4., 1., 1., 1., 1., 1.],
[5., 6., 7., 8., 9., 1., 1., 1., 1., 1.]]) torch.Size([2, 10])

3、数据类型转换

法一

x = torch.rand((2,2),dtype = torch.float32)print(x.dtype)x = x.double()print(x.dtype)x = x.int()print(x)

输出:

torch.float32
torch.float64
tensor([[0, 0],
[0, 0]], dtype=torch.int32)

法二

x = torch.LongTensor((2,2))print(x.dtype)x = x.type(torch.float32)print(x.dtype)

输出:

torch.int64
torch.float32

4、矩阵计算

x = torch.arange(0,4,1).reshape(2,-1)print(x)print(x * x )#直接相乘print(torch.mm(x,x))#矩阵乘法print(x + 1)#广播print(x.numpy())#转换成numpy

输出:

tensor([[0, 1],
[2, 3]])
tensor([[0, 1],
[4, 9]])
tensor([[ 2, 3],
[ 6, 11]])
tensor([[1, 2],
[3, 4]])
[[0 1]
[2 3]]

5、维度变化

主要是对维度大小为1的升降维操作。

 torch.squeeze(input)#去掉维度为1的维数 torch.unsqueeze(input,dim)#指定位置增加一维

以上是"Pytorch中的Tensor常用操作有哪些"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

输出 常用 内容 篇文章 维度 矩阵 类型 学习 帮助 乘法 位置 噪声 均值 大小 数据 方差 方式 方法 易懂 更多 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技公司经营范围 互联网金融监管科技数据 中国互联网科技发展 广州凡科互联网科技 四川狮子座互联网科技 互联网科技宣传片 剑昆互联网络科技有限公司临桂 华服互联网科技官网 南山科技园互联网公司有哪些 互联网为啥属于高科技 浙南科技城互联网大厦建设工程 科技互联网海报 众安科技工业互联网合作 中天科技中国工业互联网 神州科技互联网有限公司 上海同邻互联网科技有限公司规模 互联网界的黑科技 银川新华互联网科技学校原画 亚光科技低轨卫星互联网 互联网科技创新方面的资料 电子科技大学互联网 海南科技产业互联网平台 互联网金融中的现代科技 互联网科技革命什么开始 为客找房互联网科技公司怎么样 互联网营销推广黑科技 上海万顺科技互联网有限公司招聘 中国互联网公司的科技投入 上海旺乐尼互联网科技有限公司 互联网 科技节活动 微视频大赛
0