def __init__(self, nmt_specs, decoder_args):
     """Set up the NMT model used by the decoder.
     
     Args:
         nmt_specs (list):  List of tuples of which the first element
                            is the path to the NMT model (.npz) file
                            and the second is the NMT configuration
         decoder_args (object): Decoder configuration passed through
                                from configuration API.
     """
     super(BlocksNMTEnsembleVanillaDecoder, self).__init__(decoder_args)
     self.n_networks = len(nmt_specs)
     if self.n_networks < 2:
         logging.fatal("The NMT ensemble vanilla decoder needs at "
                       "least two NMT systems")
     global_config = nmt_specs[0][1]
     self.src_vocab_size = global_config['src_vocab_size']
     self.beam_size = global_config['beam_size']
     self.src_sparse_feat_map = global_config['src_sparse_feat_map'] \
         if global_config['src_sparse_feat_map'] else FlatSparseFeatMap()
     if global_config['trg_sparse_feat_map']:
         logging.fatal("Using sparse feature maps on the target size is "
                       "currently not supported by the ensemble vanilla "
                       "decoder")
     self.trg_sparse_feat_map = FlatSparseFeatMap()
     self.set_up_decoder(nmt_specs)
     self.src_eos = self.src_sparse_feat_map.word2dense(utils.EOS_ID)
예제 #2
0
 def set_up_predictor(self, nmt_model_path):
     """Initializes the predictor with the given NMT model. Code 
     following ``blocks.machine_translation.main``. 
     """
     self.src_vocab_size = self.config['src_vocab_size']
     self.trgt_vocab_size = self.config['trg_vocab_size']
     self.nmt_model = NMTModel(self.config)
     self.nmt_model.set_up()
     loader = LoadNMTUtils(nmt_model_path,
                           self.config['saveto'],
                           self.nmt_model.search_model)
     loader.load_weights()
         
     self.best_models = []
     self.val_bleu_curve = []
     self.src_sparse_feat_map = self.config['src_sparse_feat_map'] \
             if self.config['src_sparse_feat_map'] else FlatSparseFeatMap()
     if self.config['trg_sparse_feat_map']:
         self.trg_sparse_feat_map = self.config['trg_sparse_feat_map']
         self.search_algorithm = MyopticSparseSearch(
                             samples=self.nmt_model.samples,
                             trg_sparse_feat_map=self.trg_sparse_feat_map)
     else:
         self.trg_sparse_feat_map = FlatSparseFeatMap()
         self.search_algorithm = MyopticSearch(samples=self.nmt_model.samples)
     self.search_algorithm.compile()
예제 #3
0
 def __init__(self,
              src_file,
              trgt_file,
              src_vocab_size,
              trgt_vocab_size,
              preprocess=None,
              src_sparse_feat_map=None,
              trg_sparse_feat_map=None):
     """Constructor like for ``TextFile``"""
     global _current_src_sparse_feat_map, _current_trg_sparse_feat_map
     super(ParallelTextFile, self).__init__()
     self.src_file = src_file
     self.trgt_file = trgt_file
     self.src_vocab_size = src_vocab_size
     self.trgt_vocab_size = trgt_vocab_size
     self.preprocess = preprocess
     with open(self.src_file) as f:
         self.src_sentences = f.readlines()
     with open(self.trgt_file) as f:
         self.trgt_sentences = f.readlines()
     self.num_examples = len(self.src_sentences)
     if self.num_examples != len(self.trgt_sentences):
         raise ValueError
     self.src_sparse_feat_map = src_sparse_feat_map if src_sparse_feat_map \
                                                    else FlatSparseFeatMap()
     self.trg_sparse_feat_map = trg_sparse_feat_map if trg_sparse_feat_map \
                                                    else FlatSparseFeatMap()
     # Pickle hacks
     _current_src_sparse_feat_map = self.src_sparse_feat_map
     _current_trg_sparse_feat_map = self.trg_sparse_feat_map
