Package jazzparser :: Package taggers :: Package ngram_multi :: Module model :: Class MultiChordNgramModel
[hide private]
[frames] | no frames]

Class MultiChordNgramModel

source code

                       object --+    
                                |    
utils.nltk.ngram.model.NgramModel --+
                                    |
                                   MultiChordNgramModel

An ngram model that takes multiple chords (weighted by probability) as input to its decoding. It is trained on labeled data.

State labels are pairs (root,schema), each representing a lexical schema instantiated on a specific root, i.e. a lexical category. Emissions are pairs (root,label), representing chords. The parameters are tied so that chords (r,l) can only be emitted from states (r,s).

The component distributions that are actually stored are:

Instance Methods [hide private]
 
__init__(self, order, root_transition_counts, schema_transition_counts, emission_counts, estimator, backoff_model, schemata, chord_vocab, history='')
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
clear_cache(self)
Initializes or empties probability distribution caches.
source code
 
add_history(self, string)
Adds a line to the end of this model's history string.
source code
 
_get_transition_backoff_scaler(self, context)
Returns the amount to scale the backed off probabilities by when backing off to an order n-1 model in the given context.
source code
 
schema_transition_log_probability(self, *states)
Just the schema part of the transition distribution.
source code
 
schema_transition_log_probability_schemata(self, schema, *previous_schemata)
Just the schema part of the transition distribution.
source code
 
transition_log_probability(self, state, *previous_states)
Gives the probability P(label_i | label_(i-1), ..., label_(i-n)), where the previous labels are given in the sequence label_context.
source code
 
emission_log_probability(self, emission, state)
Gives the probability P(emission | label).
source code
 
get_schema_transition_matrix(self, transpose=False)
Transition matrix just for the schema distribution.
source code
 
get_root_transition_matrix(self)
Transition matrix just for the root change distribution.
source code
 
get_schema_emission_matrix(self, sequence)
Emission matrix for states decomposed into schema and root.
source code
 
normal_forward_probabilities(self, sequence, root_schema=False)
Returns: if root_schema=True, (S+1)-dimensional Numpy array, where S is the number of schemata.
source code
 
normal_backward_probabilities(self, sequence, root_schema=False)
Returns: if root_schema=True, (S+1)-dimensional Numpy array, where S is the number of schemata.
source code
 
gamma_probabilities(self, sequence, dictionary=False, forward=None, backward=None)
State-occupation probabilities.
source code
 
compute_gamma(self, *args, **kwargs)
Alias for backward compatibility.
source code
 
to_picklable_dict(self)
Produces a picklable representation of model as a dict.
source code

Inherited from utils.nltk.ngram.model.NgramModel: __repr__, backward_log_probabilities, backward_probabilities, decode_forward, decode_gamma, emission_probability, forward_backward_log_probabilities, forward_backward_probabilities, forward_log_probabilities, forward_probabilities, generalized_viterbi, generate, get_all_ngrams, get_backoff_models, get_emission_matrix, get_transition_matrix, labeled_sequence_log_probability, normal_forward_backward_probabilities, precompute, transition_log_probability_debug, transition_probability, transition_probability_debug, viterbi_decode, viterbi_selector_probabilities

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

Class Methods [hide private]
 
from_picklable_dict(cls, data)
Reproduces an model that was converted to a picklable form using to_picklable_dict.
source code
Static Methods [hide private]
 
train(data, schemata, chord_types, estimator, cutoff=0, logger=None, chord_map=None, order=2, backoff_orders=0, backoff_kwargs={})
Initializes and trains an HMM in a supervised fashion using the given training data.
source code
Properties [hide private]

Inherited from utils.nltk.ngram.model.NgramModel: model_type

Inherited from object: __class__

Method Details [hide private]

__init__(self, order, root_transition_counts, schema_transition_counts, emission_counts, estimator, backoff_model, schemata, chord_vocab, history='')
(Constructor)

source code 

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

Overrides: object.__init__
(inherited documentation)

clear_cache(self)

source code 

Initializes or empties probability distribution caches.

Make sure to call this if you change or update the distributions.

No caches used thus far, so this does nothing for now, except call the super method.

Overrides: utils.nltk.ngram.model.NgramModel.clear_cache

train(data, schemata, chord_types, estimator, cutoff=0, logger=None, chord_map=None, order=2, backoff_orders=0, backoff_kwargs={})
Static Method

source code 

Initializes and trains an HMM in a supervised fashion using the given training data. Training data should be chord sequence data (input type bulk-db or bulk-db-annotated).

