PyTorch 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中文文档

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值