예제 #4
0
 def __init__(self,
              parallel_sources,
              src_vocab_size=30000,
              trg_vocab_size=30000,
              src_sparse_feat_map=None,
              trg_sparse_feat_map=None):
     """Creates a new ``Dataset`` which allows switching between 
     multiple parallel sources. The first source is activated per
     default
     
     Args:
         parallel_sources (list): List of ``ParallelSource`` objects
         src_vocab_size (int): Size of source vocabulary
         trg_vocab_size (int): Size of target vocabulary
         src_sparse_feat_map (SparseFeatMap): Map between words and
                                              their features if you 
                                              use sparse vectors.
         trg_sparse_feat_map (SparseFeatMap): Map between words and
                                              their features if you 
                                              use sparse vectors.
     
     Raises:
         IndexError. If the list is empty
     """
     super(ParallelSourceSwitchDataset, self).__init__()
     global _current_parallel_sources
     global _current_src_sparse_feat_map, _current_trg_sparse_feat_map
     if not parallel_sources:
         raise ValueError
     self.parallel_sources = parallel_sources
     self.src_vocab_size = src_vocab_size
     self.trg_vocab_size = trg_vocab_size
     self.active_idx = 0
     self.src_sparse_feat_map = src_sparse_feat_map if src_sparse_feat_map \
                                                    else FlatSparseFeatMap()
     self.trg_sparse_feat_map = trg_sparse_feat_map if trg_sparse_feat_map \
                                                    else FlatSparseFeatMap()
     # pickle hacks
     _current_parallel_sources = parallel_sources
     _current_src_sparse_feat_map = self.src_sparse_feat_map
     _current_trg_sparse_feat_map = self.trg_sparse_feat_map
 def set_up_decoder(self, nmt_model_path):
     """This method uses the NMT configuration in ``self.config`` to
     initialize the NMT model. This method basically corresponds to 
     ``blocks.machine_translation.main``.
     
     Args:
         nmt_model_path (string):  Path to the NMT model file (.npz)
     """
     self.nmt_model = NMTModel(self.config)
     self.nmt_model.set_up()
     loader = LoadNMTUtils(nmt_model_path, self.config['saveto'],
                           self.nmt_model.search_model)
     loader.load_weights()
     self.src_sparse_feat_map = self.config['src_sparse_feat_map'] \
             if self.config['src_sparse_feat_map'] else FlatSparseFeatMap()
     if self.config['trg_sparse_feat_map']:
         self.trg_sparse_feat_map = self.config['trg_sparse_feat_map']
         self.beam_search = SparseBeamSearch(
             samples=self.nmt_model.samples,
             trg_sparse_feat_map=self.trg_sparse_feat_map)
     else:
         self.trg_sparse_feat_map = FlatSparseFeatMap()
         self.beam_search = BeamSearch(samples=self.nmt_model.samples)
예제 #6
0
class BlocksUnboundedNMTPredictor(BlocksNMTPredictor,
                                  UnboundedVocabularyPredictor):
    """This is a version of the NMT predictor which assumes an 
    unbounded vocabulary. Therefore, this predictor can only be used 
    when other predictors (like fst) define the words to score. Using
    this predictor is mandatory when a target sparse feature map is
    provided.
    """
    
    def __init__(self, nmt_model_path, gnmt_beta, config):
        """Creates a new NMT predictor with unbounded vocabulary.
        
        Args:
            nmt_model_path (string):  Path to the NMT model file (.npz)
            config (dict): NMT configuration, 
        """
        super(BlocksUnboundedNMTPredictor, self).__init__(nmt_model_path, 
                                                          gnmt_beta,
                                                          False, 
                                                          config)
    
    def set_up_predictor(self, nmt_model_path):
        """Initializes the predictor with the given NMT model. Code 
        following ``blocks.machine_translation.main``. 
        """
        self.src_vocab_size = self.config['src_vocab_size']
        self.trgt_vocab_size = self.config['trg_vocab_size']
        self.nmt_model = NMTModel(self.config)
        self.nmt_model.set_up()
        loader = LoadNMTUtils(nmt_model_path,
                              self.config['saveto'],
                              self.nmt_model.search_model)
        loader.load_weights()
            
        self.best_models = []
        self.val_bleu_curve = []
        self.src_sparse_feat_map = self.config['src_sparse_feat_map'] \
                if self.config['src_sparse_feat_map'] else FlatSparseFeatMap()
        if self.config['trg_sparse_feat_map']:
            self.trg_sparse_feat_map = self.config['trg_sparse_feat_map']
            self.search_algorithm = MyopticSparseSearch(
                                samples=self.nmt_model.samples,
                                trg_sparse_feat_map=self.trg_sparse_feat_map)
        else:
            self.trg_sparse_feat_map = FlatSparseFeatMap()
            self.search_algorithm = MyopticSearch(samples=self.nmt_model.samples)
        self.search_algorithm.compile()

    def predict_next(self, words):
        """Uses cache or runs the decoder network to get the 
        distribution over the next target words.
        
        Returns:
            np array. Full distribution over the entire NMT vocabulary
            for the next target token.
        """
        logprobs = self.search_algorithm.compute_logprobs(self.contexts,
                                                          self.states)
        if self.trg_sparse_feat_map.dim > 1:
            return {w: -sparse.dense_euclidean2(
                                        logprobs[0],
                                        self.trg_sparse_feat_map.word2dense(w))
                        for w in words}
        else:
            # logprobs are negative log probs, i.e. greater than 0
            posterior = np.multiply(logprobs[0], -1.0)
            return {w: posterior[w] for w in words}

    def get_unk_probability(self, posterior):
        """Returns negative inf as this is a unbounded predictor. """
        return NEG_INF
    
    def consume(self, word):
        """Feeds back ``word`` to the decoder network. This includes 
        embedding of ``word``, running the attention network and update
        the recurrent decoder layer.
        """
        if word >= self.trgt_vocab_size:
            word = utils.UNK_ID
        self.consumed.append(word)
        self.states.update(self.search_algorithm.compute_next_states(
                                self.contexts,
                                self.states,
                                [self.trg_sparse_feat_map.word2dense(word)]))

    def is_equal(self, state1, state2):
        """Returns true if the history is the same """
        _,consumed1 = state1
        _,consumed2 = state2
        return consumed1 == consumed2
