ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PyTorch Tensor(二)

2021-05-17 20:57:46  阅读:198  来源: 互联网

标签:None tensor torch 张量 PyTorch out Tensor


Tensor常见命令

1.创建操作

1.1 torch.eye

返回一个2维张量,对角线位置全1,其它位置全0,返回值类型Tensor

torch.eye(n, m=None, out=None)

参数:

  • n (int ) – 行数
  • m (int, optional) – 列数.如果为None,则默认为n
  • out (Tensor, optinal) - Output tensor
torch.eye(3)
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

1.2 torch.linspace

torch.linspace(start, end, steps=100, out=None) → Tensor

返回一个1维张量,包含在区间startend 上均匀间隔的steps个点。 输出1维张量的长度为steps注意steps不是步长。

参数:

  • start (float) – 序列的起始点
  • end (float) – 序列的最终值
  • steps (int) – 在start 和 end间生成的样本数
  • out (Tensor, optional) – 结果张量
torch.linspace(3, 12, steps=5)
tensor([ 3.0000,  5.2500,  7.5000,  9.7500, 12.0000])

1.3 torch.rand和torch.randn

rand是[0,1)均匀分布,randn是标准正太分布

torch.rand(*sizes, out=None) → Tensor
torch.randn(*sizes, out=None) → Tensor

形状由可变参数size确定

x = torch.rand(2,2)
x
tensor([[0.9483, 0.6083],
        [0.0579, 0.0638]])
y = torch.randn(3,3)
y
tensor([[ 0.3220, -2.4360, -0.0790],
        [-0.1366, -0.5444,  1.4342],
        [ 0.7295, -1.3341,  0.2022]])

类似的ones,zeros用法跟上面一样。

1.4 torch.arange() 和torch.range()

torch.arange(start, end, step=1, out=None) → Tensor
torch.range(start, end, step=1, out=None) → Tensor

返回一个1维张量,长度为 floor((end−start)/step)。包含从start到end,以step为步长的一组序列值(默认步长为1)
参数:

  • start (float) – 序列的起始点
  • end (float) – 序列的最终值
  • step (int) – 相邻点的间隔大小【步长】
  • out (Tensor, optional) – 结果张量
    建议使用torch.arange(),另外,python讲究左闭右开
torch.arange(1,50,10)
tensor([ 1, 11, 21, 31, 41])

2.切片,索引,连接,换位

2.1 torch.cat()

torch.cat(inputs, dimension=0) → Tensor

在给定维度上对输入的张量序列seq 进行连接操作。
参数:

  • inputs (sequence of Tensors) – 可以是任意相同Tensor 类型(float,long,duoble)python 序列
  • dimension (int, optional) – 沿着此维连接张量序列
## 例子 
x = torch.randn(1,3)
x
tensor([[-0.0675, -0.5289, -0.3090]])
y = torch.cat((x,x,x),0)
y
tensor([[-0.0675, -0.5289, -0.3090],
        [-0.0675, -0.5289, -0.3090],
        [-0.0675, -0.5289, -0.3090]])
z = torch.cat((x,x,x),1)
z
tensor([[-0.0675, -0.5289, -0.3090, -0.0675, -0.5289, -0.3090, -0.0675, -0.5289,
         -0.3090]])

torch.chunk(),torch.gather(),torch.index_select(),torch.masked_select()查看官方文档:https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch/

2.2 torch.squeeze

torch.squeeze(input, dim=None, out=None)

将输入张量形状中的1去除并返回。 如果输入是形如(A×1×B×1×C×1×D),那么输出形状就为: (A×B×C×D)

当给定dim时,那么挤压操作只在给定维度上。例如,输入形状为: (A×1×B), squeeze(input, 0)将会保持张量不变,只有用 squeeze(input, 1),形状会变成 (A×B)。

注意: 返回张量与输入张量共享内存,所以改变其中一个的内容会改变另一个。

参数:

  • input (Tensor) – 输入张量
  • dim (int, optional) – 如果给定,则input只会在给定维度挤压
  • out (Tensor, optional) – 输出张量
# 例
x = torch.ones(2,1,2,1,2)
x.size()
torch.Size([2, 1, 2, 1, 2])
y = torch.squeeze(x)
y.size()
torch.Size([2, 2, 2])
y = torch.squeeze(x, 0)
y.size()
torch.Size([2, 1, 2, 1, 2])
y = torch.squeeze(x, 1)
y.size()
torch.Size([2, 2, 1, 2])

2.3 torch.unsqueeze()

torch.unsqueeze(input, dim, out=None)

返回一个新的张量,对输入的指定位置插入维度 1

注意: 返回张量与输入张量共享内存,所以改变其中一个的内容会改变另一个。

# 例
x = torch.Tensor([1,2,3]) # 这里tensor小写大写没有区别
y = torch.unsqueeze(x,0)
z = torch.unsqueeze(x, 1)
print(y)
print(z)
tensor([[1., 2., 3.]])
tensor([[1.],
        [2.],
        [3.]])

2.4 torch.t 和transpose 实现转置

z

tensor([[1.],
[2.],
[3.]])

torch.t(z)
tensor([[1., 2., 3.]])

参考:
PyTorch中文文档

标签:None,tensor,torch,张量,PyTorch,out,Tensor
来源: https://blog.csdn.net/qq_39809262/article/details/116948267

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有