Skip to content

Commit fb53a03

Browse files
committed
Docs
1 parent 17c0857 commit fb53a03

File tree

5 files changed

+62
-16
lines changed

5 files changed

+62
-16
lines changed

.coveragerc

-3
This file was deleted.

config.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ def load_parameters():
148148
# Linear reduction: new_lr = lr * LR_GAMMA
149149
# Exponential reduction: new_lr = lr * LR_REDUCER_EXP_BASE ** (current_nb / LR_HALF_LIFE) * LR_GAMMA
150150
# Noam reduction: new_lr = lr * min(current_nb ** LR_REDUCER_EXP_BASE, current_nb * LR_HALF_LIFE ** WARMUP_EXP)
151-
LR_REDUCER_EXP_BASE = -0.5 # Base for the exponential decay.
152-
LR_HALF_LIFE = 100 # Factor/warmup steps for exponenital/noam decay.
151+
LR_REDUCER_EXP_BASE = -0.5 # Base for the exponential decay.
152+
LR_HALF_LIFE = 100 # Factor/warmup steps for exponenital/noam decay.
153153
WARMUP_EXP = -1.5 # Warmup steps for noam decay.
154154
MIN_LR = 1e-9 # Minimum value allowed for the decayed LR
155155

@@ -176,7 +176,7 @@ def load_parameters():
176176
MODEL_TYPE = 'AttentionRNNEncoderDecoder' # Model to train. See model_zoo.py for more info.
177177
# Supported architectures: 'AttentionRNNEncoderDecoder' and 'Transformer'.
178178

179-
# Hyperparameters common to all models
179+
# Common hyperparameters for all models
180180
# # # # # # # # # # # # # # # # # # # # # # # #
181181
TRAINABLE_ENCODER = True # Whether the encoder's weights should be modified during training.
182182
TRAINABLE_DECODER = True # Whether the decoder's weights should be modified during training.

demo-web/sample_server.py

+41-8
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,28 @@ class NMTSampler:
139139
def __init__(self, models, dataset, params, params_prediction, params_training, model_tokenize_f, model_detokenize_f, general_tokenize_f,
140140
general_detokenize_f, mapping=None, word2index_x=None, word2index_y=None, index2word_y=None,
141141
excluded_words=None, unk_id=1, eos_symbol='/', online=False, verbose=0):
142+
"""
143+
Builds an NMTSampler: An object containing models and dataset, for the interactive-predictive and adaptive framework.
144+
:param models:
145+
:param dataset:
146+
:param dict params: All hyperparameters of the model.
147+
:param dict params_prediction: Hyperparameters regarding prediction and search.
148+
:param dict params_training: Hyperparamters regarding incremental training.
149+
:param function model_tokenize_f: Function used for tokenizing the input sentence. E.g. BPE.
150+
:param function model_detokenize_f: Function used for detokenizing the output sentence. E.g. BPE revert.
151+
:param function general_tokenize_f: Function used for tokenizing the input sentence. E.g. Moses tokenizer.
152+
:param function general_detokenize_f: Function used for detokenizing the output sentence. E.g. Moses detokenizer.
153+
:param dict mapping: Source-target dictionary (for unk_replace heuristics 1 and 2).
154+
:param dict word2index_x: Mapping from word strings into indices for the source language.
155+
:param dict word2index_y: Mapping from word strings into indices for the target language.
156+
:param dict index2word_y: Mapping from indices into word strings for the target language.
157+
:param dict excluded_words: words that won't be generated in the middle of two isles. Currenly unused.
158+
:param int unk_id: Unknown word index.
159+
:param str eos_symbol: End-of-sentence symbol.
160+
:param bool online: Whether apply online learning after accepting each hypothesis.
161+
:param int verbose: Verbosity level.
162+
"""
163+
142164
self.models = models
143165
self.dataset = dataset
144166
self.params = params
@@ -165,7 +187,7 @@ def __init__(self, models, dataset, params, params_prediction, params_training,
165187
excluded_words=self.excluded_words,
166188
verbose=self.verbose)
167189

