31
31
32
32
假设一个mini-batch中有3个句子,每个句子中分别包含3个、1个和2个单词。我们可以用(3+1+2)xD维Tensor 加上一些索引信息来表示这个mini-batch:
33
33
34
- .. code-block :: python
34
+ .. code-block :: text
35
35
36
36
3 1 2
37
37
| | | | | |
42
42
43
43
让我们来看另一个2-level LoD-Tensor的例子:假设存在一个mini-batch中包含3个句子、1个句子和2个句子的文章,每个句子都由不同数量的单词组成,则这个mini-batch的样式可以看作:
44
44
45
- .. code-block :: python
45
+ .. code-block :: text
46
+
46
47
47
48
3 1 2
48
49
3 2 4 1 2 3
51
52
52
53
表示的LoD信息为:
53
54
54
- .. code-block :: python
55
+ .. code-block :: text
55
56
56
57
[[3,1,2]/*level=0*/,[3,2,4,1,2,3]/*level=1*/]
57
58
60
61
61
62
在视觉任务中,时常需要处理视频和图像这些元素是高维的对象,假设现存的一个nimi-batch包含3个视频,分别有3个,1个和2个帧,每个帧都具有相同大小:640x480,则这个mini-batch可以被表示为:
62
63
63
- .. code-block :: python
64
+ .. code-block :: text
64
65
65
66
3 1 2
66
67
口口口 口 口口
@@ -72,14 +73,14 @@ LoD 索引
72
73
73
74
在传统的情况下,比如有N个固定大小的图像的mini-batch,LoD-Tensor表示为:
74
75
75
- .. code-block :: python
76
+ .. code-block :: text
76
77
77
78
1 1 1 1 1
78
79
口口口口 ... 口
79
80
80
81
在这种情况下,我们不会因为索引值都为1而忽略信息,仅仅把LoD-Tensor看作是一个普通的张量:
81
82
82
- .. code-block :: python
83
+ .. code-block :: text
83
84
84
85
口口口口 ... 口
85
86
@@ -94,13 +95,13 @@ LoDTensor的偏移表示
94
95
95
96
在上述例子中,您可以计算基本元素的长度:
96
97
97
- .. code-block :: python
98
+ .. code-block :: text
98
99
99
100
3 2 4 1 2 3
100
101
101
102
将其转换为偏移表示:
102
103
103
- .. code-block :: python
104
+ .. code-block :: text
104
105
105
106
0 3 5 9 10 12 15
106
107
= = = = = =
@@ -110,21 +111,21 @@ LoDTensor的偏移表示
110
111
111
112
类似的,LoD的顶层长度
112
113
113
- .. code-block :: python
114
+ .. code-block :: text
114
115
115
116
3 1 2
116
117
117
118
可以被转化成偏移形式:
118
119
119
- .. code-block :: python
120
+ .. code-block :: text
120
121
121
122
0 3 4 6
122
123
= = =
123
124
3 3+1 4+2
124
125
125
126
因此该LoD-Tensor的偏移表示为:
126
127
127
- .. code-block :: python
128
+ .. code-block :: text
128
129
129
130
0 3 4 6
130
131
3 5 9 10 12 15
@@ -138,7 +139,7 @@ LoD-Tensor
138
139
139
140
以上文提到的一个2-level LoD-Tensor为例:
140
141
141
- .. code-block :: python
142
+ .. code-block :: text
142
143
143
144
3 1 2
144
145
3 2 4 1 2 3
@@ -147,11 +148,12 @@ LoD-Tensor
147
148
- 以偏移量表示此 LoD-Tensor:[ [0,3,4,6] , [0,3,5,9,10,12,15] ],
148
149
- 以原始长度表达此 Lod-Tensor:recursive_sequence_lengths=[ [3-0 , 4-3 , 6-4] , [3-0 , 5-3 , 9-5 , 10-9 , 12-10 , 15-12] ]。
149
150
150
- 以文字序列为例: [3,1,2] 可以表示这个mini-batch中有3篇文章,每篇文章分别有3、2、1个句子,[3,2,4,1,2,3] 表示每个句子中分别含有3、2、4、1、2、3个字。
151
+
152
+ 以文字序列为例: [3,1,2] 可以表示这个mini-batch中有3篇文章,每篇文章分别有3、1、2个句子,[3,2,4,1,2,3] 表示每个句子中分别含有3、2、4、1、2、3个字。
151
153
152
154
recursive_seq_lens 是一个双层嵌套列表,也就是列表的列表,最外层列表的size表示嵌套的层数,也就是lod-level的大小;内部的每个列表,对应表示每个lod-level下,每个元素的大小。
153
155
154
- .. code-block :: python
156
+ .. code-block :: text
155
157
156
158
#查看lod-tensor嵌套层数
157
159
print len(recursive_seq_lengths)
0 commit comments