栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

PyTorch中的Tensor基本操作总结

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PyTorch中的Tensor基本操作总结

Tensor是PyTorch 存储数据的基本单元, 数据都是存储在tensor中然后进行运算的. 本文总结 PyTorch中tensor的一些基本操作. 包括: 1, 创建tensor. 2, 改变Tensor中数据的type和shape. 3, tensor间的基本运算.

创建tensor
In [1]: import torch
In [2]: import numpy as np

从 int list中创建
In [3]: a = [1,2,3]
In [4]: t_a = torch.tensor(a)
In [5]: print(t_a)
tensor([1, 2, 3])

从numpy array中创建
In [6]: b = np.array([1, 2, 3, 4], dtype=np.int32)
In [7]: t_b = torch.from_numpy(b)
In [8]: print(t_b)
tensor([1, 2, 3, 4], dtype=torch.int32)

直接创建ones矩阵
In [9]: t_c = torch.ones(3,3)
In [10]: print(t_c)
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])

直接创建随机矩阵范围 [0,1)
In [11]: t_d = torch.rand(2,3)
In [12]: print(t_d)
tensor([[0.6955, 0.8926, 0.9778],
        [0.3022, 0.4747, 0.9094]])
改变tensor的形状与数据type 
改变tensor内数据的type
In [13]: t_a_new = t_a.to(torch.int64)

In [14]: print(t_a_new.dtype)
torch.int64


改变tensor shape 
In [15]: t = torch.zeros(20)

In [16]: t_reshape = t.reshape(4, 5)

In [17]: print(t_reshape.shape)
torch.Size([4, 5])


tensor转置
In [18]: t = torch.rand(4, 5)

In [19]: t_transpose = torch.transpose(t, 0, 1)

In [23]: print(t.shape, "-->",t_transpose.shape)
torch.Size([4, 5]) --> torch.Size([5, 4])


维度压缩, 消除多余的维度.
In [25]: t = torch.ones(1, 3, 4, 5)

In [26]: t_squeeze = torch.squeeze(t, 0)

In [27]: print(t_squeeze.shape)
torch.Size([3, 4, 5])
tensor中的数学运算
In [30]: t1 = 5 + torch.rand(3, 4) * 2

In [31]: t2 = torch.normal(mean=0, std=1, size=(3, 4))



t1 与t2 点乘(element wise)
In [32]: t3 = torch.multiply(t1, t2)

In [33]: print(t3)
tensor([[ 3.7544,  1.0344, -6.6131,  2.6453],
        [ 0.0394,  8.2370, 12.3229,  3.4751],
        [ 4.4234, 12.7690, -0.9023, -3.5027]])



X乘
In [35]: t4 = torch.torch.matmul(t1, torch.transpose(t2, 0, 1))

In [36]: print(t4)
tensor([[ 0.8210, 23.2943, 11.9209],
        [-0.1248, 24.0745, 13.7630],
        [-1.4852, 26.2885, 12.7874]])




按照某轴的 mean, sum,std运算. 
如: t1是个3行4列 的矩阵
In [37]: print(t1)
tensor([[6.6838, 5.7557, 5.2833, 6.8798],
        [5.5056, 6.8812, 5.3308, 5.7699],
        [5.3363, 6.5976, 6.3740, 6.0077]])

按t1的列计算平均值
In [38]: t5 = torch.mean(t1, axis=0)
In [39]: print(t5)
tensor([5.8419, 6.4115, 5.6627, 6.2191])

按行计算总和:
In [40]: t6 = torch.sum(t1, axis=1)
In [41]: print(t6)
tensor([24.6026, 23.4875, 24.3156])

参考自:  Machine Learning with PyTorch and Scikit-Learn Book

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/886917.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号