PyTorch学习笔记(二)

PyTorch学习笔记(二)

dimension:0行 1列

张量操作

torch.cat

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

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

torch.cat()可以看做 torch.split()torch.chunk()的反操作。 cat() 函数可以通过下面例子更好的理解。

参数:

  • inputs (sequence of Tensors) – 可以是任意相同Tensor 类型的python 序列。
  • dimension (int, optional) – 沿着此维连接张量序列。

例子:

>>> x = torch.randn(2, 3)
>>> x

 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
[torch.FloatTensor of size 2x3]

>>> torch.cat((x, x, x), 0)

 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
 0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735
[torch.FloatTensor of size 6x3]

>>> torch.cat((x, x, x), 1)

 0.5983 -0.0341  2.4918  0.5983 -0.0341  2.4918  0.5983 -0.0341  2.4918
 1.5981 -0.5265 -0.8735  1.5981 -0.5265 -0.8735  1.5981 -0.5265 -0.8735
[torch.FloatTensor of size 2x9]

torch.chunk

torch.chunk(tensor, chunks, dim=0)

在给定维度(轴)上将输入张量进行分块儿。

参数:

  • tensor (Tensor) – 待分块的输入张量
  • chunks (int) – 分块的个数
  • dim (int) – 沿着此维度进行分块

torch.gather

torch.gather(input, dim, index, out=None) → Tensor

沿给定轴dim,将输入索引张量index指定位置的值进行聚合。

参数:

  • input (Tensor) – 源张量
  • dim (int) – 索引的轴
  • index (LongTensor) – 聚合元素的下标
  • out (Tensor, optional) – 目标张量

对一个3维张量,输出可以定义为:

out[i][j][k] = tensor[index[i][j][k]][j][k]  # dim=0
out[i][j][k] = tensor[i][index[i][j][k]][k]  # dim=1
out[i][j][k] = tensor[i][j][index[i][j][k]]  # dim=3

例子:

>>> t = torch.Tensor([[1,2],[3,4]])
 1  2
 3  4
>>> torch.gather(t, 1, torch.LongTensor([[0,0],[1,0]]))
 1  1
 4  3
[torch.FloatTensor of size 2x2]

dim=1

i=0,j=0

out[0][0]=input[0,index[0][0]]=input[0,0]=1

i=0,j=1

out[0][1]=input[0,index[0][1]]=input[0,0]]=1

i=1,j=0

out[1][0]=input[1,index[1][0]]=input[1,1]]=4

i=1,j=1

out[1][1]=input[1,index[1][1]]=input[1,0]]=3

torch.index_select

torch.index_select(input, dim, index, out=None) → Tensor

沿着指定维度对输入进行切片,取index中指定的相应项(index为一个LongTensor),然后返回到一个新的张量, 返回的张量与原始张量_Tensor_有相同的维度(在指定轴上)。

注意: 返回的张量不与原始张量共享内存空间

参数:

  • input (Tensor) – 输入张量
  • dim (int) – 索引的轴
  • index (LongTensor) – 包含索引下标的一维张量
  • out (Tensor, optional) – 目标张量

例子:

>>> x

 1.2045  2.4084  0.4001  1.1372
 0.5596  1.5677  0.6219 -0.7954
 1.3635 -1.2313 -0.5414 -1.8478
[torch.FloatTensor of size 3x4]

>>> indices = torch.LongTensor([0, 2])
>>> torch.index_select(x, 0, indices)

 1.2045  2.4084  0.4001  1.1372
 1.3635 -1.2313 -0.5414 -1.8478
[torch.FloatTensor of size 2x4]

>>> torch.index_select(x, 1, indices)

 1.2045  0.4001
 0.5596  0.6219
 1.3635 -0.5414
[torch.FloatTensor of size 3x2]

dim=0,[0,2]:取第0和第1行。

dim=1,[0,2]:取第0和第1列。

torch.masked_select

torch.masked_select(input, mask, out=None) → Tensor

根据掩码张量mask中的二元值,取输入张量中的指定项(mask为一个 ByteTensor),将取值返回到一个新的1D张量,张量mask须跟input张量有相同数量的元素数目,但形状或维度不需要相同。

注意: 返回的张量不与原始张量共享内存空间

