Pytorch官方指南(五) 翻译版本
torch.full
参数:
- size,一个列表、元组,或者是定义输出Tensor大小的整数组合。
- fill_value,输出的Tensor的填充值。
- out,(可选)输出的Tensor。
- **dtype **,(可选)返回的张量的所需数据类型。默认值:全局默认值。
- layout,(可选)返回张量的所需布局。默认值:torch.stridded。
- device,(可选)返回张量的设备类型。默认值:当前设备。
- requires_grad,(可选)autograd是否应该在返回的张量上记录操作。默认值:False。
例子:
>>> torch.full((2, 3), 3.141592)
tensor([[ 3.1416, 3.1416, 3.1416],
[ 3.1416, 3.1416, 3.1416]])
torch.cat
作用:
- 将两个Tensor连接起来,可以看成torch.split( )和torch.chunk( )的逆操作。
参数:
- tensors,同一类型的任何python张量序列。非空张量必须具有相同的形状,但在cat维度中除外。
- dim,(可选)张量连接的维数,0代表列数不变行连接,1代表行数不变列连接。
- out,(可选)输出的Tensor。
例子:
>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497]])
>>> torch.cat((x, x, x), 0)
tensor([[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497],
[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497],
[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497]])
>>> torch.cat((x, x, x), 1)
tensor([[ 0.6580, -1.0969, -0.4614, 0.6580, -1.0969, -0.4614, 0.6580,
-1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497, -0.1034, -0.5790, 0.1497, -0.1034,
-0.5790, 0.1497]])
torch.chunk
作用:
- 将张量拆分为特定数量的块。
- 如果给定维度维度上的张量大小不能被块整除,则最后一个块将更小,意思就是会先满足可以满足的。
参数:
- input,将要被分割的Tensor。
- chunks,要切割成几块。
- dim,切分Tensor的维度。
例子:
>>> a = torch.ones(2, 4) # 若(2, 3), 则b的维度为(2, 2), c的维度为(2,1)
>>> b, c = torch.chunk(a, 2, 1)
>>> b, c
a: tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.]])
b: tensor([[1., 1.],
[1., 1.]])
c: tensor([[1., 1.],
[1., 1.]])
torch.gather
作用:
- 将原Tensor按行或者按列取出后重组一个新的Tensor。
参数:
- input,待处理Tensor。
- dim,索引轴,0代表按列索引取值,1代表按行索引取值。
- index,要处理的元素的索引。
- out,(可选)目的Tensor。
- sparse_grad,(可选)如果为真,梯度w.r.t.输入将是稀疏张量。
例子:
>>> t = torch.tensor([[1,2],[3,4]])
>>> torch.gather(t, 1, torch.tensor([[0,0],[1,0]]))
tensor([[ 1, 1],
[ 4, 3]])
#
>>> t = torch.tensor([[1, 2, -1], [3, 4, 5], [5, 6, 7]])
>>> torch.gather(t, 1, torch.tensor([ [1, 0, 2], [1, 0, 2], [1, 0, 2]]))
tensor([[ 2, 1, -1],
[ 4, 3, 5],
[ 6, 5, 7]])
torch.index_select
作用:
- 从Tensor中按索引选出某几行或某几列,返回的是一个新的Tensor。
参数:
- input,输入待处理Tensor。
- dim,0-按行取;1-按列取。
- index,包含索引到索引的一维张量(待处理Tensor对应索引值)。
- out,(可选)输出Tensor。
例子:
>>> x = torch.randn(3, 4)
>>> x
tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
[-0.4664, 0.2647, -0.1228, -1.1068],
[-1.1734, -0.6571, 0.7230, -0.6004]])
>>> indices = torch.tensor([0, 2])
>>> torch.index_select(x, 0, indices)
tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
[-1.1734, -0.6571, 0.7230, -0.6004]])
>>> torch.index_select(x, 1, indices)
tensor([[ 0.1427, -0.5414],
[-0.4664, -0.1228],
[-1.1734, 0.7230]])
torch.masked_select
作用:
- 按照值筛选Tensor。
参数:
- input – 输入的Tensor。
- mask– 包含要索引的二进制掩码的Tensor。
- out – 输出Tensor。
例子:
>>> x = torch.randn(3, 4)
>>> x
tensor([[ 0.3552, -2.3825, -0.8297, 0.3477],
[-1.2035, 1.2252, 0.5002, 0.6248],
[ 0.1307, -2.0608, 0.1244, 2.0139]])
# .ge()函数,返回与x相同大小的Tensor,x中的值大于0.5的位置置1,其余位置置0。
>>> mask = x.ge(0.5)
>>> mask
tensor([[False, False, False, False],
[False, True, True, True],
[False, False, False, True]])
>>> torch.masked_select(x, mask)
tensor([ 1.2252, 0.5002, 0.6248, 2.0139])
武汉加油,中国加油!!!!!