Package jazzparser :: Package utils :: Package nltk :: Package ngram :: Module baumwelch :: Class BaumWelchTrainer
[hide private]
[frames] | no frames]

Class BaumWelchTrainer

source code

object --+
         |
        BaumWelchTrainer
Known Subclasses:

Class with methods to retrain an HMM using the Baum-Welch EM algorithm.

Note that although the default implementation is for a plain jazzparser.utils.nltk.ngram.NgramModel, Baum-Welch training only makes sense if the model is an HMM. It will therefore complain if the order is not 2 and if there's a backoff model.

Module options must be processed externally. This allows them to be combined with other options as appropriate. The options defined here are a standard set of options for generic training and should be processed before the trainer is instantiated.

This is designed as a generic implementation of the algorithm. To use it for a special kind of model (e.g. one with a non-standard transition distribution), you need to override certain methods to make them appropriate to the model:

The generic version of the trainer can be used to train a DictionaryHmmModel. Subclasses are used to train other model types.

Instance Methods [hide private]
 
__init__(self, model, options={})
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
record_history(self, line)
Stores a line in the history of the model or wherever else it is appropriate to keep a record of training steps.
source code
 
create_mutable_model(self, model)
Creates a mutable version of the given model.
source code
 
get_empty_arrays(self)
Creates empty arrays to hold the accumulated probabilities during training.
source code
 
get_array_indices(self)
Returns a tuple of the dicts that map labels, emissions, etc to the indices of arrays to which they correspond.
source code
 
sequence_updates_callback(self, result)
Callback for the sequence_updates processes that takes the updates from a single sequence and adds them onto the global update accumulators.
source code
 
update_model(self, arrays, array_ids)
Replaces the distributions of the saved model with the probabilities taken from the arrays of updates.
source code
 
save(self)
Saves the model in self.model to disk.
source code
 
train(self, emissions, logger=None)
Performs unsupervised training using Baum-Welch EM.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Methods [hide private]
 
process_option_dict(cls, options)
Verifies and processes the training option values.
source code
Static Methods [hide private]
 
sequence_updates(sequence, last_model, empty_arrays, array_ids, update_initial=True)
This should be overridden by subclasses, but not by defining a static method on the class, since the function must be picklable.
source code
Class Variables [hide private]
  OPTIONS = [ModuleOption('max_iterations', filter= int, help_te...
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, model, options={})
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__
(inherited documentation)

process_option_dict(cls, options)
Class Method

source code 

Verifies and processes the training option values. Returns the processed dict.

record_history(self, line)

source code 

Stores a line in the history of the model or wherever else it is appropriate to keep a record of training steps.

Default implementation does nothing, but subclasses may want to store this information.

sequence_updates(sequence, last_model, empty_arrays, array_ids, update_initial=True)
Static Method

source code 

This should be overridden by subclasses, but not by defining a static method on the class, since the function must be picklable. For this, it needs to be a top-level function. Then you can set the sequence_updates attribute to point to it (using staticmethod), as we have done in the default implementation.

Parameters:
  • update_initial (bool) - usually you want to update all distributions, including the initial state distribution. If update_initial=False, the initial state distribution updates won't be made for this sequence. We want this when the sequence is actually a non-initial fragment of a longer sequence

create_mutable_model(self, model)

source code 

Creates a mutable version of the given model. This mutable version will be the model that receives updates during training, as defined by update_model.

get_empty_arrays(self)

source code 

Creates empty arrays to hold the accumulated probabilities during training. The sizes will depend on self.model.

get_array_indices(self)

source code 

Returns a tuple of the dicts that map labels, emissions, etc to the indices of arrays to which they correspond. These will need to be different for non-standard models.

sequence_updates_callback(self, result)

source code 

Callback for the sequence_updates processes that takes the updates from a single sequence and adds them onto the global update accumulators.

The accumulators are stored as self.global_arrays.

update_model(self, arrays, array_ids)

source code 

Replaces the distributions of the saved model with the probabilities taken from the arrays of updates. self.model is expected to be made up of mutable distributions when this is called.

save(self)

source code 

Saves the model in self.model to disk. This may be called at the end of each iteration and will be called at the end of the whole training process.

By default, does nothing. You don't have to put something in here, but you'll need to override this if you want the model to be saved during training before it gets return at the end.

train(self, emissions, logger=None)

source code 

Performs unsupervised training using Baum-Welch EM.

This is performed as a retraining step on a model that has already been initialized.

This is based on the training procedure in NLTK for HMMs: nltk.tag.hmm.HiddenMarkovModelTrainer.train_unsupervised.

Parameters:
  • emissions (list of lists of emissions) - training data. Each element is a list of emissions representing a sequence in the training data. Each emission is an emission like those used for emission_log_probability on the model
  • logger (logging.Logger) - a logger to send progress logging to

Class Variable Details [hide private]

OPTIONS

Value:
[ModuleOption('max_iterations', filter= int, help_text= "Number of tra\
ining iterations to give up after " "if we don't reach convergence bef\
ore.", usage= "max_iterations=N, where N is an integer", default= 100)\
, ModuleOption('convergence_logprob', filter= float, help_text= "Diffe\
rence in overall log probability of the " "training data made by one i\
teration after which we " "consider the training to have converged.", \
usage= "convergence_logprob=X, where X is a small floating " "point nu\
mber (e.g. 1e-3)", default= 1e-3), ModuleOption('split', filter= int, \
...