【python代码技巧2】数组索引扩增技巧
来源:互联网 发布:盛势网络剧剧照 编辑:程序博客网 时间:2024/06/06 08:42
import numpy as npimport torcha = np.array([x for x in range(12)]).reshape(3, 4)index = np.array([[0,1] for x in range(5)]).flatten()print "a:\n", a, a.shapeprint "index:\n", index, index.shape
a:[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] (3, 4)index:[0 1 0 1 0 1 0 1 0 1] (10,)
print a[index], a[index].shape
[[0 1 2 3] [4 5 6 7] [0 1 2 3] [4 5 6 7] [0 1 2 3] [4 5 6 7] [0 1 2 3] [4 5 6 7] [0 1 2 3] [4 5 6 7]] (10, 4)
pytorch同样有这种特性
a = torch.Tensor([x for x in range(12)]).view(3, 4)index = torch.LongTensor([[0,1] for x in range(5)]).view(-1)print "a:\n", aprint "index:\n", index
a: 0 1 2 3 4 5 6 7 8 9 10 11[torch.FloatTensor of size 3x4]index: 0 1 0 1 0 1 0 1 0 1[torch.LongTensor of size 10]
print a[index], a[index].size()
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7[torch.FloatTensor of size 10x4] torch.Size([10, 4])
阅读全文
0 0
- 【python代码技巧2】数组索引扩增技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- Python 代码调试技巧
- 【python代码技巧1】文件查找&数组变维
- 海明码简单计算方法
- NoClassDefFoundError
- scribe、chukwa、kafka、flume日志系统对比
- 文件下载
- Centos6.5 远程访问mysql
- 【python代码技巧2】数组索引扩增技巧
- webpack+react-router按需加载入门
- 约束布局ConstraintLayout的使用(二)
- crt连接centos时的一个小问题(桥接模式)
- pipenet.v9.0(Spray V3.40,Standard V3.4,Transient V5.3)
- 十四 iOS之 画板
- 五、python日期相关的操作
- java基础之序列化详解
- 文章Super-Convergence记录