168-
# Compile Theano sampling function by generating a fake sample # TODO: Find a better way of doing this
190+
# Compile Theano sampling function by generating a fake sample. # TODO: Find a better way of doing this
169191
logger.info('Compiling sampler...')
170192
self.generate_sample('i')
171193
logger.info('Done.')
@@ -186,7 +208,18 @@ def __init__(self, models, dataset, params, params_prediction, params_training,
186208

187209
def generate_sample(self, source_sentence, validated_prefix=None, max_N=5, isle_indices=None,
188210
filtered_idx2word=None, unk_indices=None, unk_words=None):
189-
print ("In params prediction beam_size: ", self.params_prediction['beam_size'])
211+
"""
212+
Generate sample via constrained search. Options labeled with <<isles>> are untested
213+
and likely require some modifications to correctly work.
214+
:param source_sentence: Source sentence.
215+
:param validated_prefix: Prefix to keep in the output.
216+
:param max_N: Maximum number of words to generate between validated segments. <<isles>>
217+
:param isle_indices: Indices of the validated segments. <<isles>>
218+
:param filtered_idx2word: List of candidate words to be the next one to generate (after generating fixed_words).
219+
:param unk_indices: Positions of the unknown words.
220+
:param unk_words: Unknown words.
221+
:return:
222+
"""
190223
logger.log(2, 'Beam size: %d' % (self.params_prediction['beam_size']))
191224
generate_sample_start_time = time.time()
192225
if unk_indices is None:
@@ -295,10 +328,6 @@ def generate_sample(self, source_sentence, validated_prefix=None, max_N=5, isle_
295328
decoding_predictions_end_time = time.time()
296329
logger.log(2, 'decoding_predictions time: %.6f' % (decoding_predictions_end_time - decoding_predictions_start_time))
297330

298-
# for (words_idx, starting_pos), words in unk_in_isles:
299-
# for pos_unk_word, pos_hypothesis in enumerate(range(starting_pos, starting_pos + len(words_idx))):
300-
# hypothesis[pos_hypothesis] = words[pos_unk_word]
301-
302331
# UNK words management
303332
unk_management_start_time = time.time()
304333
unk_indices = list(unk_words_dict)
@@ -330,7 +359,12 @@ def generate_sample(self, source_sentence, validated_prefix=None, max_N=5, isle_
330359
return hypothesis
331360

332361
def learn_from_sample(self, source_sentence, target_sentence):
333-
362+
"""
363+
Incrementally adapt the model with the validated sample.
364+
:param source_sentence: Source sentence (x).
365+
:param target_sentence: Target sentence (y).
366+
:return:
367+
"""
334368
# Tokenize input
335369
tokenized_input = self.general_tokenize_f(source_sentence, escape=False)
336370
tokenized_input = self.model_tokenize_f(tokenized_input)
@@ -499,7 +533,6 @@ def main():
499533
for i in range(len(args.models))]
500534
models = [updateModel(model, path, -1, full_path=True) for (model, path) in zip(model_instances, args.models)]
501535

502-
# Set additional inputs to models if using a custom loss function
503536
# parameters['USE_CUSTOM_LOSS'] = True if 'PAS' in parameters['OPTIMIZER'] else False
504537
# if parameters.get('N_BEST_OPTIMIZER', False):
505538
# logger.info('Using N-best optimizer')

docs/source/requirements.rst

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ for obtaining the required packages for running this library.
1111

1212
Nevertheless, it is highly recommended to install and configure Theano_ or Tensorflow_ with the GPU and speed optimizations enabled.
1313

14+
Alternatively, you can run the `install.sh`_ script, which will also downloads Python::
15+
16+
git clone https://github.com/lvapeab/nmt-keras
17+
cd nmt-keras
18+
bash ./install.sh
19+
20+
1421
Requirements
1522
************
1623
- Our version of Keras_.
@@ -24,4 +31,5 @@ Requirements
2431
.. _Coco-caption: https://github.com/lvapeab/coco-caption
2532
.. _pip: https://en.wikipedia.org/wiki/Pip_(package_manager)
2633
.. _Theano: http://theano.readthedocs.io/en/latest/install.html#install
27-
.. _Tensorflow: https://www.tensorflow.org/install/
34+
.. _Tensorflow: https://www.tensorflow.org/install/
35+
.. _install.sh: https://github.com/lvapeab/nmt-keras/blob/master/install.sh

utils/average_models.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ def parse_args():
2525

2626

2727
def weighted_average(args):
28-
28+
"""
29+
Apply a weighted average to the models.
30+
:param args: Options for the averaging function:
31+
* models: Path to the models.
32+
* dest: Path to the averaged model. If unspecified, the model is saved in './model'
33+
* weights: Weight given to each model in the averaging. Should be the same number of weights than models.
34+
If unspecified, it applies the same weight to each model (1/N).
35+
:return:
36+
"""
2937
logger.info("Averaging %d models" % len(args.models))
3038
average_models(args.models, args.dest, weights=args.weights)
3139
logger.info('Averaging finished.')

0 commit comments

Comments
 (0)