Sequence-to-sequence model with attention and for multiple buckets. This class implements a multi-layer recurrent neural network as encoder, and an attention-based decoder. This is the same as the model described in this paper: http://arxiv.org/abs/1412.7449 - please look there for details,
| 30 | import seq2seq_patch |
| 31 | |
| 32 | class Seq2SeqModel(object): |
| 33 | """Sequence-to-sequence model with attention and for multiple buckets. |
| 34 | |
| 35 | This class implements a multi-layer recurrent neural network as encoder, |
| 36 | and an attention-based decoder. This is the same as the model described in |
| 37 | this paper: http://arxiv.org/abs/1412.7449 - please look there for details, |
| 38 | or into the seq2seq library for complete model implementation. |
| 39 | This class also allows to use GRU cells in addition to LSTM cells, and |
| 40 | sampled softmax to handle large output vocabulary size. A single-layer |
| 41 | version of this model, but with bi-directional encoder, was presented in |
| 42 | http://arxiv.org/abs/1409.0473 |
| 43 | and sampled softmax is described in Section 3 of the following paper. |
| 44 | http://arxiv.org/abs/1412.2007 |
| 45 | """ |
| 46 | |
| 47 | def __init__(self, |
| 48 | source_vocab_size, |
| 49 | target_vocab_size, |
| 50 | buckets, |
| 51 | size, |
| 52 | num_layers, |
| 53 | max_gradient_norm, |
| 54 | batch_size, |
| 55 | learning_rate, |
| 56 | learning_rate_decay_factor, |
| 57 | use_lstm=False, |
| 58 | num_samples=512, |
| 59 | forward_only=False, |
| 60 | dtype=tf.float32): |
| 61 | """Create the model. |
| 62 | |
| 63 | Args: |
| 64 | source_vocab_size: size of the source vocabulary. |
| 65 | target_vocab_size: size of the target vocabulary. |
| 66 | buckets: a list of pairs (I, O), where I specifies maximum input length |
| 67 | that will be processed in that bucket, and O specifies maximum output |
| 68 | length. Training instances that have inputs longer than I or outputs |
| 69 | longer than O will be pushed to the next bucket and padded accordingly. |
| 70 | We assume that the list is sorted, e.g., [(2, 4), (8, 16)]. |
| 71 | size: number of units in each layer of the model. |
| 72 | num_layers: number of layers in the model. |
| 73 | max_gradient_norm: gradients will be clipped to maximally this norm. |
| 74 | batch_size: the size of the batches used during training; |
| 75 | the model construction is independent of batch_size, so it can be |
| 76 | changed after initialization if this is convenient, e.g., for decoding. |
| 77 | learning_rate: learning rate to start with. |
| 78 | learning_rate_decay_factor: decay learning rate by this much when needed. |
| 79 | use_lstm: if true, we use LSTM cells instead of GRU cells. |
| 80 | num_samples: number of samples for sampled softmax. |
| 81 | forward_only: if set, we do not construct the backward pass in the model. |
| 82 | dtype: the data type to use to store internal variables. |
| 83 | """ |
| 84 | self.source_vocab_size = source_vocab_size |
| 85 | self.target_vocab_size = target_vocab_size |
| 86 | self.buckets = buckets |
| 87 | self.batch_size = batch_size |
| 88 | self.learning_rate = tf.Variable( |
| 89 | float(learning_rate), trainable=False, dtype=dtype) |
nothing calls this directly
no outgoing calls
no test coverage detected