Pytorch框架的学习与使用_day02
torch.cat(inputs, dimension = 0) -> Tensor
给定维度上对输入张量序列seq进行连接操作
torch.cat可以看作torch.split和torch.chunk的反操作
参数:
inputs(sequence of Tensors) - 可以是任意相同Tensor类型的Python序列
dimension(int, optional) - 沿着此维连接张量序列
例子:
>>> x = torch.randn(2, 3) >>> x tensor([[ 0.8240, -0.6438, 0.1779], [ 1.8698, -0.3803, 0.0556]]) >>> torch.cat((x, x, x), 0) tensor([[ 0.8240, -0.6438, 0.1779], [ 1.8698, -0.3803, 0.0556], [ 0.8240, -0.6438, 0.1779], [ 1.8698, -0.3803, 0.0556], [ 0.8240, -0.6438, 0.1779], [ 1.8698, -0.3803, 0.0556]]) # size 6*3 0维向量进行连接 >>> torch.cat((x, x, x), 1) tensor([[ 0.8240, -0.6438, 0.1779, 0.8240, -0.6438, 0.1779, 0.8240, -0.6438, 0.1779], [ 1.8698, -0.3803, 0.0556, 1.8698, -0.3803, 0.0556, 1.8698, -0.3803, 0.0556]]) # size 2*9 1维向量进行连接
torch.chunk
torch.chunk(tensor, chunks, dim = 0)
在给定维度上将张量进行分块
参数:
tensor - 待分块的输入张量
chunks(int) - 分块的个数
dim(int) - 沿着此维度进行分块
例子:
>>> x tensor([[ 0.8240, -0.6438, 0.1779], [ 1.8698, -0.3803, 0.0556]])
>>> torch.chunk(x, 2, 0)
(tensor([[ 0.8240, -0.6438, 0.1779]]), tensor([[ 1.8698, -0.3803, 0.0556]]))
>>> torch.chunk(x, 2, 1) (tensor([[ 0.8240, -0.6438], [ 1.8698, -0.3803]]), tensor([[0.1779], [0.0556]]))
torch.gather
torch.gather(input, dim, index, out = None) ->Tensor
沿给定轴dim 将输入索引张量index指定位置的值进行聚合
对于一个三维张量,输出可以定义为
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 = 2
参数:
input(Tensor) - 源张量
dim(int) - 索引的轴
index(Long Tensor) - 聚合元素的下标
out(Tensor, optional) - 目标张量
例子:
>>> t = torch.Tensor([[1, 2], [3, 4]]) >>> torch.gather(t, 1, torch.LongTensor([[0, 0], [1,0]])) tensor([[1., 1.], [4., 3.]])
可以看出,gather的作用是这样的,index实际上是索引,具体是行还是列的索引要看前面dim 的指定,比如对于我们的栗子,【1,2;3,4】,指定dim=1,也就是横向,那么索引就是列号。index的大小就是输出的大小,所以比如index是【0,0;0,0】,那么看index第一行,0列指的是1,同理,第二行为1,0。这样就输入为【1,1;4,3】,参考这样的解释看上面的输出结果,即可理解gather的含义
torch.index_select
torch.index_select(input, dim, index, out = None) -->Tensor
沿着制定维度对输入进行切片,取index中指定的相应项,回到一个新的张量,返回张量与原始张量tensor有相同的维度
返回的张量不与原始张亮共享内存空间
参数:
input(Tensor) - 源张量
dim(int) - 索引的轴
index(Long Tensor) - 包含索引下标的一维张量
out(Tensor, optional) - 目标张量
例子:
>>> x = torch.randn(3, 4) >>> x tensor([[-0.1374, -0.6321, 0.8015, -2.0121], [-0.8106, 1.0078, -0.7167, 0.5915], [-0.8627, 0.5883, 0.9542, 0.1841]]) >>> indices = torch.LongTensor([0, 2]) >>> torch.index_select(x, 0, indices) # dim 0 按列索引 tensor([[-0.1374, -0.6321, 0.8015, -2.0121], [-0.8627, 0.5883, 0.9542, 0.1841]]) >>> torch.index_select(x, 1, indices) # dim 1 按行索引 tensor([[-0.1374, 0.8015], [-0.8106, -0.7167], [-0.8627, 0.9542]])
torch.nonzero
torch.nonzero(input, out = None) --> Tensor
返回一个包含输入input中非零元素索引的张量。输入张量中的每行包含输入中非零元素的索引
如果输入input有n维,则输出的索引张量output的形状为z×n 这里z是输入张量input中所有非零元素的个数
参数:
input(Tensor) - 源张量
out(LongTensor, optional) - 包含索引值的结果张量
例子:
>>> torch.nonzero(torch.Tensor([1, 1, 1, 0, 1])) tensor([[0], [1], [2], [4]]) # 4×1
torch.split
torch.split(tensor, split_size, dim = 0)
将输入的张量分割成相等形状的chunks(如果能分), 如果沿指定维的张量形状大小不能被split_size整分,则最后一个小分块会小于其他分块
参数:
tensor(Tensor) - 待分割张量
split_size(int) - 单个分块的形状大小
dim(int) - 沿着此维度进行分割
torch.squeeze
input(Tensor) - 源张量
dim(int, optional) - 如果给定,则input只会在给定维度挤压
out(Tensor, optional) - 目标张量
例子:
>>> x = torch.zeros(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])
torch.stack
torch.stack(squence, dim = 0)
沿着一个新维度对输入张量序列进行连接。序列中所有的张量都应该为相同形状
参数:
sqequence(Sqequence) - 待连接的张量序列
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 tensor([[ 0.5121, -0.5057, -0.5253], [ 0.0310, 0.8405, -0.7914]]) >>> torch.t(x) tensor([[ 0.5121, 0.0310], [-0.5057, 0.8405], [-0.5253, -0.7914]])
torch.transpose
torch.transpose(input, dim0, dim1, out = None) --> Tensor
相当于torch.t
torch.unbind
torch.unbind(tensor, dim = 0)
移除指定维度后,返回一个元组, 包含了沿着指定维切片以后的各个切片
参数:
tensor(Tensor) - 输入张量
dim(int) - 删除的维度
>>> x = torch.randn(2, 3) >>> x tensor([[-0.1249, -0.3148, 1.8918], [ 1.9341, -2.3968, -1.0895]]) >>> torch.unbind(x, dim = 1) (tensor([-0.1249, 1.9341]), tensor([-0.3148, -2.3968]), tensor([ 1.8918, -1.0895]))
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) tensor([[1, 2, 3, 4]]) >>> torch.unsqueeze(x, 1) tensor([[1], [2], [3], [4]])