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
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
相关文章推荐
- 详解Lua中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍
- Lua中ipair和pair的区别
- Lua中的函数精讲笔记
- 浅谈Lua的面向对象特性
- 详解Lua中的变量相关知识点
- Lua脚本语言入门笔记
- Lua脚本调用外部脚本
- 详解Lua中的if语句的使用方法
- Lua中调用函数使用点号和冒号的区别
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中强大的元方法__index详解
- Lua中调用C++函数示例
- Lua面向对象之类和继承浅析
- Lua性能优化技巧(一):前言
- Lua中获取table长度问题探讨