alex.ml package

Submodules

alex.ml.exceptions module

exception alex.ml.exceptions.FFNNException[source]

Bases: alex.AlexException

exception alex.ml.exceptions.NBListException[source]

Bases: alex.AlexException

alex.ml.features module

This module contains generic code for working with feature vectors (or, in general, collections of features).

class alex.ml.features.Abstracted[source]

Bases: object

all_instantiations(do_abstract=False)[source]
get_concrete()[source]
get_generic()[source]
instantiate(type_, value, do_abstract=False)[source]
Example: Let self represent
da1(a1=T1:v1)&da2(a2=T2:v2)&da3(a3=T1:v3).

Calling self.instantiate(“T1”, “v1”) results in

da1(a1=T1)&da2(a2=v2)&da3(a3=v3) ..if do_abstract == False

da1(a1=T1)&da2(a2=v2)&da3(a3=T1_other) ..if do_abstract == True

Calling self.instantiate(“T1”, “x1”) results in

da1(a1=x1)&da2(a2=v2)&da3(a3=v3) ..if do_abstract == False

da1(a1=T1_other)&da2(a2=v2)&da3(a3=T1_other)
..if do_abstract == True.
insts_for_type(type_)[source]
insts_for_typeval(type_, value)[source]
iter_instantiations()[source]
iter_triples()[source]
iter_typeval()[source]

Iterates the abstracted items in self, yielding combined representations of the type and value of each such token. An abstract method of this class.

join_typeval(type_, val)[source]
classmethod make_other(type_)[source]
other_val = '[OTHER]'
replace_typeval(combined, replacement)[source]
splitter = '='
to_other()[source]
class alex.ml.features.AbstractedTuple2[source]

Bases: alex.ml.features.AbstractedFeature

class alex.ml.features.Features(*args, **kwargs)[source]

Bases: object

A mostly abstract class representing features of an object.

Attributes:
features: mapping of the features to their values set: set of the features
classmethod do_with_abstract(feature, meth, *args, **kwargs)[source]
get_feature_coords_vals(feature_idxs)[source]

Builds the feature vector based on the provided mapping of features onto their indices. Returns the vector as a two lists, one of feature coordinates, one of feature values.

Arguments:
feature_idxs: a mapping { feature : feature index }
get_feature_vector(feature_idxs)[source]

Builds the feature vector based on the provided mapping of features onto their indices.

Arguments:
feature_idxs: a mapping { feature : feature index }
classmethod iter_abstract(feature)[source]
iter_instantiations()[source]
iteritems()[source]

Iterates tuples of this object’s features and their values.

classmethod join(feature_sets, distinguish=True)[source]

Joins a number of sets of features, keeping them distinct.

Arguments:
distinguish – whether to treat the feature sets as of different
types (distinguish=True) or just merge features from them by adding their values (distinguish=False). Default is True.

Returns a new instance of JoinedFeatures.

prune(to_remove=None, min_val=None)[source]

Discards specified features.

Arguments:

to_remove – collection of features to be removed min_val – threshold for feature values in order for them to be

retained (those not meeting the threshold are pruned)
class alex.ml.features.JoinedFeatures(feature_sets)[source]

Bases: alex.ml.features.Features

JoinedFeatures are indexed by tuples (feature_sets_index, feature) where feature_sets_index selects the required set of features. Sets of features are numbered with the same indices as they had in the list used to initialise JoinedFeatures.

Attributes:

features: mapping { (feature_set_index, feature) : value of feature } set: set of the (feature_set_index, feature) tuples generic: mapping { (feature_set_index, abstracted_feature) :

generic_feature }
instantiable: mapping { feature : generic part of feature } for
features from self.features.keys() that are abstracted
iter_instantiations()[source]
class alex.ml.features.ReplaceableTuple2[source]

Bases: tuple

iter_combined()[source]
replace(old, new)[source]
to_other()[source]
alex.ml.features.make_abstract(replaceable, iter_meth=None, replace_meth=None, splitter='=', make_other=None)[source]
alex.ml.features.make_abstracted_tuple(abstr_idxs)[source]

Example usage:

AbTuple2 = make_abstract_tuple((2,)) ab_feat = AbTuple2((dai.dat, dai.name,

‘=’.join(dai.name.upper(), dai.value)))

# ... ab_feat.instantiate(‘food’, ‘chinese’) ab_feat.instantiate(‘food’, ‘indian’)

alex.ml.ffnn module

class alex.ml.ffnn.FFNN[source]

Bases: object

Implements simple feed-forward neural network with:

– input layer - activation function linear – hidden layers - activation function tanh – output layer - activation function softmax

add_layer(w, b)[source]

Add next layer into the network.

Parameters:
  • w – next layer weights
  • b – next layer biases
Returns:

none

load(file_name)[source]

Loads saved NN.

