elektronn2.neuromancer.model module

class elektronn2.neuromancer.model.Model(name='')[source]

Bases: elektronn2.neuromancer.graphmanager.GraphManager

Represents a neural network model and its current training state.

The Model is defined and checked by running Model.designate_nodes() with appropriate Nodes as arguments (see example in examples/numa_mnist.py).

Permanently saving a Model with its respective training state is possible with the Model.save() function. Loading a Model from a file is done by elektronn2.neuromancer.model.modelload().

During training of a neural network, you can access the current Model via the interactive training shell as the variable “model” (see elektronn2.training.trainutils.user_input()). There are several statistics and hyperparameters of the Model that you can inspect and set directly in the shell, e.g. entering >>> model.lr = 1e-3 and exiting the prompt again effectively sets the learning rate to 1e-3 for further training. (This can also be done with the shortcut “setlr 1e-3”.)

activations(*args, **kwargs)[source]
actstats(*args, **kwargs)[source]
batch_normalisation_active

Check if batch normalisation is active in any Node in the Model.

debug_output_names

If debug_outputs is set, a list of all debug output names is returned.

designate_nodes(input_node='input', target_node=None, loss_node=None, prediction_node=None, prediction_ext=None, error_node=None, debug_outputs=None)[source]

Register input, target and other special Nodes in the Model.

Most of the Model’s attributes are derived from the Nodes that are given as arguments here.

dropout_rates

Get dropout rates.

get_param_values(skip_const=False, as_list=False)[source]

Only use this to save/load parameters!

Returns a dict of mapping the values of the params (such that they can be saved to disk) :param skip_const: whether to exclude constant parameters

gradients(*args, **kwargs)[source]
gradnet_rates

Get gradnet rates.

Description: https://arxiv.org/abs/1511.06827

gradstats(*args, **kwargs)[source]
loss(*args, **kwargs)[source]
loss_input_shapes

Get shape(s) of loss nodes’ input node(s).

The return value is either a shape (if one input) or a list of shapes (if multiple inputs).

loss_smooth

Get average loss during the last training steps.

The average is calculated over the last n steps, where n is defined by the config variable time_per_step_smoothing_length (default: 50).

lr

Get learning rate.

measure_exectimes(n_samples=5, n_warmup=4, print_info=True)[source]

Return an OrderedDict that maps node names to their estimated execution times in milliseconds.

Parameters are the same as in elektronn2.neuromancer.node_basic.Node.measure_exectime()

mixing

Get mixing weights.

mom

Get momentum.

paramstats()[source]
predict(*args, **kwargs)[source]
predict_dense(raw_img, as_uint8=False, pad_raw=False)[source]
predict_ext(*args, **kwargs)[source]
prediction_feature_names

If a prediction node is set, return its feature names.

save(file_name)[source]

Save a Model (including its training state) to a pickle file. :param file_name: File name to save the Model in.

set_opt_meta_params(opt_name, value_dict)[source]
set_param_values(value_dict, skip_const=False)[source]

Only use this to save/load parameters!

Sets new values for non constant parameters :param value_dict: dict mapping values by parameter name / or file name thereof :param skip_const: if dict also maps values for constants, these can be skipped, otherwise an exception is raised.

test_run_prediction()[source]

Execute test run on the prediction node.

time_per_step

Get average run time per training step.

The average is calculated over the last n steps, where n is defined by the config variable time_per_step_smoothing_length (default: 50).

trainingstep(*args, **kwargs)[source]

Perform one optimiser iteration. Optimisers can be chosen by the kwarg optimiser.

Signature: trainingstep(data, target(, *aux)(, **kwargs**))

Parameters:
  • *args
    • data: floatX array
      input [bs, ch (, z, y, x)]
    • targets: int16 array
      [bs,((z,)y,x)] (optional)
    • (optional) other inputs: np.ndarray
      depending in model
  • **kwargs
    • optimiser: str
      Name of the chosen optimiser class in elektronn2.neuromancer.optimiser
    • update_loss: Bool
      determine current loss after update step (e.g. needed for queue, but get_loss can also be called explicitly)
Returns:

  • loss (floatX) – Loss (nll, squared error etc…)
  • t (float) – Time spent on the GPU per step

wd

Get weight decay.

elektronn2.neuromancer.model.modelload(file_name, override_mfp_to_active=False, imposed_patch_size=None, imposed_batch_size=None, name=None, **model_load_kwargs)[source]

Load a Model from a pickle file (created by Model.save()).

model_load_kwargs: remove_bn, make_weights_constant (True/False)

elektronn2.neuromancer.model.kernel_lists_from_node_descr(model_descr)[source]

Extract the tuple (filter_shapes, pool_shapes, mfp) from a model description.

Parameters:model_descr – Model description OrderedDict.
Returns:Tuple (filter_shapes, pool_shapes, mfp).
elektronn2.neuromancer.model.params_from_model_file(file_name)[source]

Load parameters from a model file.

Parameters:file_name – File name of the pickled Model.
Returns:OrderedDict of model parameters.
elektronn2.neuromancer.model.rebuild_model(model, override_mfp_to_active=False, imposed_patch_size=None, name=None, **model_load_kwargs)[source]

Rebuild a Model by saving it to a file and reloading it from there.

Parameters:
  • model – Model object.
  • override_mfp_to_active – (See elektronn2.neuromancer.model.modelload()).
  • imposed_patch_size – (See elektronn2.neuromancer.model.modelload()).
  • name – New model name.
  • model_load_kwargs – Additional kwargs for restoring Model (see elektronn2.neuromancer.graphmanager.GraphManager.restore()).
Returns:

Rebuilt Model.

elektronn2.neuromancer.model.simple_cnn(batch_size, n_ch, n_lab, desired_input, filters, nof_filters, activation_func, pools, mfp=False, tags=None, name=None)[source]

Create a simple Model of a convolutional neural network. :param batch_size: Batch size (how many data samples are used in one

update step).
Parameters:
  • n_ch – Number of channels.
  • n_lab – Number of distinct labels (classes).
  • desired_input – Desired input image size. (Must be smaller than the size of the training images).
  • filters – List of filter sizes in each layer.
  • nof_filters – List of number of filters for each layer.
  • activation_func – Activation function.
  • pools – List of maxpooling factors for each layer.
  • mfp – List of bools that tell if max fragment pooling should be used in each layer (only intended for prediction).
  • tags – Tuple of tags for Input node (see docs of elektronn2.neuromancer.node_basic.Input).
  • name – Name of the model.
Returns:

Network Model.