关于torch中narrow的用法,本人总结了以下的一种用法,欢迎补充!
data = tensor:narrow(dim, index, size)
–表示取出tensor中第dim维上索引从index开始到index+size-1的所有元素存放在data中
举例:
.—————————————————————————————
In [ ] x = torch.rand(5, 6) –生成5*6的随机矩阵
In [ ] print(x)
out[ ]
0.2372 0.1170 0.8364 0.5361 0.9864 0.8697
0.9495 0.4100 0.2586 0.3210 0.7368 0.3527
0.6783 0.0506 0.1117 0.9947 0.2499 0.0965
0.2466 0.4949 0.4086 0.2545 0.9178 0.7450
0.5235 0.0604 0.2164 0.0239 0.3963 0.5659
[torch.DoubleTensor of size 5x6]
.—————————————————————————————
In [ ] y = x:narrow(1, 2, 3) –取出x中从第1维(列)中第2行到第4(=2+3-1)行所有的元素存放在y中
In [ ] print(y) –输出结果
Out[ ]
0.9495 0.4100 0.2586 0.3210 0.7368 0.3527
0.6783 0.0506 0.1117 0.9947 0.2499 0.0965
0.2466 0.4949 0.4086 0.2545 0.9178 0.7450
[torch.DoubleTensor of size 3x6]
.—————————————————————————————
In [ ] y = x:narrow(2,3,3) –取出x中从第2维(行)中第3列到第5列(=3+3-1)所有的元素存放在y中
In [ ] print(y)
Out[ ] –输出结果
0.8364 0.5361 0.9864
0.2586 0.3210 0.7368
0.1117 0.9947 0.2499
0.4086 0.2545 0.9178
0.2164 0.0239 0.3963
[torch.DoubleTensor of size 5x3]
.—————————————————————————————
参考资料:
http://torch5.sourceforge.net/manual/torch/index-6-7-1.html
http://torch7.readthedocs.org/en/latest/tensor/index.html
torch中narrow的用法
最新推荐文章于 2023-12-07 18:04:59 发布