Parameters:file_name – file name of the saved NN
Returns:None
predict(input)[source]

Returns the output of the last layer.

As it is output of a layer with softmax activation function, the output is a vector of probabilities of
the classes being predicted.
Parameters:input – input vector for the first NN layer.
Returns:return the output of the last activation layer
save(file_name)[source]

Saves the NN into a file.

Parameters:file_name – name of the file where the NN will be saved
Returns:None
set_input_norm(m, std)[source]
sigmoid(y)[source]
softmax(y)[source]
tanh(y)[source]

alex.ml.hypothesis module

This module collects classes representing the uncertainty about the actual value of a base type instance.

class alex.ml.hypothesis.ConfusionNetwork[source]

Bases: alex.ml.hypothesis.Hypothesis

Confusion network. In this representation, each fact breaks down into a sequence of elementary acts.

add(probability, fact)[source]

Append a fact to the confusion network.

add_merge(p, fact, combine=u'max')[source]

Add a fact and if it exists merge it according to the given combine strategy.

extend(conf_net)[source]
classmethod from_fact(fact)[source]

Constructs a deterministic confusion network that asserts the given `fact’. Note that `fact’ has to be an iterable of elementary acts.

get_prob(fact)[source]

Get the probability of the fact.

merge(conf_net, combine=u'max')[source]

Merges facts in the current and the given confusion networks.

Arguments:
combine – can be one of {‘new’, ‘max’, ‘add’, ‘arit’, ‘harm’}, and
determines how two probabilities should be merged (default: ‘max’)

XXX As of now, we know that different values for the same slot are contradictory (and in general, the set of contradicting attr-value pairs could be larger). We should therefore consider them alternatives to each other.

normalise()[source]

Makes sure that all probabilities add up to one. They should implicitly sum to one: p + (1-p) == 1.0

prune(prune_prob=0.005)[source]

Prune all low probability dialogue act items.

remove(fact_to_remove)[source]
sort(reverse=True)[source]
update_prob(probability, fact)[source]

Update the probability of a fact.

exception alex.ml.hypothesis.ConfusionNetworkException[source]

Bases: exceptions.Exception

class alex.ml.hypothesis.Hypothesis[source]

Bases: object

This is the base class for all forms of probabilistic hypotheses representations.

classmethod from_fact(fact)[source]

Constructs a deterministic hypothesis that asserts the given `fact’.

class alex.ml.hypothesis.NBList[source]

Bases: alex.ml.hypothesis.Hypothesis

This class represents the uncertainty using an n-best list.

When updating an N-best list, one should do the following.

  1. add utterances or parse a confusion network
  2. merge and normalise, in either order
add(probability, fact)[source]

Finds the last hypothesis with a lower probability and inserts the new item before that one. Optimised for adding objects from the highest probability ones to the lowest probability ones.

add_other(other)[source]

The N-best list is extended to include the other object to represent those object values that are not enumerated in the list.

Returns self.

classmethod from_fact(fact)[source]
get_best()[source]

Returns the most probable value of the object.

merge()[source]

Adds up probabilities for the same hypotheses. Returns self.

normalise()[source]

Scales the list to sum to one.

alex.ml.logarithmetic module

alex.ml.logarithmetic.add(a, b)[source]

Computes pairwise addition of two vectors in the log domain.

This is equivalent to [a1+b1, a2+b2, ...] in the linear domain..

alex.ml.logarithmetic.devide(a, b)[source]

Computes pairwise division between vectors a and b in the log domain.

This is equivalent to [a1/b1, a2/b2, ...] in the linear domain.

alex.ml.logarithmetic.dot(a, b)[source]

Computes dot product in the log domain.

This is equivalent to a1*b1+a2*b2+... in the linear domain.

alex.ml.logarithmetic.linear_to_log(a)[source]

Converts a vector from the linear domain to the log domain.

alex.ml.logarithmetic.log_to_linear(a)[source]

Converts a vector from the log domain to the linear domain.

alex.ml.logarithmetic.multiply(a, b)[source]

Computes pairwise multiplication between vectors a and b in the log domain.

This is equivalent to [a1*b1, a2*b2, ...] in the linear domain.

alex.ml.logarithmetic.normalise(a)[source]

normalises the input probability vector to sum to one in the log domain.

This is equivalent to a/sum(a) in the linear domain.

alex.ml.logarithmetic.sub(a, b)[source]

Computes pairwise subtraction of two vectors in the log domain.

This is equivalent to [a1-b1, a2-b2, ...] in the linear domain.

alex.ml.logarithmetic.sum(a, axis=None)[source]

alex.ml.test_hypothesis module

class alex.ml.test_hypothesis.TestConfusionNetwork(methodName='runTest')[source]

Bases: unittest.case.TestCase

test_iter()[source]
test_remove()[source]

alex.ml.tffnn module

Module contents