chainer_chemistry.models.Classifier

class chainer_chemistry.models.Classifier(predictor, lossfun=<function softmax_cross_entropy>, accfun=None, metrics_fun=<function accuracy>, label_key=-1, device=-1)[source]

A simple classifier model.

This is an example of chain that wraps another chain. It computes the loss and accuracy based on a given input/label pair.

Parameters:
  • predictor (Link) – Predictor network.
  • lossfun (function) – Loss function.
  • accfun (function) – DEPRECATED. Please use metrics_fun instead.
  • metrics_fun (function or dict or None) – Function that computes metrics.
  • label_key (int or str) – Key to specify label variable from arguments. When it is int, a variable in positional arguments is used. And when it is str, a variable in keyword arguments is used.
  • device (int) – GPU device id of this Classifier to be used. -1 indicates to use in CPU.
predictor

Predictor network.

Type:Link
lossfun

Loss function.

Type:function
accfun

DEPRECATED. Please use metrics_fun instead.

Type:function
y

Prediction for the last minibatch.

Type:Variable
loss

Loss value for the last minibatch.

Type:Variable
metrics

Metrics computed in last minibatch

Type:dict
compute_metrics

If True, compute metrics on the forward computation. The default value is True.

Type:bool

Note

The differences between original Classifier class in chainer and chainer chemistry are as follows. 1. predict and predict_proba methods are supported. 2. device can be managed internally by the Classifier 3. accfun is deprecated, metrics_fun is used instead. 4. metrics_fun can be dict which specifies the metrics name as key

and function as value.

Note

This link uses chainer.softmax_cross_entropy() with default arguments as a loss function (specified by lossfun), if users do not explicitly change it. In particular, the loss function does not support double backpropagation. If you need second or higher order differentiation, you need to turn it on with enable_double_backprop=True:

>>> import chainer.functions as F
>>> import chainer.links as L
>>>
>>> def lossfun(x, t):
...     return F.softmax_cross_entropy(
...         x, t, enable_double_backprop=True)
>>>
>>> predictor = L.Linear(10)
>>> model = L.Classifier(predictor, lossfun=lossfun)
__init__(predictor, lossfun=<function softmax_cross_entropy>, accfun=None, metrics_fun=<function accuracy>, label_key=-1, device=-1)[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(predictor[, lossfun, accfun, …]) Initialize self.
add_hook(hook[, name]) Registers a link hook.
add_link(name, link) Registers a child link to this chain.
add_param(name[, shape, dtype, initializer]) Registers a parameter to the link.
add_persistent(name, value) Registers a persistent value to the link.
addgrads(link) Accumulates gradient values from given link.
children() Returns a generator of all child links.
cleargrads() Clears all gradient arrays.
copy([mode]) Copies the link hierarchy to new one.
copyparams(link[, copy_persistent]) Copies all parameters from given link.
count_params() Counts the total number of parameters.
delete_hook(name) Unregisters the link hook.
disable_update() Disables update rules of all parameters under the link hierarchy.
enable_update() Enables update rules of all parameters under the link hierarchy.
get_device()
init_scope() Creates an initialization scope.
initialize([device]) Initialization of the model.
links([skipself]) Returns a generator of all links under the hierarchy.
load_pickle(filepath[, device]) Load the model from filepath of pickle file, and send to device
namedlinks([skipself]) Returns a generator of all (path, link) pairs under the hierarchy.
namedparams([include_uninit]) Returns a generator of all (path, param) pairs under the hierarchy.
params([include_uninit]) Returns a generator of all parameters under the link hierarchy.
predict(data[, batchsize, converter, …]) Predict label of each category by taking .
predict_proba(data[, batchsize, converter, …]) Calculate probability of each category.
register_persistent(name) Registers an attribute of a given name as a persistent value.
repeat(n_repeat[, mode]) Repeats this link multiple times to make a Sequential.
save_pickle(filepath[, protocol]) Save the model to filepath as a pickle file
serialize(serializer) Serializes the link object.
to_cpu() Copies parameter variables and persistent values to CPU.
to_gpu([device]) Copies parameter variables and persistent values to GPU.
to_intel64() Copies parameter variables and persistent values to CPU.
update_device([device])
zerograds() Initializes all gradient arrays by zero.

Attributes

accfun
accuracy
compute_accuracy
compute_metrics
local_link_hooks Ordered dictionary of registered link hooks.
update_enabled True if at least one parameter has an update rule enabled.
within_init_scope True if the current code is inside of an initialization scope.
xp Array module for this link.