参数:

  • input (Tensor) – 输入张量
  • mask (ByteTensor) – 掩码张量,包含了二元索引值
  • out (Tensor, optional) – 目标张量

torch.nonzero

torch.nonzero(input, out=None) → LongTensor

返回输入张量中的非零元素的索引。

如果输入inputn维,则输出的索引张量output的形状为 z x n, 这里 z 是输入张量input中所有非零元素的个数。

参数:

  • input (Tensor) – 源张量
  • out (LongTensor, optional) – 包含索引值的结果张量

例子:

>>> torch.nonzero(torch.Tensor([1, 1, 1, 0, 1])) 0 1 2 4[torch.LongTensor of size 4x1]>>> torch.nonzero(torch.Tensor([[0.6, 0.0, 0.0, 0.0],...                             [0.0, 0.4, 0.0, 0.0],...                             [0.0, 0.0, 1.2, 0.0],...                             [0.0, 0.0, 0.0,-0.4]])) 0  0 1  1 2  2 3  3[torch.LongTensor of size 4x2]

torch.split

torch.split(tensor, split_size, dim=0)

将输入张量分割成相等形状的chunks(如果可分)。 如果沿指定维的张量形状大小不能被split_size 整分, 则最后一个分块会小于其它分块。

参数:

  • tensor (Tensor) – 待分割张量
  • split_size (int) – 单个分块的形状大小
  • dim (int) – 沿着此维进行分割

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.zeros(2,1,2,1,2)>>> x.size()(2L, 1L, 2L, 1L, 2L)>>> y = torch.squeeze(x)>>> y.size()(2L, 2L, 2L)>>> y = torch.squeeze(x, 0)>>> y.size()(2L, 1L, 2L, 1L, 2L)>>> y = torch.squeeze(x, 1)>>> y.size()(2L, 2L, 1L, 2L)

torch.stack

torch.stack(sequence, dim=0)

沿着一个新维度对输入张量序列进行连接。 序列中所有的张量都应该为相同形状。

参数:

  • sqequence (Sequence) – 待连接的张量序列
  • dim (int) – 插入的维度。必须介于0与待连接的张量序列数之间。

torch.t

torch.t(input, out=None) → Tensor

输入一个矩阵(2维张量),并转置0, 1维。 可以被视为函数transpose(input, 0, 1)的简写函数。

参数:

  • input (Tensor) – 输入张量
  • out (Tensor, optional) – 结果张量

例子:

>>> x = torch.randn(2, 3)>>> x 0.4834  0.6907  1.3417-0.1300  0.5295  0.2321[torch.FloatTensor of size 2x3]>>> torch.t(x) 0.4834 -0.1300 0.6907  0.5295 1.3417  0.2321[torch.FloatTensor of size 3x2]

torch.transpose

torch.transpose(input, dim0, dim1, out=None) → Tensor

返回输入矩阵input的转置。交换维度dim0和dim1。 输出张量与输入张量共享内存,所以改变其中一个会导致另外一个也被修改。

参数:

  • input (Tensor) – 输入张量
  • dim0 (int) – 转置的第一维
  • dim1 (int) – 转置的第二维

例子:

>>> x = torch.randn(2, 3)>>> x 0.5983 -0.0341  2.4918 1.5981 -0.5265 -0.8735[torch.FloatTensor of size 2x3]>>> torch.transpose(x, 0, 1) 0.5983  1.5981-0.0341 -0.5265 2.4918 -0.8735[torch.FloatTensor of size 3x2]

torch.unbind

torch.unbind(tensor, dim=0)

移除指定维后,返回一个元组,包含了沿着指定维切片后的各个切片。

参数:

  • tensor (Tensor) – 输入张量
  • dim (int) – 删除的维度

torch.unsqueeze

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

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

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

如果dim为负,则将会被转化dim+input.dim()+1
参数:

  • tensor (Tensor) – 输入张量
  • dim (int) – 插入维度的索引
  • out (Tensor, optional) – 结果张量

例子:

>>> x = torch.Tensor([1, 2, 3, 4])>>> torch.unsqueeze(x, 0) 1  2  3  4[torch.FloatTensor of size 1x4]>>> torch.unsqueeze(x, 1) 1 2 3 4[torch.FloatTensor of size 4x1]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没有胡子的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值