Overrides: utils.nltk.ngram.model.NgramModel.train

_get_transition_backoff_scaler(self, context)

source code 

Returns the amount to scale the backed off probabilities by when backing off to an order n-1 model in the given context. This is presented as alpha in Jurafsky and Martin.

Returned as a base 2 log.

A more efficient way to do this would be to supply a function of the context specific to the discounting technique. In this case it wouldn't be necessary to sum the discounted mass each time.

Overrides: utils.nltk.ngram.model.NgramModel._get_transition_backoff_scaler
(inherited documentation)

schema_transition_log_probability_schemata(self, schema, *previous_schemata)

source code 

Just the schema part of the transition distribution. This method takes just schemata as args, instead of whole states.

transition_log_probability(self, state, *previous_states)

source code 

Gives the probability P(label_i | label_(i-1), ..., label_(i-n)), where the previous labels are given in the sequence label_context. The context should be in reverse order, i.e. with the most recent label at the start.

Note that this is the probability of a label given the previous n-1 labels, which is the same as the probability of the n-gram [label_i, ..., label_(i-n+1)] given the ngram [label_(i-1), ..., label_(i-n)], since all but the last element of the ngram overlaps with the condition, so has probability 1.

Caches all computed transition probabilities. This is particularly important for backoff models. Many n-grams will back off to the same (n-1)-gram and we don't want to recompute the transition probability for that each time.

Overrides: utils.nltk.ngram.model.NgramModel.transition_log_probability
(inherited documentation)

emission_log_probability(self, emission, state)

source code 

Gives the probability P(emission | label). Returned as a base 2 log.

The emission should be a pair of (root,label), together defining a chord.

There's a special case of this. If the emission is a list, it's assumed to be a distribution over emissions. The list should contain (prob,em) pairs, where em is an emission, such as is normally passed into this function, and prob is the weight to give to this possible emission. The probabilities of the possible emissions are summed up, weighted by the prob values.

Overrides: utils.nltk.ngram.model.NgramModel.emission_log_probability

get_schema_emission_matrix(self, sequence)

source code 

Emission matrix for states decomposed into schema and root. Matrix has dimensions (time, root, schema).

normal_forward_probabilities(self, sequence, root_schema=False)

source code 

Return the forward probability matrix as a Numpy array. This is equivalent to forward_probabilities, but much faster. It doesn't need logs because it's normalizing at each timestep.

Parameters:
  • seq_prob - return the log probability of the whole sequence as well as the array (tuple of (array,logprob)).
Returns:
if root_schema=True, (S+1)-dimensional Numpy array, where S is the number of schemata. The dimensions are (time, last root, last schema, previous schema, etc). Otherwise, the array has the same definition as the superclass.
Overrides: utils.nltk.ngram.model.NgramModel.normal_forward_probabilities

See Also: jazzparser.utils.nltk.ngram.model.NgramModel.normal_forward_probabilities

Note: tested against superclass method. They're giving the same results to a high precision (differences ~1e-20)

normal_backward_probabilities(self, sequence, root_schema=False)

source code 

Return the backward probability matrices a Numpy array. This is faster than backward_log_probabilities because it uses Numpy arrays with non-log probabilities and normalizes each timestep.

Returns:
if root_schema=True, (S+1)-dimensional Numpy array, where S is the number of schemata. The dimensions are (time, last root, last schema, previous schema, etc). Otherwise, the array has the same definition as the superclass.
Overrides: utils.nltk.ngram.model.NgramModel.normal_backward_probabilities

See Also: jazzparser.utils.nltk.ngram.model.NgramModel.normal_backward_probabilities

gamma_probabilities(self, sequence, dictionary=False, forward=None, backward=None)

source code 

State-occupation probabilities.

Overridden so we don't need to construct the full ngram matrix, which can be huge with trigrams to the point of running out of memory.

Parameters:
  • dictionary (bool) - return a list of label dictionaries instead of a numpy matrix
Overrides: utils.nltk.ngram.model.NgramModel.gamma_probabilities

compute_gamma(self, *args, **kwargs)

source code 

Alias for backward compatibility. Use gamma_probabilities

to_picklable_dict(self)

source code 

Produces a picklable representation of model as a dict.

Overrides: utils.nltk.ngram.model.NgramModel.to_picklable_dict

from_picklable_dict(cls, data)
Class Method

source code 

Reproduces an model that was converted to a picklable form using to_picklable_dict.

Overrides: utils.nltk.ngram.model.NgramModel.from_picklable_dict