theanets.recurrent.Classifier

class theanets.recurrent.Classifier(layers, loss='xe', weighted=False)

A classifier computes a distribution over labels, given an input.

Notes

Classifier models default to a cross-entropy loss. To use a different loss, provide a non-default argument for the loss keyword argument when constructing your model.

Examples

To create a recurrent classification model, just create a new class instance. Often you’ll provide the layer configuration at this time:

>>> model = theanets.recurrent.Classifier([10, (20, 'rnn'), 50])

See Creating a Model for more information.

Data

Training data for a recurrent classification model takes the form of two three-dimensional arrays.

The first array provides the input data for the model. Its shape is (num-examples, num-time-steps, num-variables): the first axis enumerates data points in a batch, the second enumerates time steps, and the third enumerates the input variables in the model.

The second array provides the target class labels for the inputs. Its shape is (num-examples, num-time-steps), and each integer value in the array gives the class label for the corresponding input example and time step.

For instance, to create a training dataset containing 1000 examples, each with 100 time steps:

>>> inputs = np.random.randn(1000, 100, 10).astype('f')
>>> outputs = np.random.randint(50, size=(1000, 100)).astype('i')

Training

Training the model can be as simple as calling the train() method:

>>> model.train([inputs, outputs])

See Training a Model for more information.

Use

A model can be used to predict() the output of some input data points:

>>> test = np.random.randn(3, 200, 10).astype('f')
>>> print(model.predict(test))

This method returns a two-dimensional array containing the most likely class for each input example and time step.

Note that the test data does not need to have the same number of time steps as the training data.

To retrieve the probabilities of the classes for each example, use predict_proba():

>>> model.predict_proba(test).shape
(3, 100, 50)

Recurrent classifiers have a predict_sequence() helper method that predicts values in an ongoing sequence. Given a seed value, the model predicts one time step ahead, then adds the prediction to the seed, predicts one more step ahead, and so on:

>>> seed = np.random.randint(50, size=10).astype('i')
>>> print(model.predict_sequence(seed, 100))

See Text for more utility code that is helpful for working with sequences of class labels.

See also Using a Model for more information.

__init__(layers, loss='xe', weighted=False)

Methods

__init__(layers[, loss, weighted])
predict_sequence(labels, steps[, streams, rng]) Draw a sequential sample of class labels from this network.

Attributes

DEFAULT_OUTPUT_ACTIVATION
INPUT_NDIM
inputs A list of Theano variables for feedforward computations.
num_params Number of parameters in the entire network model.
params A list of the learnable Theano parameters for this network.
variables A list of Theano variables for loss computations.
predict_sequence(labels, steps, streams=1, rng=None)

Draw a sequential sample of class labels from this network.

Parameters:

labels : list of int

A list of integer class labels to get the classifier started.

steps : int

The number of time steps to sample.

streams : int, optional

Number of parallel streams to sample from the model. Defaults to 1.

rng : numpy.random.RandomState or int, optional

A random number generator, or an integer seed for a random number generator. If not provided, the random number generator will be created with an automatically chosen seed.