nmt_keras package¶
Submodules¶
model_zoo¶
-
class
nmt_keras.model_zoo.
TranslationModel
(params, model_type='Translation_Model', verbose=1, structure_path=None, weights_path=None, model_name=None, vocabularies=None, store_path=None, set_optimizer=True, clear_dirs=True)¶ Bases:
keras_wrapper.cnn_model.Model_Wrapper
Translation model class. Instance of the Model_Wrapper class (see staged_keras_wrapper).
Parameters: - params (dict) – all hyperparameters of the model.
- model_type (str) – network name type (corresponds to any method defined in the section ‘MODELS’ of this class). Only valid if ‘structure_path’ == None.
- verbose (int) – set to 0 if you don’t want the model to output informative messages
- structure_path (str) – path to a Keras’ model json file. If we speficy this parameter then ‘type’ will be only an informative parameter.
- weights_path (str) – path to the pre-trained weights file (if None, then it will be initialized according to params)
- model_name (str) – optional name given to the network (if None, then it will be assigned to current time as its name)
- vocabularies (dict) – vocabularies used for word embedding
- store_path (str) – path to the folder where the temporal model packups will be stored
- set_optimizer (bool) – Compile optimizer or not.
- clear_dirs (bool) – Clean model directories or not.
-
AttentionRNNEncoderDecoder
(params)¶ - Neural machine translation with:
- BRNN encoder
- Attention mechansim on input sequence of annotations
- Conditional RNN for decoding
- Deep output layers:
- Context projected to output
- Last word projected to output
- Possibly deep encoder/decoder
- See:
Parameters: params (int) – Dictionary of hyper-params (see config.py) Returns: None
-
GroundHogModel
(params)¶ - Neural machine translation with:
- BRNN encoder
- Attention mechansim on input sequence of annotations
- Conditional RNN for decoding
- Deep output layers:
- Context projected to output
- Last word projected to output
- Possibly deep encoder/decoder
- See:
Parameters: params (int) – Dictionary of hyper-params (see config.py) Returns: None
-
Transformer
(params)¶ - Neural machine translation consisting in stacking blocks of:
- Multi-head attention.
- Dropout.
- Residual connection.
- Normalization.
- Position-wise feed-forward networks.
Positional information is injected to the model via embeddings with positional encoding.
Parameters: params (int) – Dictionary of params (see config.py) Returns: None
-
setOptimizer
(**kwargs)¶ Sets and compiles a new optimizer for the Translation_Model. The configuration is read from Translation_Model.params. :return: None
-
setParams
(params)¶ Set self.params as params. :param params: :return:
-
nmt_keras.model_zoo.
getPositionalEncodingWeights
(input_dim, output_dim, name='', verbose=True)¶ Obtains fixed sinusoidal embeddings for obtaining the positional encoding.
Parameters: - input_dim (int) – Input dimension of the embeddings (i.e. vocabulary size).
- output_dim (int) – Embeddings dimension.
- name (str) – Name of the layer
- verbose (int) – Be verbose
Returns: A list with sinusoidal embeddings.
training¶
-
nmt_keras.training.
train_model
(params, load_dataset=None)¶ Training function.
Sets the training parameters from params.
Build or loads the model and launches the training.
Parameters: - params (dict) – Dictionary of network hyperparameters.
- load_dataset (str) – Load dataset from file or build it from the parameters.
Returns: None
apply_model¶
-
nmt_keras.apply_model.
sample_ensemble
(args, params)¶ Use several translation models for obtaining predictions from a source text file.
Parameters: - args (argparse.Namespace) –
Arguments given to the method:
- dataset: Dataset instance with data.
- text: Text file with source sentences.
- splits: Splits to sample. Should be already included in the dataset object.
- dest: Output file to save scores.
- weights: Weight given to each model in the ensemble. You should provide the same number of weights than models. By default, it applies the same weight to each model (1/N).
- n_best: Write n-best list (n = beam size).
- config: Config .pkl for loading the model configuration. If not specified, hyperparameters are read from config.py.
- models: Path to the models.
- verbose: Be verbose or not.
- params – parameters of the translation model.
- args (argparse.Namespace) –
-
nmt_keras.apply_model.
score_corpus
(args, params)¶ Use one or several translation models for scoring source–target pairs-
Parameters: - args (argparse.Namespace) –
Arguments given to the method:
- dataset: Dataset instance with data.
- source: Text file with source sentences.
- target: Text file with target sentences.
- splits: Splits to sample. Should be already included in the dataset object.
- dest: Output file to save scores.
- weights: Weight given to each model in the ensemble. You should provide the same number of weights than models. By default, it applies the same weight to each model (1/N).
- verbose: Be verbose or not.
- config: Config .pkl for loading the model configuration. If not specified, hyperparameters are read from config.py.
- models: Path to the models.
- params (dict) – parameters of the translation model.
- args (argparse.Namespace) –
build_callbacks¶
-
nmt_keras.build_callbacks.
buildCallbacks
(params, model, dataset)¶ - Builds the selected set of callbacks run during the training of the model:
- EvalPerformance: Evaluates the model in the validation set given a number of epochs/updates.
- SampleEachNUpdates: Shows several translation samples during training.
Parameters: - params (dict) – Dictionary of network hyperparameters.
- model (Model_Wrapper) – Model instance on which to apply the callback.
- dataset (Dataset) – Dataset instance on which to apply the callback.
Returns: list of callbacks to pass to the Keras’ training.