Skip to content

How to Use

Robert Fratila edited this page Nov 10, 2017 · 16 revisions

Contents

Building a network


The Network object is equipped with constructing any DNN by specifying a few variables first.

from vulcanai.net import Network

input_var = T.fmatrix('input')
y = T.fmatrix('truth')

# Dense network configuration example
network_dense_config = {
    'mode': 'dense',
    'units': [1024, 1024, 784],
    'dropouts': [0.2, 0.2, 0.2],
}

# convolutional network configuration example
network_conv_config = {
    'mode': 'conv',
    'filters': [16, 32],
    'filter_size': [[5, 5], [5, 5]],
    'stride': [[1, 1], [1, 1]],
    'pool': {
        'mode': 'average_exc_pad',
        'stride': [[2, 2], [2, 2]]
    }
}

dense_net = Network(
    name='NETWORK_NAME',
    dimensions=(None, SIZE_OF_ROW),
    input_var=TENSOR_VAR,
    y=TENSOR_VAR,
    config=network_dense_config,
    input_network=None,
    num_classes=None,
    activation='rectify',
    pred_activation='softmax',
    optimizer='adam',
    learning_rate=0.001
)

The parameters name, dimensions, input_var, y, config are required to be defined. The parameters input_network, num_classes, activation, pred_activation, optimizer and learning_rate are defaulted values (as shown above) but they can also be modified.

Loading a network from disk


Since we save the entire Network object to disk, this alleviates the trouble of saving network weights separately. You don't need to worry about remembering what the network configuration was, the save does. It's as simple as:

dense_net = Network.load_model('path/to_model.network')

Since certain variables are omitted from saving, the network will load some of them and then build the rest of them internally. No additional steps are needed to be taken except for making sure your .theanorc file is configured properly.

Get network output


To do a forward pass of data through the network, run:

dense_net.forward_pass(input_data=data, convert_to_class=False)

If you would like the class probabilities, keep convert_to_class to False. If you would like a list of predictions for each input sample, set the parameter convert_to_class as True.

Training the model


Training a network requires a few parameters and passed data. One would configure a training session as such:

dense_net.train(
    epochs=NUM_OF_EPOCHS,
    train_x=TRAINING_SET_SAMPLES,
    train_y=TRAINING_SET_LABELS,
    val_x=VALIDATION_SET_SAMPLES,
    val_y=VALIDATION_SET_LABELS,
    batch_ratio=PARTITION_PERCENTAGE,
    plot=[True|False]
)
  • epochs: The number of iterations you want your model to pass over the entire data.
  • train_x: The training samples you would like to train the model on.
  • train_y: The training labels you would like to train the model against.
  • val_x: The samples you would like to validate the model with (which have not been trained on).
  • val_y: The labels you would like to validate the model with (which have not been trained on).
  • batch_ratio: When memory is a limiting factor, this will partition the training data by the specified percentage. Example: at 0.25, the model will train on 25% of the data at once and iterate 4 times until all training data has been used during a single epoch.
  • plot: Set to True if you desire live feedback about model performance. Set to False if you are training with no display.

Conducting the test suite


The method run_test() will perform all the relevant metrics that one would need to determine if the model is performing well enough. Passing it test samples and labels (which have not been trained on) is all it requires.

from vulcanai.model_tests import run_test, k_fold_validation

run_test(dense_net, test_x=TEST_SET_SAMPLES, test_y=TEST_SET_LABELS, figure_path='figures', plot=[True|False])

k_fold_validation(dense_net, test_x=TEST_SET_SAMPLES, test_y=TEST_SET_LABELS, k=K_FOLDS, epochs=NUM_EPOCHS_PER_FOLD, plot=[True|False])

Saving network to disk


Once you are satisfied with how your model performs, saving it is as easy as:

dense_net.save_model()

This will also save shallower connected networks if they haven't been saved yet. There is an optional save_path parameter which is the folder where the model will save. It defaults to models. It will also save the metadata, in JSON form, describing the network architecture for later analysis. There are certain variables that must be left out of saving to allow for gpu and cpu cross compatibility:

  • trainer <class 'theano.compile.function_module.Function'>
  • validator <class 'theano.compile.function_module.Function'>
  • y <class 'theano.tensor.var.TensorVariable'>
  • cost <class 'theano.tensor.var.TensorVariable'>
  • input_var <class 'theano.tensor.var.TensorVariable'>
  • output <class 'theano.compile.function_module.Function'>

Saving the training data to disk


To save the training curves, which consists of:

  • epoch
  • training_error
  • training_accuracy
  • validation_error
  • validation_accuracy

is as easy as:

dense_net.save_record()

There is an optional save_path parameter which is the folder where the record will save. It defaults to records. To display the record after saving:

from vulcanai.utils import display_record

display_record(load_path='pth/to/pickle/file')

more information on this can be found here

Clone this wiki locally