您的位置:首页 > 编程语言 > Lua

torch中narrow的用法

2015-10-19 13:13 267 查看
关于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 lua