index_copy_(dim, index, tensor) –>Tensor 按参数Index中的索引数确定的顺序,将参数tensor中的元素复制到原来的tensor中。参数tensor的尺寸必须严格地与原tensor匹配,否则会发生错误。
参数:
--dim (int)-
索引
index
所指向的维度
--index (LongTensor)-
需要从
tensor
中选取的指数
--tensor (Tensor)-
含有被复制元素的
tensor
例子:
>>> x = torch.Tensor(3, 3)
>>> t = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> index = torch.LongTensor([0, 2, 1])
>>> x.index_copy_(0, index, t)
>>> x
1 2 3
7 8 9
4 5 6
[torch.FloatTensor of size 3x3]
index_fill_(dim, index, val) -->Tensor
按参数index中的索引数确定的顺序,将原tensor用参数填充。
参数:
--dim (int)-索引 index 所指向的维度
--index (LongTensor)-索引
--val (Tensor)-填充的值
例子:
>>> x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> index = torch.LongTensor([0, 2])
>>> x.index_fill_(0, index, -1)
>>> x
-1 2 -1
-1 5 -1
-1 8 -1
[torch.FloatTensor of size 3x3]
index_select(dim, index) -->Tensor
rrow(dimension, start, length) –>Te
返回一个本
tensor
经过缩小后的
tensor
。维度dim缩小范围是start到start+length。原tensor与返回tensor共享相同的底层内存。
参数:
--dimension (int)-
需要缩小的维度
--start (int)-
起始维度
- length (int)-
例子:
>>> x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> x.narrow(0, 0, 2)
1 2 3
4 5 6
[torch.FloatTensor of size 2x3]
>>> x.narrow(1, 1, 2)
2 3
5 6
8 9
[torch.FloatTensor of size 3x2]
permute(dims)
将
tensor
的维度换位。
参数:
--
dims__ (int..*)-
换位顺序
例子:
>>> x = torch.randn(2, 3, 5)
>>> x.size()
torch.Size([2, 3, 5])
>>> x.permute(2, 0, 1).size()
torch.Size([5, 2, 3])
pin_memory()
如果原来没有在固定内存中,则将
tensor
复制到固定内存中。
repeat(sizes)
沿着指定的维度重复
tensor
。不同于
expand()
,本函数复制的是
tensor中的数据。
参数:
---sizes (torch.Size ot int...)-
沿着每一维重复的次数
例子:
>>> x = torch.Tensor([1, 2, 3])
>>> x.repeat(4, 2)
1 2 3 1 2 3
1 2 3 1 2 3
1 2 3 1 2 3
1 2 3 1 2 3
[torch.FloatTensor of size 4x6]
>>> x.repeat(4, 2, 1).size()
torch.Size([4, 2, 3])
resize_(*sizes)
将
tensor
的大小调整为指定的大小。如果元素个数比当前的内存大小大,就将底层存储大小调整为与新元素数目一致的大小。如果元素个数比当前内存小,则底层存储不会被改变。原来tensor中被保存下来的元素将保持不变,但新内存将不会被初始化。
参数:
--sizes (torch.Size or int...)-
需要调整的大小
例子:
>>> x = torch.Tensor([[1, 2], [3, 4], [5, 6]])
>>> x.resize_(2, 2)
>>> x
1 2
3 4
[torch.FloatTensor of size 2x2]
scatter_(input, dim, index, src) –>Tensor
将
src
中的所有值按照
index
确定的索引写入本tensor中。其中索引是根据给定dimension,dim按照gather()描述的规则来确定。
注意,index的值必须是在_0_到(self.size(dim)-1)_之间。
参数:
--input (Tensor)-
源
tensor
--dim (int)-
索引的轴向
--index (LongTensor)-
散射元素的索引指数
--src (Tensor or float)-
散射的源元素
例子:
>>> x = torch.rand(2, 5)
>>> x
0.4319 0.6500 0.4080 0.8760 0.2355
0.2609 0.4711 0.8486 0.8573 0.1029
[torch.FloatTensor of size 2x5]
>>> torch.zeros(3, 5).scatter_(0, torch.LongTensor([[0, 1,
2, 0, 0], [2, 0, 0, 1, 2]]), x) , →
0.4319 0.4711 0.8486 0.8760 0.2355
0.0000 0.6500 0.0000 0.8573 0.0000
0.2609 0.0000 0.4080 0.0000 0.1029
[torch.FloatTensor of size 3x5]
>>> z = torch.zeros(2, 4).scatter_(1,
torch.LongTensor([[2], [3]]), 1.23) , →
>>> z
0.0000 0.0000 1.2300 0.0000
0.0000 0.0000 0.0000 1.2300
[torch.FloatTensor of size 2x4]