Pytorch学习(1)

1.tensor理解

在pytorch 中没有string类型,那么如何表示字符串呢,例如表示一个(“hello world").因此我们通过引入张量(tensor)来表示字符串。pytorch本质是一个利用CPU或者GPU大型数据的框架。

🔵一维tensor:一个变量(字符串、整型)

🔵二维tensor :神经网路哟出入层与非线性层之间的权重值

🔵三维tensor:图片(处理批次、宽、高)

🔵四维tensor:图片(处理批次、颜色、宽、高)

Tensor与tensor区别:Tensor创建采用shape参数、小写tensor直接写入要创建的数据。

torch.FloatTensor(1)
tensor([9.1477e-41])

torch.tensor([1.1,2.2])
tensor([1.1000, 2.2000])

注:为初始化的tensor,输入神经网络中会造成数据,超出数据范围。

2.API操作

torch.rand() 随机生成[0,1]之间的数据

torch.rand_like(a) 随机生成(0,1)之间跟a一样维度的数据

torch.randint(1,10,(3,3)随机生成0-10之间(3,3)的数据

torch.randn()正态分布

torch.arange(0,10,2) 生成等差数列 min=0 max=10 差为2

torch.linsapce(0,10,steps=4) 切成4份 min=0 max=10

torch.ones() 生成单位矩阵

torch.zeros() 生成零矩阵

torch.eye()生成对角矩阵

torch.full([1,10],8) 生成(1,10)全为8的数据

torch.randperm(10)随机打乱0到9之间的数

3.索引

4.index_select 函数使用

torch.index_select(input,dim,index,out=None) 函数返回的是沿着输入张量的指定维度的指定索引号进行索引的张量子集,其中输入张量、指定维度和指定索引号就是 torch.index_select(input,dim,index,out=None) 函数的三个关键参数,函数参数有:

input(Tensor) - 需要进行索引操作的输入张量;

dim(int) - 需要对输入张量进行索引的维度;

index(LongTensor) - 包含索引号的 1D 张量;

out(Tensor, optional) - 指定输出的张量。比如执行 torch.zeros(2, 2, out = tensor_a),相当于执行 tensor_a = torch.zeros(2, 2);

接下来使用 torch.index_select(input,dim,index,out=None) 函数分别对 1D 张量、2D 张量和 3D 张量进行索引.

>>> import torch
>>> # 创建1D张量
>>> a = torch.arange(0, 9)
>>> print(a)
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> # 获取1D张量的第1个维度且索引号为2和3的张量子集
>>> print(torch.index_select(a, dim = 0, index = torch.tensor([2, 3])))
tensor([2, 3])
>>> # 创建2D张量
>>> b = torch.arange(0, 9).view([3, 3])
>>> print(b)
tensor([[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]])
>>> # 获取2D张量的第2个维度且索引号为0和1的张量子集(第一列和第二列)
>>> print(torch.index_select(b, dim = 1, index = torch.tensor([0, 1])))
tensor([[0, 1],
        [3, 4],
        [6, 7]])
>>> # 创建3D张量
>>> c = torch.arange(0, 9).view([1, 3, 3])
>>> print(c)
tensor([[[0, 1, 2],
         [3, 4, 5],
         [6, 7, 8]]])
>>> # 获取3D张量的第1个维度且索引号为0的张量子集
>>> print(torch.index_select(c, dim = 0, index = torch.tensor([0])))
tensor([[[0, 1, 2],
         [3, 4, 5],
         [6, 7, 8]]])

「由于 index_select 函数只能针对输入张量的其中一个维度的一个或者多个索引号进行索引,因此可以通过 PyTorch 中的高级索引来实现。」

获取 1D 张量 a 的第 1 个维度且索引号为 2 和 3 的张量子集: torch.index_select(a, dim = 0, index = torch.tensor([2, 3]))⟺a[[2, 3]];

获取 2D 张量 b 的第 2 个维度且索引号为 0 和 1 的张量子集(第一列和第二列): torch.index_select(b, dim = 1, index = torch.tensor([0, 1]))⟺b[:, [0, 1]];

创建 3D 张量 c 的第 1 个维度且索引号为 0 的张量子集: torch.index_select(c, dim = 0, index = torch.tensor([0]))⟺c[[0]];

index_select 函数虽然简单,但是有几点需要注意:

index 参数必须是 1D 长整型张量 (1D-LongTensor);

>>> import torch
>>> index1 = torch.tensor([1, 2])
>>> print(index.type())
torch.LongTensor
>>> index2 = torch.tensor([1., 2.])
>>> print(index2.type())
torch.FloatTensor
>>> index3 = torch.tensor([[1, 2]])
>>> # 创建1D张量
>>> a = torch.arange(0, 9)
>>> print(torch.index_select(a, dim = 0, index = index1))
tensor([1, 2])
>>> # print(torch.index_select(a, dim = 0, index = index2))
RuntimeError: index_select(): Expected dtype int64 for index
>>> # print(torch.index_select(a, dim = 0, index = index3))
IndexError: index_select(): Index is supposed to be a vector

使用 index_select 函数输出的张量维度和原始的输入张量维度相同。这也是为什么即使在对输入张量的其中一个维度的一个索引号进行索引 (此时可以使用基本索引和切片索引) 时也需要使用 PyTorch 中的高级索引方式才能与 index_select 函数等价的原因所在;

>>> import torch
>>> # 创建2D张量
>>> d = torch.arange(0, 4).view([2, 2])
>>> # 使用index_select函数索引
>>> d1 = torch.index_select(d, dim = 0, index = torch.tensor([0]))
>>> print(d1)
tensor([[0, 1]])
>>> print(d1.size())
torch.Size([1, 2])
>>> # 使用PyTorch中的高级索引
>>> d2 = d[[0]]
>>> print(d2)
tensor([[0, 1]])
>>> print(d2.size())
torch.Size([1, 2])
>>> # 使用基本索引和切片索引
>>> d3 = d[0]
>>> print(d3)
tensor([0, 1])
>>> print(d3.size())
torch.Size([2])

通过上面的代码可以看出,三种方式索引出来的张量子集中的元素都是一样的,不同的是索引出来张量子集的形状,index_select 函数对输入张量进行索引可以使用高级索引实现。

原文地址:http://mp.weixin.qq.com/s?__biz=Mzg5NzE1NzM4NA==&mid=2247487326&idx=1&sn=f97761f5629c6085b919841e1eb3c7f8&chksm=c0775f39f700d62f6d422d3581b7525eaa12ebd0c6600d49f1545d5ff15f4e19a0cebd4e97a2&token=817977641&lang=zh_CN#rd

5.unsqueeze

unsqueeze扩展维度(在每个维度的前面插入)

torch.Size([1,4,1,28,28])
a.unsqueeze(0).shape
torch.Size([1,1,4,1,28,28])

6.squeeze

维度删减

torch.Size([1,32,1,1])
b.squeeze().shape #把各个维度为1都进行压缩
torch.size([32])
b.squezze(0).shape
torch.size([32,1,1])

7.expand

维度扩展

torch.Size([1,32,1,1])
b.expand(4,32,14,14).shape  
torch.Size([4,32,14,14]) #把维度size为1进行扩展 不是1的不变

torch.Size([4,32,14,14])
b.expand(-1,32,-1,-1).shape
torch.Size([1,32,1,1]) # 将之前进行扩展的还原

8.矩阵相关操作

(1) .t 矩阵转置 只能操作一

(2)Transpose

(3)permute

一次可以执行多次的transpose 参数为各个位置序号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值