提交 2bb09cf1 编写于 作者: L luotao02

adjust format of new_layer and rnn, refine seq2seq doc

ISSUE=4604739 

git-svn-id: https://svn.baidu.com/idl/trunk/paddle@1453 1ad973e4-5ce8-4261-8a94-b56d1f490c56
上级 fcc40964
此差异已折叠。
......@@ -287,6 +287,7 @@ paddle train \
2>&1 | tee 'translation/gen.log'
```
- job: set job mode to test
- save_dir: the path of saved models
- num_passes and test_pass: loading model parameters from test_pass to (num_passes - 1), here only loads `data/wmt14_model/pass-00012`
You will see messages like this:
......
Recurrent Neural Network Configuration
=====================================
======================================
This tutorial will guide you how to configure recurrent neural network in PaddlePaddle. PaddlePaddle supports highly flexible and efficient recurrent neural network configuration. In this tutorial, you will learn how to:
- prepare sequence data for learning recurrent neural networks.
- configure recurrent neural network architecture.
- generate sequence with learned recurrent neural network models.
We will use vanilla recurrent neural network, and sequence to sequence model to guide you through these steps. The code of sequence to sequence model can be found at :code:`demo/seqToseq`.
=====================
Prepare Sequence Data
=====================
PaddlePaddle does not need any preprocessing to sequence data, such as padding. The only thing that needs to be done is to set the type of the corresponding type to input. For example, the following code snippets defines three input. All of them are sequences, and the size of them are :code:`src_dict`, :code:`trg_dict`, and :code:`trg_dict`:
We will use vanilla recurrent neural network, and sequence to sequence model to guide you through these steps. The code of sequence to sequence model can be found at `/demo/seqToseq`.
=================
Prepare Sequence Data
=================
PaddlePaddle does not need any preprocessing to sequence data, such as padding. The only thing that needs to be done is to set the type of the corresponding type to input. For example, the following code snippets defines three input. All of them are sequences, and the size of them are `src_dict`, `trg_dict`, and `trg_dict`::
.. code-block:: python
settings.slots = [
integer_value_sequence(len(settings.src_dict)),
integer_value_sequence(len(settings.trg_dict)),
integer_value_sequence(len(settings.trg_dict))
]
integer_value_sequence(len(settings.trg_dict))]
Then at the `process` function, each `yield` function will return three integer lists. Each integer list is treated as a sequence of integers::
Then at the :code:`process` function, each :code:`yield` function will return three integer lists. Each integer list is treated as a sequence of integers:
.. code-block:: python
yield src_ids, trg_ids, trg_ids_next
For more details description of how to write a data provider, please refer to :doc:`Python Data Provider <../py_data_provider_wrapper>`. The full data provider file is located at `./demo/seqToseq/dataprovider.py`.
For more details description of how to write a data provider, please refer to :doc:`Python Data Provider <../py_data_provider_wrapper>`. The full data provider file is located at :code:`demo/seqToseq/dataprovider.py`.
=================
===============================================
Configure Recurrent Neural Network Architecture
=================
===============================================
----------------
-------------------------------------
Simple Gated Recurrent Neural Network
----------------
-------------------------------------
Recurrent neural network process a sequence at each time step sequentially. An example of the architecture of LSTM is listed below.
.. image:: ./bi_lstm.jpg
:align: center
Generally speaking, a recurrent network perform the following operations from t=1 to t=T, or reversely from t=T to t=1.
Generally speaking, a recurrent network perform the following operations from :math:`t=1` to :math:`t=T`, or reversely from :math:`t=T` to :math:`t=1`.
.. math::
x_{t+1} = f_x(x_t), y_t = f_y(x_t)
where :math:`f_x(.)` is called *step function*, and :math:`f_y(.)` is called *output function*. In vanilla recurrent neural network, both of the step function and output function are very simple. However, PaddlePaddle supports the configuration of very complex architectures by modifying these two functions. We will use the sequence to sequence model with attention as an example to demonstrate how you can configure complex recurrent neural network models. In this section, we will use a simple vanilla recurrent neural network as an example of configuring simple recurrent neural network using `recurrent_group`. Notice that if you only need to use simple RNN, GRU, or LSTM, then `grumemory` and `lstmemory` is recommended because they are more computationally efficient than `recurrent_group`.
For vanilla RNN, at each time step, the *step function* is:
where :math:`f_x(.)` is called **step function**, and :math:`f_y(.)` is called **output function**. In vanilla recurrent neural network, both of the step function and output function are very simple. However, PaddlePaddle supports the configuration of very complex architectures by modifying these two functions. We will use the sequence to sequence model with attention as an example to demonstrate how you can configure complex recurrent neural network models. In this section, we will use a simple vanilla recurrent neural network as an example of configuring simple recurrent neural network using :code:`recurrent_group`. Notice that if you only need to use simple RNN, GRU, or LSTM, then :code:`grumemory` and :code:`lstmemory` is recommended because they are more computationally efficient than :code:`recurrent_group`.
For vanilla RNN, at each time step, the **step function** is:
.. math::
x_{t+1} = W_x x_t + W_i I_t + b
where :math:`x_t` is the RNN state, and :math:`I_t` is the input, :math:`W_x` and :math:`W_i` are transformation matrices for RNN states and inputs, respectively. :math:`b` is the bias.
Its *output function* simply takes :math:`x_t` as the output.
Its **output function** simply takes :math:`x_t` as the output.
`recurrent_group` is the most important tools for constructing recurrent neural networks. It defines the *step function*, *output function* and the inputs of the recurrent neural network. Notice that the `step` argument of this function implements both the `step function` and the `output function`::
:code:`recurrent_group` is the most important tools for constructing recurrent neural networks. It defines the **step function**, **output function** and the inputs of the recurrent neural network. Notice that the :code:`step` argument of this function implements both the :code:`step function` and the :code:`output function`:
.. code-block:: python
def simple_rnn(input,
size=None,
......@@ -91,30 +90,27 @@ Its *output function* simply takes :math:`x_t` as the output.
input=input)
PaddlePaddle uses memory to construct step function. *Memory* is the most important concept when constructing recurrent neural networks in PaddlePaddle. A memory is a state that is used recurrently in step functions, such as , :math:`x_{t+1} = f_x(x_t)`. One memory contains an *output* and a *input*. The output of memory at the current time step is utilized as the input of the memory at the next time step. A memory can also has a *boot layer*, whose output is utilized as the initial value of the memory. In our case, the output of the gated recurrent unit is employed as the output memory. Notice that the name of the layer `rnn_out` is the same as the name of `out_mem`. This means the output of the layer `rnn_out` (:math:`x_{t+1}`) is utilized as the *output* of `out_mem` memory.
PaddlePaddle uses memory to construct step function. **Memory** is the most important concept when constructing recurrent neural networks in PaddlePaddle. A memory is a state that is used recurrently in step functions, such as :math:`x_{t+1} = f_x(x_t)`. One memory contains an **output** and a **input**. The output of memory at the current time step is utilized as the input of the memory at the next time step. A memory can also has a **boot layer**, whose output is utilized as the initial value of the memory. In our case, the output of the gated recurrent unit is employed as the output memory. Notice that the name of the layer :code:`rnn_out` is the same as the name of :code:`out_mem`. This means the output of the layer :code:`rnn_out` (:math:`x_{t+1}`) is utilized as the **output** of :code:`out_mem` memory.
A memory can also be a sequence. In this case, at each time step, we have a sequence as the state of the recurrent neural network. This can be useful when constructing very complex recurrent neural network. Other advanced functions include defining multiple memories, and defining hierarchical recurrent neural network architecture using sub-sequence.
We return :code:`rnn_out` at the end of the function. It means that the output of the layer :code:`rnn_out` is utilized as the **output** function of the gated recurrent neural network.
We return `rnn_out` at the end of the function. It means that the output of the layer `rnn_out` is utilized as the *output* function of the gated recurrent neural network.
----------------
-----------------------------------------
Sequence to Sequence Model with Attention
----------------
-----------------------------------------
We will use the sequence to sequence model with attention as an example to demonstrate how you can configure complex recurrent neural network models. An illustration of the sequence to sequence model with attention is shown in the following figure.
.. image:: ./encoder-decoder-attention-model.png
:align: center
In this model, the source sequence :math:`S = \{s_1, \dots, s_T\}` is encoded with a bidirectional gated recurrent neural networks. The hidden states of the bidirectional gated recurrent neural network :math:`H_S = \{H_1, \dots, H_T\}` is called *encoder vector* The decoder is a gated recurrent neural network. When decoding each token :math:`y_t`, the gated recurrent neural network generates a set of weights :math:`W_S^t = \{W_1^t, \dots, W_T^t\}`, which are used to compute a weighted sum of the encoder vector. The weighted sum of the encoder vector is utilized to condition the generation of the token :math:`y_t`.
In this model, the source sequence :math:`S = \{s_1, \dots, s_T\}` is encoded with a bidirectional gated recurrent neural networks. The hidden states of the bidirectional gated recurrent neural network :math:`H_S = \{H_S1, \dots, H_sT\}` is called *encoder vector* The decoder is a gated recurrent neural network. When decoding each token :math:`y_t`, the gated recurrent neural network generates a set of weights :math:`W_S^t = \{W_S1^t, \dots, W_sT^t\}`, which are used to compute a weighted sum of the encoder vector. The weighted sum of the encoder vector is utilized to condition the generation of the token :math:`y_t`.
The encoder part of the model is listed below. It calls `grumemory` to represent gated recurrent neural network. It is the recommended way of using recurrent neural network if the network architecture is simple, because it is faster than `recurrent_group`. We have implemented most of the commonly used recurrent neural network architectures, you can refer to :doc:`Layers <../trainer_config_helpers/layers>` for more details.
The encoder part of the model is listed below. It calls :code:`grumemory` to represent gated recurrent neural network. It is the recommended way of using recurrent neural network if the network architecture is simple, because it is faster than :code:`recurrent_group`. We have implemented most of the commonly used recurrent neural network architectures, you can refer to :doc:`Layers <../trainer_config_helpers/layers>` for more details.
We also project the encoder vector to `decoder_size` dimensional space, get the first instance of the backward recurrent network, and project it to `decoder_size` dimensional space::
We also project the encoder vector to :code:`decoder_size` dimensional space, get the first instance of the backward recurrent network, and project it to :code:`decoder_size` dimensional space:
.. code-block:: python
# Define the data layer of the source sentence.
src_word_id = data_layer(name='source_language_word', size=source_dict_dim)
......@@ -143,8 +139,9 @@ We also project the encoder vector to `decoder_size` dimensional space, get the
decoder_boot = mixed_layer(input=[full_matrix_projection(backward_first)], size=decoder_size, act=TanhActivation())
The decoder uses `recurrent_group` to define the recurrent neural network. The step and output functions are defined in `gru_decoder_with_attention`::
The decoder uses :code:`recurrent_group` to define the recurrent neural network. The step and output functions are defined in :code:`gru_decoder_with_attention`:
.. code-block:: python
trg_embedding = embedding_layer(
input=data_layer(name='target_language_word',
......@@ -168,8 +165,9 @@ The decoder uses `recurrent_group` to define the recurrent neural network. The s
])
The implementation of the step function is listed as below. First, it defines the *memory* of the decoder network. Then it defines attention, gated recurrent unit step function, and the output function::
The implementation of the step function is listed as below. First, it defines the **memory** of the decoder network. Then it defines attention, gated recurrent unit step function, and the output function:
.. code-block:: python
def gru_decoder_with_attention(enc_vec, enc_proj, current_word):
# Defines the memory of the decoder.
......@@ -202,21 +200,22 @@ The implementation of the step function is listed as below. First, it defines th
=================
Generate Sequence
=================
After training the model, we can use it to generate sequences. A common practice is to use *beam search* to generate sequences. The following code snippets defines a beam search algorithm. Notice that `beam_search` function assumes the output function of the `step` returns a softmax normalized probability vector of the next token. We made the following changes to the model.
After training the model, we can use it to generate sequences. A common practice is to use **beam search** to generate sequences. The following code snippets defines a beam search algorithm. Notice that :code:`beam_search` function assumes the output function of the :code:`step` returns a softmax normalized probability vector of the next token. We made the following changes to the model.
* use `GeneratedInput` for trg_embedding. `GeneratedInput` computes the embedding of the generated token at the last time step for the input at the current time step.
* use `beam_search` function. This function needs to set:
* use :code:`GeneratedInput` for trg_embedding. :code:`GeneratedInput` computes the embedding of the generated token at the last time step for the input at the current time step.
* use :code:`beam_search` function. This function needs to set:
- `id_input`: the integer ID of the data, used to identify the corresponding output in the generated files.
- `dict_file`: the dictionary file for converting word id to word.
- `bos_id`: the start token. Every sentence starts with the start token.
- `eos_id`: the end token. Every sentence ends with the end token.
- `beam_size`: the beam size used in beam search.
- `max_length`: the maximum length of the generated sentences.
- `result_file`: the path of the generation result file.
- :code:`id_input`: the integer ID of the data, used to identify the corresponding output in the generated files.
- :code:`dict_file`: the dictionary file for converting word id to word.
- :code:`bos_id`: the start token. Every sentence starts with the start token.
- :code:`eos_id`: the end token. Every sentence ends with the end token.
- :code:`beam_size`: the beam size used in beam search.
- :code:`max_length`: the maximum length of the generated sentences.
- :code:`result_file`: the path of the generation result file.
The code is listed below:
The code is listed below::
.. code-block:: python
gen_inputs = [StaticInput(input=encoded_vector,
is_seq=True),
......@@ -249,4 +248,4 @@ The code is listed below::
Notice that this generation technique is only useful for decoder like generation process. If you are working on sequence tagging tasks, please refer to :doc:`Semantic Role Labeling Demo <../../../demo/semantic_role_labeling>` for more details.
The full configuration file is located at `./demo/seqToseq/seqToseq_net.py`.
The full configuration file is located at :code:`demo/seqToseq/seqToseq_net.py`.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册