Create the model. Args: source_vocab_size: size of the source vocabulary. target_vocab_size: size of the target vocabulary. buckets: a list of pairs (I, O), where I specifies maximum input length that will be processed in that bucket, and O specifies maximum output
(self,
source_vocab_size,
target_vocab_size,
buckets,
size,
num_layers,
max_gradient_norm,
batch_size,
learning_rate,
learning_rate_decay_factor,
use_lstm=False,
num_samples=512,
forward_only=False,
dtype=tf.float32)
| 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) |
| 90 | self.learning_rate_decay_op = self.learning_rate.assign( |
| 91 | self.learning_rate * learning_rate_decay_factor) |
| 92 | self.global_step = tf.Variable(0, trainable=False) |
| 93 | |
| 94 | # If we use sampled softmax, we need an output projection. |
| 95 | output_projection = None |
| 96 | softmax_loss_function = None |
| 97 | # Sampled softmax only makes sense if we sample less than vocabulary size. |
| 98 | if num_samples > 0 and num_samples < self.target_vocab_size: |
| 99 | w_t = tf.get_variable("proj_w", [self.target_vocab_size, size], dtype=dtype) |
| 100 | w = tf.transpose(w_t) |
| 101 | b = tf.get_variable("proj_b", [self.target_vocab_size], dtype=dtype) |
| 102 | output_projection = (w, b) |
| 103 | |
| 104 | def sampled_loss(labels, logits): |