예제 #7
0
    def __init__(self,
                 source_sentence,
                 samples,
                 model,
                 data_stream,
                 config,
                 n_best=1,
                 track_n_models=1,
                 normalize=True,
                 store_full_main_loop=False,
                 **kwargs):
        """Creates a new extension which adds model selection based on
            the accuracy score to the training main loop.
            
        Args:
            source_sentence (Variable): Input variable to the sampling
            computation graph
            samples (Variable): Samples variable of the CG
            model (NMTModel): See the model module
            data_stream (DataStream): Data stream to the development
            set
            config (dict): NMT configuration
            n_best (int): beam size
            track_n_models (int): Number of n-best models for which to
            create checkpoints.
            normalize (boolean): Enables length normalization
            store_full_main_loop (boolean): Stores the iteration state
            in the old style of
            Blocks 0.1. Not recommended
            """
        super(AccValidator, self).__init__(**kwargs)
        self.store_full_main_loop = store_full_main_loop
        self.source_sentence = source_sentence
        self.samples = samples
        self.model = model
        self.data_stream = data_stream
        self.config = config
        self.n_best = n_best
        self.track_n_models = track_n_models
        self.normalize = normalize
        self.best_models = []
        self.val_bleu_curve = []
        
        self.src_sparse_feat_map = config['src_sparse_feat_map'] if config['src_sparse_feat_map'] \
            else FlatSparseFeatMap()
        if config['trg_sparse_feat_map']:
            self.trg_sparse_feat_map = config['trg_sparse_feat_map']
            self.beam_search = SparseBeamSearch(
                                                samples=samples,
                                                trg_sparse_feat_map=self.trg_sparse_feat_map)
        else:
            self.trg_sparse_feat_map = FlatSparseFeatMap()
            self.beam_search = BeamSearch(samples=samples)
        
        # Create saving directory if it does not exist
        if not os.path.exists(self.config['saveto']):
            os.makedirs(self.config['saveto'])
        
        if self.config['reload']:
            try:
                bleu_score = numpy.load(os.path.join(self.config['saveto'],
                                                     'val_bleu_scores.npz'))
                self.val_bleu_curve = bleu_score['bleu_scores'].tolist()
                # Track n best previous bleu scores
                for i, bleu in enumerate(
                    sorted(self.val_bleu_curve, reverse=True)):
                        if i < self.track_n_models:
                            self.best_models.append(ModelInfo(bleu))
                logging.info("BleuScores Reloaded")
            except:
                logging.info("BleuScores not Found")

        self.verbose = self.config.get('val_set_out', None)
        utils.load_trg_wmap(self.config['trg_wmap'])
        self.trg_wmap = utils.trg_wmap