theanets.feedforward.Classifier¶
-
class
theanets.feedforward.
Classifier
(layers, loss='xe', weighted=False, rng=13)[source]¶ 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 theloss
keyword argument when constructing your model.Examples
To create a classification model, just create a new class instance. Often you’ll provide the layer configuration at this time:
>>> model = theanets.Classifier([10, (20, 'tanh'), 50])
See Creating a Model for more information.
Data
Training data for a classification model takes the form of a two-dimensional array of input data and a one-dimensional vector of target labels. The input array has a shape (num-examples, num-variables): the first axis enumerates data points in a batch, and the second enumerates the input variables in the model.
The second array provides the target class labels for the inputs. Its shape is (num-examples, ), and each integer value in the array gives the class label for the corresponding input example.
For instance, to create a training dataset containing 1000 examples:
>>> inputs = np.random.randn(1000, 10).astype('f') >>> outputs = np.random.randint(50, size=1000).astype('i')
Training
Training the model can be as simple as calling the
train()
method, giving the inputs and target outputs as a dataset:>>> model.train([inputs, outputs])
See Training a Model for more information.
Use
A classification model can be used to
predict()
the output of some input data points:>>> test = np.random.randn(3, 10).astype('f') >>> print(model.predict(test))
This method returns a vector containing the most likely class for each input example.
To retrieve the probabilities of the classes for each example, use
predict_proba()
:>>> model.predict_proba(test).shape (3, 50)
See also Using a Model for more information.
-
__init__
(layers, loss='xe', weighted=False, rng=13)[source]¶ x.__init__(…) initializes x; see help(type(x)) for signature
Methods
__init__
(layers[, loss, weighted, rng])x.__init__(…) initializes x; see help(type(x)) for signature add_layer
([layer])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, **kwargs)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_or_handle)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, **kwargs)Compute a greedy classification for the given set of data. predict_logit
(x, **kwargs)Compute the logit values that underlie the softmax output. predict_proba
(x, **kwargs)Compute class posterior probabilities for the given set of data. save
(filename_or_handle)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
Default activation for the output layer. INPUT_NDIM
OUTPUT_NDIM
Number of dimensions for holding output data arrays. inputs
A list of Theano variables for feedforward computations. params
A list of the learnable Theano parameters for this network. variables
A list of Theano variables for loss computations. -
DEFAULT_OUTPUT_ACTIVATION
= 'softmax'¶ Default activation for the output layer.
-
OUTPUT_NDIM
= 1¶ Number of dimensions for holding output data arrays.
-
monitors
(**kwargs)[source]¶ Return expressions that should be computed to monitor training.
Returns: - monitors : list of (name, expression) pairs
A list of named monitor expressions to compute for this network.
-
predict
(x, **kwargs)[source]¶ Compute a greedy classification for the given set of data.
Parameters: - x : ndarray (num-examples, num-variables)
An array containing examples to classify. Examples are given as the rows in this array.
Returns: - k : ndarray (num-examples, )
A vector of class index values, one per row of input data.
-
predict_logit
(x, **kwargs)[source]¶ Compute the logit values that underlie the softmax output.
Parameters: - x : ndarray (num-examples, num-variables)
An array containing examples to classify. Examples are given as the rows in this array.
Returns: - l : ndarray (num-examples, num-classes)
An array of posterior class logit values, one row of logit values per row of input data.
-
predict_proba
(x, **kwargs)[source]¶ Compute class posterior probabilities for the given set of data.
Parameters: - x : ndarray (num-examples, num-variables)
An array containing examples to predict. Examples are given as the rows in this array.
Returns: - p : ndarray (num-examples, num-classes)
An array of class posterior probability values, one per row of input data.
-
score
(x, y, w=None, **kwargs)[source]¶ Compute the mean accuracy on a set of labeled data.
Parameters: - x : ndarray (num-examples, num-variables)
An array containing examples to classify. Examples are given as the rows in this array.
- y : ndarray (num-examples, )
A vector of integer class labels, one for each row of input data.
- w : ndarray (num-examples, )
A vector of weights, one for each row of input data.
Returns: - score : float
The (possibly weighted) mean accuracy of the model on the data.
-