theanets.recurrent.Classifier¶

class
theanets.recurrent.
Classifier
(layers, loss='xe', weighted=False, rng=13)¶ A classifier computes a distribution over labels, given an input.
Notes
Classifier models default to a
crossentropy
loss. To use a different loss, provide a nondefault argument for theloss
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 threedimensional arrays.
The first array provides the input data for the model. Its shape is (numexamples, numtimesteps, numvariables): 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 (numexamples, numtimesteps), 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 twodimensional 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, rng=13)¶
Methods
__init__
(layers[, loss, weighted, rng])add_layer
([layer, is_output])Add a layer to our network graph. add_loss
([loss])Add a loss function to the model. build_graph
([regularizers])Connect the layers in this network to form a computation graph. classify
(x)feed_forward
(x, **kwargs)Compute a forward pass of all layers from the given input. find
(which, param)Get a parameter from a layer in the network. itertrain
(train[, valid, algo, subalgo, ...])Train our network, one batch at a time. load
(filename)Load a saved network from disk. loss
(**kwargs)Return a variable representing the regularized loss for this network. monitors
(**kwargs)Return expressions that should be computed to monitor training. predict
(x)Compute a greedy classification for the given set of data. predict_logit
(x)Compute the logit values that underlie the softmax output. predict_proba
(x)Compute class posterior probabilities for the given set of data. predict_sequence
(labels, steps[, streams, rng])Draw a sequential sample of class labels from this network. save
(filename)Save the state of this network to a pickle file on disk. score
(x, y[, w])Compute the mean accuracy on a set of labeled data. set_loss
(*args, **kwargs)Clear the current loss functions from the network and add a new one. train
(*args, **kwargs)Train the network until the trainer converges. updates
(**kwargs)Return expressions to run as updates during network training. Attributes
DEFAULT_OUTPUT_ACTIVATION
INPUT_NDIM
OUTPUT_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, optionalA 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.
