提交 d35afb26 编写于 作者: T Travis CI

Deploy to GitHub Pages: 34193844

上级 2236d24c
IfOp should have only one branch. An IfOp operator takes a `cond` variable whose value must be a vector of N boolean elements. Its return value has N instances. If cond[i] == True, input instance input[i] will go through true_block() and generate output[i]; otherwise it will produce output from false_bloack().
# The `IfElse` Operator
```python
import paddle as pd
PaddlePaddle's `IfElse` operator differs from TensorFlow's:
x = var()
y = var()
cond = var()
default_value = var()
b = pd.create_ifelseop(inputs=[x], output_num=1)
with b.true_block():
x = b.inputs(0)
z = operator.add(x, y)
b.set_output(0, operator.softmax(z))
with b.false_block():
x = b.inputs(0)
z = layer.fc(x)
b.set_output(0, operator.softmax(z))
out = b(cond)
```
- the TensorFlow version takes a scalar boolean value as the condition so that the whole mini-batch goes to either the true or the false branch, whereas
- the PaddlePaddle version takes a vector of boolean value as the condition, and instances corresponding to true values go to the true branch, those corresponding to false values go to the false branch.
## Example
The following PaddlePaddle program shows the usage of the IfElse operator:
If only true_block is set in an IfElseOp, a special case is that we can have a default value for false as:
```python
import paddle as pd
x = var()
y = var()
cond = var()
default_value = var()
b = pd.create_ifelseop(inputs=[x], output_num=1, default_value)
with b.true_block():
x = b.inputs(0)
z = operator.add(x, y)
b.set_output(0, operator.softmax(z))
x = minibatch([10, 20, 30]) # shape=[None, 1]
y = var(1) # shape=[1], value=1
z = minibatch([10, 20, 30]) # shape=[None, 1]
cond = larger_than(x, 15) # [false, true, true]
ie = pd.ifelse()
with ie.true_block():
d = pd.layer.add(x, y)
ie.output(d, pd.layer.softmax(d))
with ie.false_block():
d = pd.layer.fc(z)
ie.output(d, d+1)
o1, o2 = ie(cond)
```
out = b(cond)
A challenge to implement the `IfElse` operator is to infer those variables to be split, or, say, to identify the variable of the mini-batch or those derived from the mini-batch.
An equivalent C++ program is as follows:
```c++
namespace pd = paddle;
int x = 10;
int y = 1;
int z = 10;
bool cond = false;
int o1, o2;
if (cond) {
int d = x + y;
o1 = z;
o2 = pd::layer::softmax(z);
} else {
int d = pd::layer::fc(z);
o1 = d;
o2 = d+1;
}
```
where default_value is a list of vars for `cond` == False.
# Design for TensorArray
This design doc presents the necessity of a new C++ class `TensorArray`.
In addition to the very simple C++ implementation
```c++
class TensorArray {
public:
explicit TensorArray(const LoDTensor&);
explicit TensorArray(size_t size);
private:
vector<LoDTensor> values_;
};
```
We also need to expose it to PaddlePaddle's Python API,
because users would want to use it with our very flexible operators `WhileLoop`.
An example for a RNN based on dynamic operators is
```python
input = pd.data(...)
num_steps = Var(12)
TensorArray states(size=num_steps)
TensorArray step_inputs(unstack_from=input)
TensorArray step_outputs(size=num_steps)
W = Tensor(...)
U = Tensor(...)
default_state = some_op()
step = Var(1)
wloop = paddle.create_whileloop(loop_vars=[step])
with wloop.frame():
wloop.break_if(pd.equal(step, num_steps)
pre_state = states.read(step-1, default_state)
step_input = step_inputs.read(step)
state = pd.sigmoid(pd.matmul(U, pre_state) + pd.matmul(W, step_input))
states.write(step, state)
step_outputs.write(step, state) # output state
step.update(state+1)
output = step_outputs.stack()
```
## Background
Steps are one of the core concepts of RNN. In each time step of RNN, there should be several input segments, states, and output segments; all these components act like arrays, for example, call `states[step_id]` will get the state in `step_id`th time step.
An RNN can be implemented with the following pseudocode
```c++
Array states;
Array input_segments;
Array output_segments;
Parameter W, U;
step = 1
seq_len = 12
while_loop {
if (step == seq_len) break;
states[step] = sigmoid(W * states[step-1] + U * input_segments[step]);
output_segments[step] = states[step] // take state as output
step++;
}
```
According to the [RNN roadmap](https://github.com/PaddlePaddle/Paddle/issues/4561), there are several different RNNs that PaddlePaddle will eventually support.
Currently, the basic RNN implementation supported by PaddlePaddle is the `recurrent_op` which takes tensors as input and splits them into `input_segments`.
Since a tensor cannot store variable-length sequences directly, PaddlePaddle implements the tensor with level of details (`LoDTensor` for short).
Segmenting the `LoDTensor` is much more complicated than splitting a tensor, that makes it necessary to refactor the `recurrent_op` with `LoDTensor` segmenting support.
As the next step in RNN support, `dynamic_recurrent_op` should be introduced to handle inputs with variable-length sequences.
The implementation is similar to `recurrent_op`.
The key difference is the way **the original input `LoDTensors` and outupts are split to get the `input_segments` and the `output_segments`.**
Though it can't be built over `recurrent_op` or `dynamic_recurrent_op` directly,
the logic behind splitting a tensor or a LoD tensor into `input_segments` remains the same.
## Why `TensorArray`
The logic behind splitting the inputs to segments, states and outputs is similar and can be shared in a seperate module.
The array of `states`, `input_segments` and `output_segments` would be exposed to users when writing a dynamic RNN model similar to the above pseudo codes.
So there should be an array-like container, which can store the segments of a tensor or LoD tensor.
**This container can store an array of tensors and provides several methods to split a tensor or a LoD tensor** .
This is where the notion of `TensorArray` comes from.
## Introduce TensorArray to uniform all the three RNNs
TensorArray as a new concept is borrowed from TensorFlow,
it is meant to be used with dynamic iteration primitives such as `while_loop` and `map_fn`.
This concept can be used to support our new design of dynamic operations, and help to refactor some existing variant-sentence-related layers,
such as `RecurrentGradientMachine`.
such as `recurrent_op`, `RecurrentGradientMachine`.
In [our design for dynamic RNN](https://github.com/PaddlePaddle/Paddle/pull/4401),
`TensorArray` is used to segment inputs and store states in all time steps.
By providing some methods similar to a C++ array,
the definition of some state-based dynamic models such as RNN could be more natural and highly flexible.
## Dynamic-Related Methods
Some basic methods should be proposed as follows:
### stack()
Pack the values in a `TensorArray` into a tensor with rank one higher than each tensor in `values`.
### unstack(axis=0)
Unpacks the given dimension of a rank-`R` tensor into rank-`(R-1)` tensors.
### concat()
Return the values in the `TensorArray` as a concatenated Tensor.
### write(index, value, data_shared=true)
Write value into index of the TensorArray.
### read(index)
Read the value at location `index` in the `TensorArray`.
### size()
Return the number of values.
the definition of some state-based dynamic models such as RNN can be more natural and highly flexible.
## Dynamic-operations on TensorArray
`TensorArray` will be used directly when defining dynamic models, so some operators listed below should be implemented
```python
# several helper operators for TensorArray
def tensor_array_stack(ta, tensor):
'''
get a tensor array `ta`, return a packed `tensor`.
'''
pass
def tensor_array_unstack(tensor, ta):
'''
get a `tensor`, unstack it and get a tensor array `ta`.
'''
pass
def tensor_array_write(ta, index, tensor, data_shared):
'''
get a `tensor` and a scalar tensor `index`, write `tensor` into index-th
value of the tensor array `ta`.
`data_shared` is an attribute that specifies whether to copy or reference the tensors.
'''
pass
def tensor_array_read(ta, index, tensor):
'''
get a tensor array `ta`, a scalar tensor `index`, read the index-th value of
`ta` and return as the `tensor`.
'''
pass
def tensor_array_size(ta, tensor):
'''
get a tensor array `ta`, return the size of `ta` and return as the scalar `tensor`.
'''
pass
```
It is trivial for users to use so many low-level operators, so some helper methods should be proposed in python wrapper to make `TensorArray` easier to use,
for example
```python
class TensorArray:
def __init__(self, name):
self.name = name
self.desc = TensorArrayDesc()
def stack(self, name=None):
'''
Pack the values in a `TensorArray` into a tensor with rank one higher
than each tensor in `values`.
`stack` can be used to split tensor into time steps for RNN or whileloop.
@name: str
the name of the variable to output.
'''
tensor = NewVar(name)
tensor_array_stack(self.name, tensor)
return tensor
def unstack(self, input):
'''
Unpacks the given dimension of a rank-`R` tensor into rank-`(R-1)` tensors.
`unstack` can be used to concatenate all the time steps for RNN or whileloop.
@input: str
the name of input tensor
'''
tensor_array_unstack(tensor, self.name)
def write(self, index, value, data_shared=True):
'''
Write value into index of the TensorArray.
If `data_shared` is set to True, than the index-th value in TensorArray will
be shared with the tensor passed in.
@index: str
name of a scalar tensor
@value: str
name of a tensor
@data_shared: bool
'''
tensor_array_write(self.name, index, value, data_shared)
def read(self, index, output):
'''
Read the value at location `index` in the `TensorArray`.
@index: str
name of a scalar tensor
@output:
name of a output variable
'''
tensor_array_read(self.name, index, output)
def size(self, output):
'''
Return the number of values.
@output: str
name of a scalar tensor
'''
tensor_array_size(self.name, output)
```
## LoDTensor-related Supports
The `RecurrentGradientMachine` in Paddle serves as a flexible RNN layer; it takes variant length sequences as input,
because each step of RNN could only take a tensor-represented batch of data as input,
The `RecurrentGradientMachine` in Paddle serves as a flexible RNN layer; it takes varience-length sequences as input, and output sequences too.
Since each step of RNN can only take a tensor-represented batch of data as input,
some preprocess should be taken on the inputs such as sorting the sentences by their length in descending order and cut each word and pack to new batches.
Such cut-like operations can be embedded into `TensorArray` as general methods called `unpack` and `pack`.
Such cut-like operations can be embedded into `TensorArray` as general methods called `unpack` and `pack`,
these two operations are similar to `stack` and `unstack` except that they operate on variable-length sequences formated as a LoD tensor rather than a tensor.
Some definitions are like
```python
def unpack(level):
'''
Split LodTensor in some `level` and generate batches, if set `sort_by_length`,
will sort by length.
With these two methods, a variant-sentence-RNN can be implemented like
Returns:
- a new `TensorArray`, whose values are LodTensors and represents batches
of data.
- an int32 Tensor, which stores the map from the new batch's indices to
original LoDTensor
'''
pass
def pack(level, indices_map):
'''
Recover the original LoD-arranged LoDTensor with the values in a `TensorArray`
and `level` and `indices_map`.
'''
pass
```
With these two methods, a varience-length sentence supported RNN can be implemented like
```c++
// input is the varient-length data
......@@ -58,16 +269,3 @@ LoDTensor rnn_output = ta.pack(ta, indice_map);
```
the code above shows that by embedding the LoDTensor-related preprocess operations into `TensorArray`,
the implementation of a RNN that supports varient-length sentences is far more concise than `RecurrentGradientMachine` because the latter mixes all the codes together, hard to read and extend.
some details are as follows.
### unpack(level, sort_by_length)
Split LodTensor in some `level` and generate batches, if set `sort_by_length`, will sort by length.
Returns:
- a new `TensorArray`, whose values are LodTensors and represents batches of data.
- an int32 Tensor, which stores the map from the new batch's indices to original LoDTensor
### pack(level, indices_map)
Recover the original LoD-arranged LoDTensor with the values in a `TensorArray` and `level` and `indices_map`.
......@@ -8,7 +8,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>&lt;no title&gt; &mdash; PaddlePaddle documentation</title>
<title>The IfElse Operator &mdash; PaddlePaddle documentation</title>
......@@ -168,7 +168,7 @@
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li>&lt;no title&gt;</li>
<li>The <code class="docutils literal"><span class="pre">IfElse</span></code> Operator</li>
</ul>
</div>
......@@ -177,45 +177,55 @@
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<p>IfOp should have only one branch. An IfOp operator takes a <code class="docutils literal"><span class="pre">cond</span></code> variable whose value must be a vector of N boolean elements. Its return value has N instances. If cond[i] == True, input instance input[i] will go through true_block() and generate output[i]; otherwise it will produce output from false_bloack().</p>
<div class="section" id="the-ifelse-operator">
<span id="the-ifelse-operator"></span><h1>The <code class="docutils literal"><span class="pre">IfElse</span></code> Operator<a class="headerlink" href="#the-ifelse-operator" title="Permalink to this headline"></a></h1>
<p>PaddlePaddle&#8217;s <code class="docutils literal"><span class="pre">IfElse</span></code> operator differs from TensorFlow&#8217;s:</p>
<ul class="simple">
<li>the TensorFlow version takes a scalar boolean value as the condition so that the whole mini-batch goes to either the true or the false branch, whereas</li>
<li>the PaddlePaddle version takes a vector of boolean value as the condition, and instances corresponding to true values go to the true branch, those corresponding to false values go to the false branch.</li>
</ul>
<div class="section" id="example">
<span id="example"></span><h2>Example<a class="headerlink" href="#example" title="Permalink to this headline"></a></h2>
<p>The following PaddlePaddle program shows the usage of the IfElse operator:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">paddle</span> <span class="kn">as</span> <span class="nn">pd</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">cond</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">default_value</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">create_ifelseop</span><span class="p">(</span><span class="n">inputs</span><span class="o">=</span><span class="p">[</span><span class="n">x</span><span class="p">],</span> <span class="n">output_num</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">with</span> <span class="n">b</span><span class="o">.</span><span class="n">true_block</span><span class="p">():</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">inputs</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">b</span><span class="o">.</span><span class="n">set_output</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">operator</span><span class="o">.</span><span class="n">softmax</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>
<span class="k">with</span> <span class="n">b</span><span class="o">.</span><span class="n">false_block</span><span class="p">():</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">inputs</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">fc</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">b</span><span class="o">.</span><span class="n">set_output</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">operator</span><span class="o">.</span><span class="n">softmax</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">b</span><span class="p">(</span><span class="n">cond</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">minibatch</span><span class="p">([</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">])</span> <span class="c1"># shape=[None, 1]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">var</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># shape=[1], value=1</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">minibatch</span><span class="p">([</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">])</span> <span class="c1"># shape=[None, 1]</span>
<span class="n">cond</span> <span class="o">=</span> <span class="n">larger_than</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span> <span class="c1"># [false, true, true]</span>
<span class="n">ie</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">ifelse</span><span class="p">()</span>
<span class="k">with</span> <span class="n">ie</span><span class="o">.</span><span class="n">true_block</span><span class="p">():</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">ie</span><span class="o">.</span><span class="n">output</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">softmax</span><span class="p">(</span><span class="n">d</span><span class="p">))</span>
<span class="k">with</span> <span class="n">ie</span><span class="o">.</span><span class="n">false_block</span><span class="p">():</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">fc</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="n">ie</span><span class="o">.</span><span class="n">output</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">d</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
<span class="n">o1</span><span class="p">,</span> <span class="n">o2</span> <span class="o">=</span> <span class="n">ie</span><span class="p">(</span><span class="n">cond</span><span class="p">)</span>
</pre></div>
</div>
<p>If only true_block is set in an IfElseOp, a special case is that we can have a default value for false as:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">paddle</span> <span class="kn">as</span> <span class="nn">pd</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">cond</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">default_value</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">create_ifelseop</span><span class="p">(</span><span class="n">inputs</span><span class="o">=</span><span class="p">[</span><span class="n">x</span><span class="p">],</span> <span class="n">output_num</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">default_value</span><span class="p">)</span>
<span class="k">with</span> <span class="n">b</span><span class="o">.</span><span class="n">true_block</span><span class="p">():</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">inputs</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">b</span><span class="o">.</span><span class="n">set_output</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">operator</span><span class="o">.</span><span class="n">softmax</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">b</span><span class="p">(</span><span class="n">cond</span><span class="p">)</span>
<p>A challenge to implement the <code class="docutils literal"><span class="pre">IfElse</span></code> operator is to infer those variables to be split, or, say, to identify the variable of the mini-batch or those derived from the mini-batch.</p>
<p>An equivalent C++ program is as follows:</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="k">namespace</span> <span class="n">pd</span> <span class="o">=</span> <span class="n">paddle</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
<span class="kt">bool</span> <span class="n">cond</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">o1</span><span class="p">,</span> <span class="n">o2</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">cond</span><span class="p">)</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">d</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">;</span>
<span class="n">o1</span> <span class="o">=</span> <span class="n">z</span><span class="p">;</span>
<span class="n">o2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">::</span><span class="n">layer</span><span class="o">::</span><span class="n">softmax</span><span class="p">(</span><span class="n">z</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">d</span> <span class="o">=</span> <span class="n">pd</span><span class="o">::</span><span class="n">layer</span><span class="o">::</span><span class="n">fc</span><span class="p">(</span><span class="n">z</span><span class="p">);</span>
<span class="n">o1</span> <span class="o">=</span> <span class="n">d</span><span class="p">;</span>
<span class="n">o2</span> <span class="o">=</span> <span class="n">d</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>where default_value is a list of vars for <code class="docutils literal"><span class="pre">cond</span></code> == False.</p>
</div>
</div>
</div>
......
......@@ -179,49 +179,230 @@
<div class="section" id="design-for-tensorarray">
<span id="design-for-tensorarray"></span><h1>Design for TensorArray<a class="headerlink" href="#design-for-tensorarray" title="Permalink to this headline"></a></h1>
<p>This design doc presents the necessity of a new C++ class <code class="docutils literal"><span class="pre">TensorArray</span></code>.
In addition to the very simple C++ implementation</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">TensorArray</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
<span class="k">explicit</span> <span class="n">TensorArray</span><span class="p">(</span><span class="k">const</span> <span class="n">LoDTensor</span><span class="o">&amp;</span><span class="p">);</span>
<span class="k">explicit</span> <span class="nf">TensorArray</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">size</span><span class="p">);</span>
<span class="k">private</span><span class="o">:</span>
<span class="n">vector</span><span class="o">&lt;</span><span class="n">LoDTensor</span><span class="o">&gt;</span> <span class="n">values_</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
</div>
<p>We also need to expose it to PaddlePaddle&#8217;s Python API,
because users would want to use it with our very flexible operators <code class="docutils literal"><span class="pre">WhileLoop</span></code>.
An example for a RNN based on dynamic operators is</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="nb">input</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">data</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="n">num_steps</span> <span class="o">=</span> <span class="n">Var</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span>
<span class="n">TensorArray</span> <span class="n">states</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="n">num_steps</span><span class="p">)</span>
<span class="n">TensorArray</span> <span class="n">step_inputs</span><span class="p">(</span><span class="n">unstack_from</span><span class="o">=</span><span class="nb">input</span><span class="p">)</span>
<span class="n">TensorArray</span> <span class="n">step_outputs</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="n">num_steps</span><span class="p">)</span>
<span class="n">W</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="n">U</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="n">default_state</span> <span class="o">=</span> <span class="n">some_op</span><span class="p">()</span>
<span class="n">step</span> <span class="o">=</span> <span class="n">Var</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">wloop</span> <span class="o">=</span> <span class="n">paddle</span><span class="o">.</span><span class="n">create_whileloop</span><span class="p">(</span><span class="n">loop_vars</span><span class="o">=</span><span class="p">[</span><span class="n">step</span><span class="p">])</span>
<span class="k">with</span> <span class="n">wloop</span><span class="o">.</span><span class="n">frame</span><span class="p">():</span>
<span class="n">wloop</span><span class="o">.</span><span class="n">break_if</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">equal</span><span class="p">(</span><span class="n">step</span><span class="p">,</span> <span class="n">num_steps</span><span class="p">)</span>
<span class="n">pre_state</span> <span class="o">=</span> <span class="n">states</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">step</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">default_state</span><span class="p">)</span>
<span class="n">step_input</span> <span class="o">=</span> <span class="n">step_inputs</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">step</span><span class="p">)</span>
<span class="n">state</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">sigmoid</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">U</span><span class="p">,</span> <span class="n">pre_state</span><span class="p">)</span> <span class="o">+</span> <span class="n">pd</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="n">step_input</span><span class="p">))</span>
<span class="n">states</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">step</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span>
<span class="n">step_outputs</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">step</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span> <span class="c1"># output state</span>
<span class="n">step</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">state</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
<span class="n">output</span> <span class="o">=</span> <span class="n">step_outputs</span><span class="o">.</span><span class="n">stack</span><span class="p">()</span>
</pre></div>
</div>
<div class="section" id="background">
<span id="background"></span><h2>Background<a class="headerlink" href="#background" title="Permalink to this headline"></a></h2>
<p>Steps are one of the core concepts of RNN. In each time step of RNN, there should be several input segments, states, and output segments; all these components act like arrays, for example, call <code class="docutils literal"><span class="pre">states[step_id]</span></code> will get the state in <code class="docutils literal"><span class="pre">step_id</span></code>th time step.</p>
<p>An RNN can be implemented with the following pseudocode</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="n">Array</span> <span class="n">states</span><span class="p">;</span>
<span class="n">Array</span> <span class="n">input_segments</span><span class="p">;</span>
<span class="n">Array</span> <span class="n">output_segments</span><span class="p">;</span>
<span class="n">Parameter</span> <span class="n">W</span><span class="p">,</span> <span class="n">U</span><span class="p">;</span>
<span class="n">step</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">seq_len</span> <span class="o">=</span> <span class="mi">12</span>
<span class="n">while_loop</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">step</span> <span class="o">==</span> <span class="n">seq_len</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span>
<span class="n">states</span><span class="p">[</span><span class="n">step</span><span class="p">]</span> <span class="o">=</span> <span class="n">sigmoid</span><span class="p">(</span><span class="n">W</span> <span class="o">*</span> <span class="n">states</span><span class="p">[</span><span class="n">step</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">U</span> <span class="o">*</span> <span class="n">input_segments</span><span class="p">[</span><span class="n">step</span><span class="p">]);</span>
<span class="n">output_segments</span><span class="p">[</span><span class="n">step</span><span class="p">]</span> <span class="o">=</span> <span class="n">states</span><span class="p">[</span><span class="n">step</span><span class="p">]</span> <span class="c1">// take state as output</span>
<span class="n">step</span><span class="o">++</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>According to the <a class="reference external" href="https://github.com/PaddlePaddle/Paddle/issues/4561">RNN roadmap</a>, there are several different RNNs that PaddlePaddle will eventually support.</p>
<p>Currently, the basic RNN implementation supported by PaddlePaddle is the <code class="docutils literal"><span class="pre">recurrent_op</span></code> which takes tensors as input and splits them into <code class="docutils literal"><span class="pre">input_segments</span></code>.</p>
<p>Since a tensor cannot store variable-length sequences directly, PaddlePaddle implements the tensor with level of details (<code class="docutils literal"><span class="pre">LoDTensor</span></code> for short).
Segmenting the <code class="docutils literal"><span class="pre">LoDTensor</span></code> is much more complicated than splitting a tensor, that makes it necessary to refactor the <code class="docutils literal"><span class="pre">recurrent_op</span></code> with <code class="docutils literal"><span class="pre">LoDTensor</span></code> segmenting support.</p>
<p>As the next step in RNN support, <code class="docutils literal"><span class="pre">dynamic_recurrent_op</span></code> should be introduced to handle inputs with variable-length sequences.</p>
<p>The implementation is similar to <code class="docutils literal"><span class="pre">recurrent_op</span></code>.
The key difference is the way <strong>the original input <code class="docutils literal"><span class="pre">LoDTensors</span></code> and outupts are split to get the <code class="docutils literal"><span class="pre">input_segments</span></code> and the <code class="docutils literal"><span class="pre">output_segments</span></code>.</strong></p>
<p>Though it can&#8217;t be built over <code class="docutils literal"><span class="pre">recurrent_op</span></code> or <code class="docutils literal"><span class="pre">dynamic_recurrent_op</span></code> directly,
the logic behind splitting a tensor or a LoD tensor into <code class="docutils literal"><span class="pre">input_segments</span></code> remains the same.</p>
</div>
<div class="section" id="why-tensorarray">
<span id="why-tensorarray"></span><h2>Why <code class="docutils literal"><span class="pre">TensorArray</span></code><a class="headerlink" href="#why-tensorarray" title="Permalink to this headline"></a></h2>
<p>The logic behind splitting the inputs to segments, states and outputs is similar and can be shared in a seperate module.</p>
<p>The array of <code class="docutils literal"><span class="pre">states</span></code>, <code class="docutils literal"><span class="pre">input_segments</span></code> and <code class="docutils literal"><span class="pre">output_segments</span></code> would be exposed to users when writing a dynamic RNN model similar to the above pseudo codes.</p>
<p>So there should be an array-like container, which can store the segments of a tensor or LoD tensor.</p>
<p><strong>This container can store an array of tensors and provides several methods to split a tensor or a LoD tensor</strong> .
This is where the notion of <code class="docutils literal"><span class="pre">TensorArray</span></code> comes from.</p>
</div>
<div class="section" id="introduce-tensorarray-to-uniform-all-the-three-rnns">
<span id="introduce-tensorarray-to-uniform-all-the-three-rnns"></span><h2>Introduce TensorArray to uniform all the three RNNs<a class="headerlink" href="#introduce-tensorarray-to-uniform-all-the-three-rnns" title="Permalink to this headline"></a></h2>
<p>TensorArray as a new concept is borrowed from TensorFlow,
it is meant to be used with dynamic iteration primitives such as <code class="docutils literal"><span class="pre">while_loop</span></code> and <code class="docutils literal"><span class="pre">map_fn</span></code>.</p>
<p>This concept can be used to support our new design of dynamic operations, and help to refactor some existing variant-sentence-related layers,
such as <code class="docutils literal"><span class="pre">RecurrentGradientMachine</span></code>.</p>
such as <code class="docutils literal"><span class="pre">recurrent_op</span></code>, <code class="docutils literal"><span class="pre">RecurrentGradientMachine</span></code>.</p>
<p>In <a class="reference external" href="https://github.com/PaddlePaddle/Paddle/pull/4401">our design for dynamic RNN</a>,
<code class="docutils literal"><span class="pre">TensorArray</span></code> is used to segment inputs and store states in all time steps.
By providing some methods similar to a C++ array,
the definition of some state-based dynamic models such as RNN could be more natural and highly flexible.</p>
<div class="section" id="dynamic-related-methods">
<span id="dynamic-related-methods"></span><h2>Dynamic-Related Methods<a class="headerlink" href="#dynamic-related-methods" title="Permalink to this headline"></a></h2>
<p>Some basic methods should be proposed as follows:</p>
<div class="section" id="stack">
<span id="stack"></span><h3>stack()<a class="headerlink" href="#stack" title="Permalink to this headline"></a></h3>
<p>Pack the values in a <code class="docutils literal"><span class="pre">TensorArray</span></code> into a tensor with rank one higher than each tensor in <code class="docutils literal"><span class="pre">values</span></code>.</p>
</div>
<div class="section" id="unstack-axis-0">
<span id="unstack-axis-0"></span><h3>unstack(axis=0)<a class="headerlink" href="#unstack-axis-0" title="Permalink to this headline"></a></h3>
<p>Unpacks the given dimension of a rank-<code class="docutils literal"><span class="pre">R</span></code> tensor into rank-<code class="docutils literal"><span class="pre">(R-1)</span></code> tensors.</p>
the definition of some state-based dynamic models such as RNN can be more natural and highly flexible.</p>
</div>
<div class="section" id="concat">
<span id="concat"></span><h3>concat()<a class="headerlink" href="#concat" title="Permalink to this headline"></a></h3>
<p>Return the values in the <code class="docutils literal"><span class="pre">TensorArray</span></code> as a concatenated Tensor.</p>
</div>
<div class="section" id="write-index-value-data-shared-true">
<span id="write-index-value-data-shared-true"></span><h3>write(index, value, data_shared=true)<a class="headerlink" href="#write-index-value-data-shared-true" title="Permalink to this headline"></a></h3>
<p>Write value into index of the TensorArray.</p>
</div>
<div class="section" id="read-index">
<span id="read-index"></span><h3>read(index)<a class="headerlink" href="#read-index" title="Permalink to this headline"></a></h3>
<p>Read the value at location <code class="docutils literal"><span class="pre">index</span></code> in the <code class="docutils literal"><span class="pre">TensorArray</span></code>.</p>
<div class="section" id="dynamic-operations-on-tensorarray">
<span id="dynamic-operations-on-tensorarray"></span><h2>Dynamic-operations on TensorArray<a class="headerlink" href="#dynamic-operations-on-tensorarray" title="Permalink to this headline"></a></h2>
<p><code class="docutils literal"><span class="pre">TensorArray</span></code> will be used directly when defining dynamic models, so some operators listed below should be implemented</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># several helper operators for TensorArray</span>
<span class="k">def</span> <span class="nf">tensor_array_stack</span><span class="p">(</span><span class="n">ta</span><span class="p">,</span> <span class="n">tensor</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> get a tensor array `ta`, return a packed `tensor`.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">tensor_array_unstack</span><span class="p">(</span><span class="n">tensor</span><span class="p">,</span> <span class="n">ta</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> get a `tensor`, unstack it and get a tensor array `ta`.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">tensor_array_write</span><span class="p">(</span><span class="n">ta</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">tensor</span><span class="p">,</span> <span class="n">data_shared</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> get a `tensor` and a scalar tensor `index`, write `tensor` into index-th</span>
<span class="sd"> value of the tensor array `ta`.</span>
<span class="sd"> `data_shared` is an attribute that specifies whether to copy or reference the tensors.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">tensor_array_read</span><span class="p">(</span><span class="n">ta</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">tensor</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> get a tensor array `ta`, a scalar tensor `index`, read the index-th value of</span>
<span class="sd"> `ta` and return as the `tensor`.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">tensor_array_size</span><span class="p">(</span><span class="n">ta</span><span class="p">,</span> <span class="n">tensor</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> get a tensor array `ta`, return the size of `ta` and return as the scalar `tensor`.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
</pre></div>
</div>
<div class="section" id="size">
<span id="size"></span><h3>size()<a class="headerlink" href="#size" title="Permalink to this headline"></a></h3>
<p>Return the number of values.</p>
<p>It is trivial for users to use so many low-level operators, so some helper methods should be proposed in python wrapper to make <code class="docutils literal"><span class="pre">TensorArray</span></code> easier to use,
for example</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">TensorArray</span><span class="p">:</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
<span class="bp">self</span><span class="o">.</span><span class="n">desc</span> <span class="o">=</span> <span class="n">TensorArrayDesc</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">stack</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Pack the values in a `TensorArray` into a tensor with rank one higher</span>
<span class="sd"> than each tensor in `values`.</span>
<span class="sd"> `stack` can be used to split tensor into time steps for RNN or whileloop.</span>
<span class="sd"> @name: str</span>
<span class="sd"> the name of the variable to output.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">tensor</span> <span class="o">=</span> <span class="n">NewVar</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
<span class="n">tensor_array_stack</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">tensor</span><span class="p">)</span>
<span class="k">return</span> <span class="n">tensor</span>
<span class="k">def</span> <span class="nf">unstack</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Unpacks the given dimension of a rank-`R` tensor into rank-`(R-1)` tensors.</span>
<span class="sd"> `unstack` can be used to concatenate all the time steps for RNN or whileloop.</span>
<span class="sd"> @input: str</span>
<span class="sd"> the name of input tensor</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">tensor_array_unstack</span><span class="p">(</span><span class="n">tensor</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">data_shared</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Write value into index of the TensorArray.</span>
<span class="sd"> If `data_shared` is set to True, than the index-th value in TensorArray will</span>
<span class="sd"> be shared with the tensor passed in.</span>
<span class="sd"> @index: str</span>
<span class="sd"> name of a scalar tensor</span>
<span class="sd"> @value: str</span>
<span class="sd"> name of a tensor</span>
<span class="sd"> @data_shared: bool</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">tensor_array_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">data_shared</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">output</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Read the value at location `index` in the `TensorArray`.</span>
<span class="sd"> @index: str</span>
<span class="sd"> name of a scalar tensor</span>
<span class="sd"> @output:</span>
<span class="sd"> name of a output variable</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">tensor_array_read</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">output</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">size</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">output</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Return the number of values.</span>
<span class="sd"> @output: str</span>
<span class="sd"> name of a scalar tensor</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">tensor_array_size</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">output</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="lodtensor-related-supports">
<span id="lodtensor-related-supports"></span><h2>LoDTensor-related Supports<a class="headerlink" href="#lodtensor-related-supports" title="Permalink to this headline"></a></h2>
<p>The <code class="docutils literal"><span class="pre">RecurrentGradientMachine</span></code> in Paddle serves as a flexible RNN layer; it takes variant length sequences as input,
because each step of RNN could only take a tensor-represented batch of data as input,
<p>The <code class="docutils literal"><span class="pre">RecurrentGradientMachine</span></code> in Paddle serves as a flexible RNN layer; it takes varience-length sequences as input, and output sequences too.</p>
<p>Since each step of RNN can only take a tensor-represented batch of data as input,
some preprocess should be taken on the inputs such as sorting the sentences by their length in descending order and cut each word and pack to new batches.</p>
<p>Such cut-like operations can be embedded into <code class="docutils literal"><span class="pre">TensorArray</span></code> as general methods called <code class="docutils literal"><span class="pre">unpack</span></code> and <code class="docutils literal"><span class="pre">pack</span></code>.</p>
<p>With these two methods, a variant-sentence-RNN can be implemented like</p>
<p>Such cut-like operations can be embedded into <code class="docutils literal"><span class="pre">TensorArray</span></code> as general methods called <code class="docutils literal"><span class="pre">unpack</span></code> and <code class="docutils literal"><span class="pre">pack</span></code>,
these two operations are similar to <code class="docutils literal"><span class="pre">stack</span></code> and <code class="docutils literal"><span class="pre">unstack</span></code> except that they operate on variable-length sequences formated as a LoD tensor rather than a tensor.</p>
<p>Some definitions are like</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">unpack</span><span class="p">(</span><span class="n">level</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Split LodTensor in some `level` and generate batches, if set `sort_by_length`,</span>
<span class="sd"> will sort by length.</span>
<span class="sd"> Returns:</span>
<span class="sd"> - a new `TensorArray`, whose values are LodTensors and represents batches</span>
<span class="sd"> of data.</span>
<span class="sd"> - an int32 Tensor, which stores the map from the new batch&#39;s indices to</span>
<span class="sd"> original LoDTensor</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">pack</span><span class="p">(</span><span class="n">level</span><span class="p">,</span> <span class="n">indices_map</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Recover the original LoD-arranged LoDTensor with the values in a `TensorArray`</span>
<span class="sd"> and `level` and `indices_map`.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>With these two methods, a varience-length sentence supported RNN can be implemented like</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="c1">// input is the varient-length data</span>
<span class="n">LodTensor</span> <span class="nf">sentence_input</span><span class="p">(</span><span class="n">xxx</span><span class="p">);</span>
<span class="n">TensorArray</span> <span class="n">ta</span><span class="p">;</span>
......@@ -245,20 +426,6 @@ some preprocess should be taken on the inputs such as sorting the sentences by t
</div>
<p>the code above shows that by embedding the LoDTensor-related preprocess operations into <code class="docutils literal"><span class="pre">TensorArray</span></code>,
the implementation of a RNN that supports varient-length sentences is far more concise than <code class="docutils literal"><span class="pre">RecurrentGradientMachine</span></code> because the latter mixes all the codes together, hard to read and extend.</p>
<p>some details are as follows.</p>
<div class="section" id="unpack-level-sort-by-length">
<span id="unpack-level-sort-by-length"></span><h3>unpack(level, sort_by_length)<a class="headerlink" href="#unpack-level-sort-by-length" title="Permalink to this headline"></a></h3>
<p>Split LodTensor in some <code class="docutils literal"><span class="pre">level</span></code> and generate batches, if set <code class="docutils literal"><span class="pre">sort_by_length</span></code>, will sort by length.</p>
<p>Returns:</p>
<ul class="simple">
<li>a new <code class="docutils literal"><span class="pre">TensorArray</span></code>, whose values are LodTensors and represents batches of data.</li>
<li>an int32 Tensor, which stores the map from the new batch&#8217;s indices to original LoDTensor</li>
</ul>
</div>
<div class="section" id="pack-level-indices-map">
<span id="pack-level-indices-map"></span><h3>pack(level, indices_map)<a class="headerlink" href="#pack-level-indices-map" title="Permalink to this headline"></a></h3>
<p>Recover the original LoD-arranged LoDTensor with the values in a <code class="docutils literal"><span class="pre">TensorArray</span></code> and <code class="docutils literal"><span class="pre">level</span></code> and <code class="docutils literal"><span class="pre">indices_map</span></code>.</p>
</div>
</div>
</div>
......
因为 它太大了无法显示 source diff 。你可以改为 查看blob
IfOp should have only one branch. An IfOp operator takes a `cond` variable whose value must be a vector of N boolean elements. Its return value has N instances. If cond[i] == True, input instance input[i] will go through true_block() and generate output[i]; otherwise it will produce output from false_bloack().
# The `IfElse` Operator
```python
import paddle as pd
PaddlePaddle's `IfElse` operator differs from TensorFlow's:
x = var()
y = var()
cond = var()
default_value = var()
b = pd.create_ifelseop(inputs=[x], output_num=1)
with b.true_block():
x = b.inputs(0)
z = operator.add(x, y)
b.set_output(0, operator.softmax(z))
with b.false_block():
x = b.inputs(0)
z = layer.fc(x)
b.set_output(0, operator.softmax(z))
out = b(cond)
```
- the TensorFlow version takes a scalar boolean value as the condition so that the whole mini-batch goes to either the true or the false branch, whereas
- the PaddlePaddle version takes a vector of boolean value as the condition, and instances corresponding to true values go to the true branch, those corresponding to false values go to the false branch.
## Example
The following PaddlePaddle program shows the usage of the IfElse operator:
If only true_block is set in an IfElseOp, a special case is that we can have a default value for false as:
```python
import paddle as pd
x = var()
y = var()
cond = var()
default_value = var()
b = pd.create_ifelseop(inputs=[x], output_num=1, default_value)
with b.true_block():
x = b.inputs(0)
z = operator.add(x, y)
b.set_output(0, operator.softmax(z))
x = minibatch([10, 20, 30]) # shape=[None, 1]
y = var(1) # shape=[1], value=1
z = minibatch([10, 20, 30]) # shape=[None, 1]
cond = larger_than(x, 15) # [false, true, true]
ie = pd.ifelse()
with ie.true_block():
d = pd.layer.add(x, y)
ie.output(d, pd.layer.softmax(d))
with ie.false_block():
d = pd.layer.fc(z)
ie.output(d, d+1)
o1, o2 = ie(cond)
```
out = b(cond)
A challenge to implement the `IfElse` operator is to infer those variables to be split, or, say, to identify the variable of the mini-batch or those derived from the mini-batch.
An equivalent C++ program is as follows:
```c++
namespace pd = paddle;
int x = 10;
int y = 1;
int z = 10;
bool cond = false;
int o1, o2;
if (cond) {
int d = x + y;
o1 = z;
o2 = pd::layer::softmax(z);
} else {
int d = pd::layer::fc(z);
o1 = d;
o2 = d+1;
}
```
where default_value is a list of vars for `cond` == False.
# Design for TensorArray
This design doc presents the necessity of a new C++ class `TensorArray`.
In addition to the very simple C++ implementation
```c++
class TensorArray {
public:
explicit TensorArray(const LoDTensor&);
explicit TensorArray(size_t size);
private:
vector<LoDTensor> values_;
};
```
We also need to expose it to PaddlePaddle's Python API,
because users would want to use it with our very flexible operators `WhileLoop`.
An example for a RNN based on dynamic operators is
```python
input = pd.data(...)
num_steps = Var(12)
TensorArray states(size=num_steps)
TensorArray step_inputs(unstack_from=input)
TensorArray step_outputs(size=num_steps)
W = Tensor(...)
U = Tensor(...)
default_state = some_op()
step = Var(1)
wloop = paddle.create_whileloop(loop_vars=[step])
with wloop.frame():
wloop.break_if(pd.equal(step, num_steps)
pre_state = states.read(step-1, default_state)
step_input = step_inputs.read(step)
state = pd.sigmoid(pd.matmul(U, pre_state) + pd.matmul(W, step_input))
states.write(step, state)
step_outputs.write(step, state) # output state
step.update(state+1)
output = step_outputs.stack()
```
## Background
Steps are one of the core concepts of RNN. In each time step of RNN, there should be several input segments, states, and output segments; all these components act like arrays, for example, call `states[step_id]` will get the state in `step_id`th time step.
An RNN can be implemented with the following pseudocode
```c++
Array states;
Array input_segments;
Array output_segments;
Parameter W, U;
step = 1
seq_len = 12
while_loop {
if (step == seq_len) break;
states[step] = sigmoid(W * states[step-1] + U * input_segments[step]);
output_segments[step] = states[step] // take state as output
step++;
}
```
According to the [RNN roadmap](https://github.com/PaddlePaddle/Paddle/issues/4561), there are several different RNNs that PaddlePaddle will eventually support.
Currently, the basic RNN implementation supported by PaddlePaddle is the `recurrent_op` which takes tensors as input and splits them into `input_segments`.
Since a tensor cannot store variable-length sequences directly, PaddlePaddle implements the tensor with level of details (`LoDTensor` for short).
Segmenting the `LoDTensor` is much more complicated than splitting a tensor, that makes it necessary to refactor the `recurrent_op` with `LoDTensor` segmenting support.
As the next step in RNN support, `dynamic_recurrent_op` should be introduced to handle inputs with variable-length sequences.
The implementation is similar to `recurrent_op`.
The key difference is the way **the original input `LoDTensors` and outupts are split to get the `input_segments` and the `output_segments`.**
Though it can't be built over `recurrent_op` or `dynamic_recurrent_op` directly,
the logic behind splitting a tensor or a LoD tensor into `input_segments` remains the same.
## Why `TensorArray`
The logic behind splitting the inputs to segments, states and outputs is similar and can be shared in a seperate module.
The array of `states`, `input_segments` and `output_segments` would be exposed to users when writing a dynamic RNN model similar to the above pseudo codes.
So there should be an array-like container, which can store the segments of a tensor or LoD tensor.
**This container can store an array of tensors and provides several methods to split a tensor or a LoD tensor** .
This is where the notion of `TensorArray` comes from.
## Introduce TensorArray to uniform all the three RNNs
TensorArray as a new concept is borrowed from TensorFlow,
it is meant to be used with dynamic iteration primitives such as `while_loop` and `map_fn`.
This concept can be used to support our new design of dynamic operations, and help to refactor some existing variant-sentence-related layers,
such as `RecurrentGradientMachine`.
such as `recurrent_op`, `RecurrentGradientMachine`.
In [our design for dynamic RNN](https://github.com/PaddlePaddle/Paddle/pull/4401),
`TensorArray` is used to segment inputs and store states in all time steps.
By providing some methods similar to a C++ array,
the definition of some state-based dynamic models such as RNN could be more natural and highly flexible.
## Dynamic-Related Methods
Some basic methods should be proposed as follows:
### stack()
Pack the values in a `TensorArray` into a tensor with rank one higher than each tensor in `values`.
### unstack(axis=0)
Unpacks the given dimension of a rank-`R` tensor into rank-`(R-1)` tensors.
### concat()
Return the values in the `TensorArray` as a concatenated Tensor.
### write(index, value, data_shared=true)
Write value into index of the TensorArray.
### read(index)
Read the value at location `index` in the `TensorArray`.
### size()
Return the number of values.
the definition of some state-based dynamic models such as RNN can be more natural and highly flexible.
## Dynamic-operations on TensorArray
`TensorArray` will be used directly when defining dynamic models, so some operators listed below should be implemented
```python
# several helper operators for TensorArray
def tensor_array_stack(ta, tensor):
'''
get a tensor array `ta`, return a packed `tensor`.
'''
pass
def tensor_array_unstack(tensor, ta):
'''
get a `tensor`, unstack it and get a tensor array `ta`.
'''
pass
def tensor_array_write(ta, index, tensor, data_shared):
'''
get a `tensor` and a scalar tensor `index`, write `tensor` into index-th
value of the tensor array `ta`.
`data_shared` is an attribute that specifies whether to copy or reference the tensors.
'''
pass
def tensor_array_read(ta, index, tensor):
'''
get a tensor array `ta`, a scalar tensor `index`, read the index-th value of
`ta` and return as the `tensor`.
'''
pass
def tensor_array_size(ta, tensor):
'''
get a tensor array `ta`, return the size of `ta` and return as the scalar `tensor`.
'''
pass
```
It is trivial for users to use so many low-level operators, so some helper methods should be proposed in python wrapper to make `TensorArray` easier to use,
for example
```python
class TensorArray:
def __init__(self, name):
self.name = name
self.desc = TensorArrayDesc()
def stack(self, name=None):
'''
Pack the values in a `TensorArray` into a tensor with rank one higher
than each tensor in `values`.
`stack` can be used to split tensor into time steps for RNN or whileloop.
@name: str
the name of the variable to output.
'''
tensor = NewVar(name)
tensor_array_stack(self.name, tensor)
return tensor
def unstack(self, input):
'''
Unpacks the given dimension of a rank-`R` tensor into rank-`(R-1)` tensors.
`unstack` can be used to concatenate all the time steps for RNN or whileloop.
@input: str
the name of input tensor
'''
tensor_array_unstack(tensor, self.name)
def write(self, index, value, data_shared=True):
'''
Write value into index of the TensorArray.
If `data_shared` is set to True, than the index-th value in TensorArray will
be shared with the tensor passed in.
@index: str
name of a scalar tensor
@value: str
name of a tensor
@data_shared: bool
'''
tensor_array_write(self.name, index, value, data_shared)
def read(self, index, output):
'''
Read the value at location `index` in the `TensorArray`.
@index: str
name of a scalar tensor
@output:
name of a output variable
'''
tensor_array_read(self.name, index, output)
def size(self, output):
'''
Return the number of values.
@output: str
name of a scalar tensor
'''
tensor_array_size(self.name, output)
```
## LoDTensor-related Supports
The `RecurrentGradientMachine` in Paddle serves as a flexible RNN layer; it takes variant length sequences as input,
because each step of RNN could only take a tensor-represented batch of data as input,
The `RecurrentGradientMachine` in Paddle serves as a flexible RNN layer; it takes varience-length sequences as input, and output sequences too.
Since each step of RNN can only take a tensor-represented batch of data as input,
some preprocess should be taken on the inputs such as sorting the sentences by their length in descending order and cut each word and pack to new batches.
Such cut-like operations can be embedded into `TensorArray` as general methods called `unpack` and `pack`.
Such cut-like operations can be embedded into `TensorArray` as general methods called `unpack` and `pack`,
these two operations are similar to `stack` and `unstack` except that they operate on variable-length sequences formated as a LoD tensor rather than a tensor.
Some definitions are like
```python
def unpack(level):
'''
Split LodTensor in some `level` and generate batches, if set `sort_by_length`,
will sort by length.
With these two methods, a variant-sentence-RNN can be implemented like
Returns:
- a new `TensorArray`, whose values are LodTensors and represents batches
of data.
- an int32 Tensor, which stores the map from the new batch's indices to
original LoDTensor
'''
pass
def pack(level, indices_map):
'''
Recover the original LoD-arranged LoDTensor with the values in a `TensorArray`
and `level` and `indices_map`.
'''
pass
```
With these two methods, a varience-length sentence supported RNN can be implemented like
```c++
// input is the varient-length data
......@@ -58,16 +269,3 @@ LoDTensor rnn_output = ta.pack(ta, indice_map);
```
the code above shows that by embedding the LoDTensor-related preprocess operations into `TensorArray`,
the implementation of a RNN that supports varient-length sentences is far more concise than `RecurrentGradientMachine` because the latter mixes all the codes together, hard to read and extend.
some details are as follows.
### unpack(level, sort_by_length)
Split LodTensor in some `level` and generate batches, if set `sort_by_length`, will sort by length.
Returns:
- a new `TensorArray`, whose values are LodTensors and represents batches of data.
- an int32 Tensor, which stores the map from the new batch's indices to original LoDTensor
### pack(level, indices_map)
Recover the original LoD-arranged LoDTensor with the values in a `TensorArray` and `level` and `indices_map`.
......@@ -8,7 +8,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>&lt;no title&gt; &mdash; PaddlePaddle 文档</title>
<title>The IfElse Operator &mdash; PaddlePaddle 文档</title>
......@@ -182,7 +182,7 @@
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li>&lt;no title&gt;</li>
<li>The <code class="docutils literal"><span class="pre">IfElse</span></code> Operator</li>
</ul>
</div>
......@@ -191,45 +191,55 @@
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<p>IfOp should have only one branch. An IfOp operator takes a <code class="docutils literal"><span class="pre">cond</span></code> variable whose value must be a vector of N boolean elements. Its return value has N instances. If cond[i] == True, input instance input[i] will go through true_block() and generate output[i]; otherwise it will produce output from false_bloack().</p>
<div class="section" id="the-ifelse-operator">
<span id="the-ifelse-operator"></span><h1>The <code class="docutils literal"><span class="pre">IfElse</span></code> Operator<a class="headerlink" href="#the-ifelse-operator" title="永久链接至标题"></a></h1>
<p>PaddlePaddle&#8217;s <code class="docutils literal"><span class="pre">IfElse</span></code> operator differs from TensorFlow&#8217;s:</p>
<ul class="simple">
<li>the TensorFlow version takes a scalar boolean value as the condition so that the whole mini-batch goes to either the true or the false branch, whereas</li>
<li>the PaddlePaddle version takes a vector of boolean value as the condition, and instances corresponding to true values go to the true branch, those corresponding to false values go to the false branch.</li>
</ul>
<div class="section" id="example">
<span id="example"></span><h2>Example<a class="headerlink" href="#example" title="永久链接至标题"></a></h2>
<p>The following PaddlePaddle program shows the usage of the IfElse operator:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">paddle</span> <span class="kn">as</span> <span class="nn">pd</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">cond</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">default_value</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">create_ifelseop</span><span class="p">(</span><span class="n">inputs</span><span class="o">=</span><span class="p">[</span><span class="n">x</span><span class="p">],</span> <span class="n">output_num</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">with</span> <span class="n">b</span><span class="o">.</span><span class="n">true_block</span><span class="p">():</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">inputs</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">b</span><span class="o">.</span><span class="n">set_output</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">operator</span><span class="o">.</span><span class="n">softmax</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>
<span class="k">with</span> <span class="n">b</span><span class="o">.</span><span class="n">false_block</span><span class="p">():</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">inputs</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">fc</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">b</span><span class="o">.</span><span class="n">set_output</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">operator</span><span class="o">.</span><span class="n">softmax</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">b</span><span class="p">(</span><span class="n">cond</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">minibatch</span><span class="p">([</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">])</span> <span class="c1"># shape=[None, 1]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">var</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># shape=[1], value=1</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">minibatch</span><span class="p">([</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">])</span> <span class="c1"># shape=[None, 1]</span>
<span class="n">cond</span> <span class="o">=</span> <span class="n">larger_than</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">15</span><span class="p">)</span> <span class="c1"># [false, true, true]</span>
<span class="n">ie</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">ifelse</span><span class="p">()</span>
<span class="k">with</span> <span class="n">ie</span><span class="o">.</span><span class="n">true_block</span><span class="p">():</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">ie</span><span class="o">.</span><span class="n">output</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">softmax</span><span class="p">(</span><span class="n">d</span><span class="p">))</span>
<span class="k">with</span> <span class="n">ie</span><span class="o">.</span><span class="n">false_block</span><span class="p">():</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">layer</span><span class="o">.</span><span class="n">fc</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="n">ie</span><span class="o">.</span><span class="n">output</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">d</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
<span class="n">o1</span><span class="p">,</span> <span class="n">o2</span> <span class="o">=</span> <span class="n">ie</span><span class="p">(</span><span class="n">cond</span><span class="p">)</span>
</pre></div>
</div>
<p>If only true_block is set in an IfElseOp, a special case is that we can have a default value for false as:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">paddle</span> <span class="kn">as</span> <span class="nn">pd</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">cond</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">default_value</span> <span class="o">=</span> <span class="n">var</span><span class="p">()</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">create_ifelseop</span><span class="p">(</span><span class="n">inputs</span><span class="o">=</span><span class="p">[</span><span class="n">x</span><span class="p">],</span> <span class="n">output_num</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">default_value</span><span class="p">)</span>
<span class="k">with</span> <span class="n">b</span><span class="o">.</span><span class="n">true_block</span><span class="p">():</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">inputs</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="n">b</span><span class="o">.</span><span class="n">set_output</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">operator</span><span class="o">.</span><span class="n">softmax</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">b</span><span class="p">(</span><span class="n">cond</span><span class="p">)</span>
<p>A challenge to implement the <code class="docutils literal"><span class="pre">IfElse</span></code> operator is to infer those variables to be split, or, say, to identify the variable of the mini-batch or those derived from the mini-batch.</p>
<p>An equivalent C++ program is as follows:</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="k">namespace</span> <span class="n">pd</span> <span class="o">=</span> <span class="n">paddle</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
<span class="kt">bool</span> <span class="n">cond</span> <span class="o">=</span> <span class="nb">false</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">o1</span><span class="p">,</span> <span class="n">o2</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">cond</span><span class="p">)</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">d</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">;</span>
<span class="n">o1</span> <span class="o">=</span> <span class="n">z</span><span class="p">;</span>
<span class="n">o2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">::</span><span class="n">layer</span><span class="o">::</span><span class="n">softmax</span><span class="p">(</span><span class="n">z</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">d</span> <span class="o">=</span> <span class="n">pd</span><span class="o">::</span><span class="n">layer</span><span class="o">::</span><span class="n">fc</span><span class="p">(</span><span class="n">z</span><span class="p">);</span>
<span class="n">o1</span> <span class="o">=</span> <span class="n">d</span><span class="p">;</span>
<span class="n">o2</span> <span class="o">=</span> <span class="n">d</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>where default_value is a list of vars for <code class="docutils literal"><span class="pre">cond</span></code> == False.</p>
</div>
</div>
</div>
......
......@@ -193,49 +193,230 @@
<div class="section" id="design-for-tensorarray">
<span id="design-for-tensorarray"></span><h1>Design for TensorArray<a class="headerlink" href="#design-for-tensorarray" title="永久链接至标题"></a></h1>
<p>This design doc presents the necessity of a new C++ class <code class="docutils literal"><span class="pre">TensorArray</span></code>.
In addition to the very simple C++ implementation</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">TensorArray</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
<span class="k">explicit</span> <span class="n">TensorArray</span><span class="p">(</span><span class="k">const</span> <span class="n">LoDTensor</span><span class="o">&amp;</span><span class="p">);</span>
<span class="k">explicit</span> <span class="nf">TensorArray</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">size</span><span class="p">);</span>
<span class="k">private</span><span class="o">:</span>
<span class="n">vector</span><span class="o">&lt;</span><span class="n">LoDTensor</span><span class="o">&gt;</span> <span class="n">values_</span><span class="p">;</span>
<span class="p">};</span>
</pre></div>
</div>
<p>We also need to expose it to PaddlePaddle&#8217;s Python API,
because users would want to use it with our very flexible operators <code class="docutils literal"><span class="pre">WhileLoop</span></code>.
An example for a RNN based on dynamic operators is</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="nb">input</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">data</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="n">num_steps</span> <span class="o">=</span> <span class="n">Var</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span>
<span class="n">TensorArray</span> <span class="n">states</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="n">num_steps</span><span class="p">)</span>
<span class="n">TensorArray</span> <span class="n">step_inputs</span><span class="p">(</span><span class="n">unstack_from</span><span class="o">=</span><span class="nb">input</span><span class="p">)</span>
<span class="n">TensorArray</span> <span class="n">step_outputs</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="n">num_steps</span><span class="p">)</span>
<span class="n">W</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="n">U</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="n">default_state</span> <span class="o">=</span> <span class="n">some_op</span><span class="p">()</span>
<span class="n">step</span> <span class="o">=</span> <span class="n">Var</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">wloop</span> <span class="o">=</span> <span class="n">paddle</span><span class="o">.</span><span class="n">create_whileloop</span><span class="p">(</span><span class="n">loop_vars</span><span class="o">=</span><span class="p">[</span><span class="n">step</span><span class="p">])</span>
<span class="k">with</span> <span class="n">wloop</span><span class="o">.</span><span class="n">frame</span><span class="p">():</span>
<span class="n">wloop</span><span class="o">.</span><span class="n">break_if</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">equal</span><span class="p">(</span><span class="n">step</span><span class="p">,</span> <span class="n">num_steps</span><span class="p">)</span>
<span class="n">pre_state</span> <span class="o">=</span> <span class="n">states</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">step</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">default_state</span><span class="p">)</span>
<span class="n">step_input</span> <span class="o">=</span> <span class="n">step_inputs</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">step</span><span class="p">)</span>
<span class="n">state</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">sigmoid</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">U</span><span class="p">,</span> <span class="n">pre_state</span><span class="p">)</span> <span class="o">+</span> <span class="n">pd</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">W</span><span class="p">,</span> <span class="n">step_input</span><span class="p">))</span>
<span class="n">states</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">step</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span>
<span class="n">step_outputs</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">step</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span> <span class="c1"># output state</span>
<span class="n">step</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">state</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
<span class="n">output</span> <span class="o">=</span> <span class="n">step_outputs</span><span class="o">.</span><span class="n">stack</span><span class="p">()</span>
</pre></div>
</div>
<div class="section" id="background">
<span id="background"></span><h2>Background<a class="headerlink" href="#background" title="永久链接至标题"></a></h2>
<p>Steps are one of the core concepts of RNN. In each time step of RNN, there should be several input segments, states, and output segments; all these components act like arrays, for example, call <code class="docutils literal"><span class="pre">states[step_id]</span></code> will get the state in <code class="docutils literal"><span class="pre">step_id</span></code>th time step.</p>
<p>An RNN can be implemented with the following pseudocode</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="n">Array</span> <span class="n">states</span><span class="p">;</span>
<span class="n">Array</span> <span class="n">input_segments</span><span class="p">;</span>
<span class="n">Array</span> <span class="n">output_segments</span><span class="p">;</span>
<span class="n">Parameter</span> <span class="n">W</span><span class="p">,</span> <span class="n">U</span><span class="p">;</span>
<span class="n">step</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">seq_len</span> <span class="o">=</span> <span class="mi">12</span>
<span class="n">while_loop</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">step</span> <span class="o">==</span> <span class="n">seq_len</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span>
<span class="n">states</span><span class="p">[</span><span class="n">step</span><span class="p">]</span> <span class="o">=</span> <span class="n">sigmoid</span><span class="p">(</span><span class="n">W</span> <span class="o">*</span> <span class="n">states</span><span class="p">[</span><span class="n">step</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">U</span> <span class="o">*</span> <span class="n">input_segments</span><span class="p">[</span><span class="n">step</span><span class="p">]);</span>
<span class="n">output_segments</span><span class="p">[</span><span class="n">step</span><span class="p">]</span> <span class="o">=</span> <span class="n">states</span><span class="p">[</span><span class="n">step</span><span class="p">]</span> <span class="c1">// take state as output</span>
<span class="n">step</span><span class="o">++</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>According to the <a class="reference external" href="https://github.com/PaddlePaddle/Paddle/issues/4561">RNN roadmap</a>, there are several different RNNs that PaddlePaddle will eventually support.</p>
<p>Currently, the basic RNN implementation supported by PaddlePaddle is the <code class="docutils literal"><span class="pre">recurrent_op</span></code> which takes tensors as input and splits them into <code class="docutils literal"><span class="pre">input_segments</span></code>.</p>
<p>Since a tensor cannot store variable-length sequences directly, PaddlePaddle implements the tensor with level of details (<code class="docutils literal"><span class="pre">LoDTensor</span></code> for short).
Segmenting the <code class="docutils literal"><span class="pre">LoDTensor</span></code> is much more complicated than splitting a tensor, that makes it necessary to refactor the <code class="docutils literal"><span class="pre">recurrent_op</span></code> with <code class="docutils literal"><span class="pre">LoDTensor</span></code> segmenting support.</p>
<p>As the next step in RNN support, <code class="docutils literal"><span class="pre">dynamic_recurrent_op</span></code> should be introduced to handle inputs with variable-length sequences.</p>
<p>The implementation is similar to <code class="docutils literal"><span class="pre">recurrent_op</span></code>.
The key difference is the way <strong>the original input <code class="docutils literal"><span class="pre">LoDTensors</span></code> and outupts are split to get the <code class="docutils literal"><span class="pre">input_segments</span></code> and the <code class="docutils literal"><span class="pre">output_segments</span></code>.</strong></p>
<p>Though it can&#8217;t be built over <code class="docutils literal"><span class="pre">recurrent_op</span></code> or <code class="docutils literal"><span class="pre">dynamic_recurrent_op</span></code> directly,
the logic behind splitting a tensor or a LoD tensor into <code class="docutils literal"><span class="pre">input_segments</span></code> remains the same.</p>
</div>
<div class="section" id="why-tensorarray">
<span id="why-tensorarray"></span><h2>Why <code class="docutils literal"><span class="pre">TensorArray</span></code><a class="headerlink" href="#why-tensorarray" title="永久链接至标题"></a></h2>
<p>The logic behind splitting the inputs to segments, states and outputs is similar and can be shared in a seperate module.</p>
<p>The array of <code class="docutils literal"><span class="pre">states</span></code>, <code class="docutils literal"><span class="pre">input_segments</span></code> and <code class="docutils literal"><span class="pre">output_segments</span></code> would be exposed to users when writing a dynamic RNN model similar to the above pseudo codes.</p>
<p>So there should be an array-like container, which can store the segments of a tensor or LoD tensor.</p>
<p><strong>This container can store an array of tensors and provides several methods to split a tensor or a LoD tensor</strong> .
This is where the notion of <code class="docutils literal"><span class="pre">TensorArray</span></code> comes from.</p>
</div>
<div class="section" id="introduce-tensorarray-to-uniform-all-the-three-rnns">
<span id="introduce-tensorarray-to-uniform-all-the-three-rnns"></span><h2>Introduce TensorArray to uniform all the three RNNs<a class="headerlink" href="#introduce-tensorarray-to-uniform-all-the-three-rnns" title="永久链接至标题"></a></h2>
<p>TensorArray as a new concept is borrowed from TensorFlow,
it is meant to be used with dynamic iteration primitives such as <code class="docutils literal"><span class="pre">while_loop</span></code> and <code class="docutils literal"><span class="pre">map_fn</span></code>.</p>
<p>This concept can be used to support our new design of dynamic operations, and help to refactor some existing variant-sentence-related layers,
such as <code class="docutils literal"><span class="pre">RecurrentGradientMachine</span></code>.</p>
such as <code class="docutils literal"><span class="pre">recurrent_op</span></code>, <code class="docutils literal"><span class="pre">RecurrentGradientMachine</span></code>.</p>
<p>In <a class="reference external" href="https://github.com/PaddlePaddle/Paddle/pull/4401">our design for dynamic RNN</a>,
<code class="docutils literal"><span class="pre">TensorArray</span></code> is used to segment inputs and store states in all time steps.
By providing some methods similar to a C++ array,
the definition of some state-based dynamic models such as RNN could be more natural and highly flexible.</p>
<div class="section" id="dynamic-related-methods">
<span id="dynamic-related-methods"></span><h2>Dynamic-Related Methods<a class="headerlink" href="#dynamic-related-methods" title="永久链接至标题"></a></h2>
<p>Some basic methods should be proposed as follows:</p>
<div class="section" id="stack">
<span id="stack"></span><h3>stack()<a class="headerlink" href="#stack" title="永久链接至标题"></a></h3>
<p>Pack the values in a <code class="docutils literal"><span class="pre">TensorArray</span></code> into a tensor with rank one higher than each tensor in <code class="docutils literal"><span class="pre">values</span></code>.</p>
</div>
<div class="section" id="unstack-axis-0">
<span id="unstack-axis-0"></span><h3>unstack(axis=0)<a class="headerlink" href="#unstack-axis-0" title="永久链接至标题"></a></h3>
<p>Unpacks the given dimension of a rank-<code class="docutils literal"><span class="pre">R</span></code> tensor into rank-<code class="docutils literal"><span class="pre">(R-1)</span></code> tensors.</p>
the definition of some state-based dynamic models such as RNN can be more natural and highly flexible.</p>
</div>
<div class="section" id="concat">
<span id="concat"></span><h3>concat()<a class="headerlink" href="#concat" title="永久链接至标题"></a></h3>
<p>Return the values in the <code class="docutils literal"><span class="pre">TensorArray</span></code> as a concatenated Tensor.</p>
</div>
<div class="section" id="write-index-value-data-shared-true">
<span id="write-index-value-data-shared-true"></span><h3>write(index, value, data_shared=true)<a class="headerlink" href="#write-index-value-data-shared-true" title="永久链接至标题"></a></h3>
<p>Write value into index of the TensorArray.</p>
</div>
<div class="section" id="read-index">
<span id="read-index"></span><h3>read(index)<a class="headerlink" href="#read-index" title="永久链接至标题"></a></h3>
<p>Read the value at location <code class="docutils literal"><span class="pre">index</span></code> in the <code class="docutils literal"><span class="pre">TensorArray</span></code>.</p>
<div class="section" id="dynamic-operations-on-tensorarray">
<span id="dynamic-operations-on-tensorarray"></span><h2>Dynamic-operations on TensorArray<a class="headerlink" href="#dynamic-operations-on-tensorarray" title="永久链接至标题"></a></h2>
<p><code class="docutils literal"><span class="pre">TensorArray</span></code> will be used directly when defining dynamic models, so some operators listed below should be implemented</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># several helper operators for TensorArray</span>
<span class="k">def</span> <span class="nf">tensor_array_stack</span><span class="p">(</span><span class="n">ta</span><span class="p">,</span> <span class="n">tensor</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> get a tensor array `ta`, return a packed `tensor`.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">tensor_array_unstack</span><span class="p">(</span><span class="n">tensor</span><span class="p">,</span> <span class="n">ta</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> get a `tensor`, unstack it and get a tensor array `ta`.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">tensor_array_write</span><span class="p">(</span><span class="n">ta</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">tensor</span><span class="p">,</span> <span class="n">data_shared</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> get a `tensor` and a scalar tensor `index`, write `tensor` into index-th</span>
<span class="sd"> value of the tensor array `ta`.</span>
<span class="sd"> `data_shared` is an attribute that specifies whether to copy or reference the tensors.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">tensor_array_read</span><span class="p">(</span><span class="n">ta</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">tensor</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> get a tensor array `ta`, a scalar tensor `index`, read the index-th value of</span>
<span class="sd"> `ta` and return as the `tensor`.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">tensor_array_size</span><span class="p">(</span><span class="n">ta</span><span class="p">,</span> <span class="n">tensor</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> get a tensor array `ta`, return the size of `ta` and return as the scalar `tensor`.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
</pre></div>
</div>
<div class="section" id="size">
<span id="size"></span><h3>size()<a class="headerlink" href="#size" title="永久链接至标题"></a></h3>
<p>Return the number of values.</p>
<p>It is trivial for users to use so many low-level operators, so some helper methods should be proposed in python wrapper to make <code class="docutils literal"><span class="pre">TensorArray</span></code> easier to use,
for example</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">TensorArray</span><span class="p">:</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
<span class="bp">self</span><span class="o">.</span><span class="n">desc</span> <span class="o">=</span> <span class="n">TensorArrayDesc</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">stack</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Pack the values in a `TensorArray` into a tensor with rank one higher</span>
<span class="sd"> than each tensor in `values`.</span>
<span class="sd"> `stack` can be used to split tensor into time steps for RNN or whileloop.</span>
<span class="sd"> @name: str</span>
<span class="sd"> the name of the variable to output.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">tensor</span> <span class="o">=</span> <span class="n">NewVar</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
<span class="n">tensor_array_stack</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">tensor</span><span class="p">)</span>
<span class="k">return</span> <span class="n">tensor</span>
<span class="k">def</span> <span class="nf">unstack</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Unpacks the given dimension of a rank-`R` tensor into rank-`(R-1)` tensors.</span>
<span class="sd"> `unstack` can be used to concatenate all the time steps for RNN or whileloop.</span>
<span class="sd"> @input: str</span>
<span class="sd"> the name of input tensor</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">tensor_array_unstack</span><span class="p">(</span><span class="n">tensor</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">data_shared</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Write value into index of the TensorArray.</span>
<span class="sd"> If `data_shared` is set to True, than the index-th value in TensorArray will</span>
<span class="sd"> be shared with the tensor passed in.</span>
<span class="sd"> @index: str</span>
<span class="sd"> name of a scalar tensor</span>
<span class="sd"> @value: str</span>
<span class="sd"> name of a tensor</span>
<span class="sd"> @data_shared: bool</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">tensor_array_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">data_shared</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">output</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Read the value at location `index` in the `TensorArray`.</span>
<span class="sd"> @index: str</span>
<span class="sd"> name of a scalar tensor</span>
<span class="sd"> @output:</span>
<span class="sd"> name of a output variable</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">tensor_array_read</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">index</span><span class="p">,</span> <span class="n">output</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">size</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">output</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Return the number of values.</span>
<span class="sd"> @output: str</span>
<span class="sd"> name of a scalar tensor</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">tensor_array_size</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">output</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="lodtensor-related-supports">
<span id="lodtensor-related-supports"></span><h2>LoDTensor-related Supports<a class="headerlink" href="#lodtensor-related-supports" title="永久链接至标题"></a></h2>
<p>The <code class="docutils literal"><span class="pre">RecurrentGradientMachine</span></code> in Paddle serves as a flexible RNN layer; it takes variant length sequences as input,
because each step of RNN could only take a tensor-represented batch of data as input,
<p>The <code class="docutils literal"><span class="pre">RecurrentGradientMachine</span></code> in Paddle serves as a flexible RNN layer; it takes varience-length sequences as input, and output sequences too.</p>
<p>Since each step of RNN can only take a tensor-represented batch of data as input,
some preprocess should be taken on the inputs such as sorting the sentences by their length in descending order and cut each word and pack to new batches.</p>
<p>Such cut-like operations can be embedded into <code class="docutils literal"><span class="pre">TensorArray</span></code> as general methods called <code class="docutils literal"><span class="pre">unpack</span></code> and <code class="docutils literal"><span class="pre">pack</span></code>.</p>
<p>With these two methods, a variant-sentence-RNN can be implemented like</p>
<p>Such cut-like operations can be embedded into <code class="docutils literal"><span class="pre">TensorArray</span></code> as general methods called <code class="docutils literal"><span class="pre">unpack</span></code> and <code class="docutils literal"><span class="pre">pack</span></code>,
these two operations are similar to <code class="docutils literal"><span class="pre">stack</span></code> and <code class="docutils literal"><span class="pre">unstack</span></code> except that they operate on variable-length sequences formated as a LoD tensor rather than a tensor.</p>
<p>Some definitions are like</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">unpack</span><span class="p">(</span><span class="n">level</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Split LodTensor in some `level` and generate batches, if set `sort_by_length`,</span>
<span class="sd"> will sort by length.</span>
<span class="sd"> Returns:</span>
<span class="sd"> - a new `TensorArray`, whose values are LodTensors and represents batches</span>
<span class="sd"> of data.</span>
<span class="sd"> - an int32 Tensor, which stores the map from the new batch&#39;s indices to</span>
<span class="sd"> original LoDTensor</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
<span class="k">def</span> <span class="nf">pack</span><span class="p">(</span><span class="n">level</span><span class="p">,</span> <span class="n">indices_map</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Recover the original LoD-arranged LoDTensor with the values in a `TensorArray`</span>
<span class="sd"> and `level` and `indices_map`.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>With these two methods, a varience-length sentence supported RNN can be implemented like</p>
<div class="highlight-c++"><div class="highlight"><pre><span></span><span class="c1">// input is the varient-length data</span>
<span class="n">LodTensor</span> <span class="nf">sentence_input</span><span class="p">(</span><span class="n">xxx</span><span class="p">);</span>
<span class="n">TensorArray</span> <span class="n">ta</span><span class="p">;</span>
......@@ -259,20 +440,6 @@ some preprocess should be taken on the inputs such as sorting the sentences by t
</div>
<p>the code above shows that by embedding the LoDTensor-related preprocess operations into <code class="docutils literal"><span class="pre">TensorArray</span></code>,
the implementation of a RNN that supports varient-length sentences is far more concise than <code class="docutils literal"><span class="pre">RecurrentGradientMachine</span></code> because the latter mixes all the codes together, hard to read and extend.</p>
<p>some details are as follows.</p>
<div class="section" id="unpack-level-sort-by-length">
<span id="unpack-level-sort-by-length"></span><h3>unpack(level, sort_by_length)<a class="headerlink" href="#unpack-level-sort-by-length" title="永久链接至标题"></a></h3>
<p>Split LodTensor in some <code class="docutils literal"><span class="pre">level</span></code> and generate batches, if set <code class="docutils literal"><span class="pre">sort_by_length</span></code>, will sort by length.</p>
<p>Returns:</p>
<ul class="simple">
<li>a new <code class="docutils literal"><span class="pre">TensorArray</span></code>, whose values are LodTensors and represents batches of data.</li>
<li>an int32 Tensor, which stores the map from the new batch&#8217;s indices to original LoDTensor</li>
</ul>
</div>
<div class="section" id="pack-level-indices-map">
<span id="pack-level-indices-map"></span><h3>pack(level, indices_map)<a class="headerlink" href="#pack-level-indices-map" title="永久链接至标题"></a></h3>
<p>Recover the original LoD-arranged LoDTensor with the values in a <code class="docutils literal"><span class="pre">TensorArray</span></code> and <code class="docutils literal"><span class="pre">level</span></code> and <code class="docutils literal"><span class="pre">indices_map</span></code>.</p>
</div>
</div>
</div>
......
因为 它太大了无法显示 source diff 。你可以改为 查看blob
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册