Skip to content

Integration with PyTorch (With Steps)

This topic shows you how to integrate MissingLink SDK with a PyTorch multilayer perception neural network that is trained on the MNIST dataset.

The following steps are covered:

  • Define a project callback with your credentials.
  • Create a new experiment.
  • Define an experiment context.
  • Change the loop.
  • Define a validation context.
  • Define a testing context.


You can also consider trying the step-by-step tutorial for integrating the MissingLink SDK with an existing PyTorch example.


  • You must have PyTorch installed in the same working environment that MissingLink SDK is installed. The SDK doesn't enforce PyTorch as one of its dependencies.

  • You must have created a new project. If not, follow the instructions in Creating a project.


Ensure that you can successfully run the basic training script. In the steps that follow below, the basic script is integrated with the MissingLink SDK to enable remote monitoring of the training, validation, and testing process.

Compare the basic scriptwith the integrated script.

Write code

  1. Import the SDK and define your credentials at the beginning of the file (before any function definition).

    import missinglink
  2. Now, create a PyTorchProject instance with your credentials, which helps to monitor the experiment in real time. In the run_training function and before the training loop, add the following statement:

    missinglink_project = missinglink.PyTorchProject()
  3. Create a new experiment as the outermost context, wrapping around the training loop. You need to pass the trained model as an argument. You also need to pass the monitored metrics inside a dictionary that maps the metric name to its function. You can provide the experiment with a name and description.


    If you are using custom metrics, there are additional steps that you need to perform first. For more information, see Visualization of PyTorch Custom Metrics.

    Add the following statement right before the training loop:

    with missinglink_project.create_experiment(
        metrics={'loss': loss},
        display_name='MNIST multilayer perception',
        description='Two fully connected hidden layers') as experiment:

    Parameter descriptions

    • model: Reference to the model object
    • metrics: Dictionary of all the metrics that will be tracked during the experiment
    • display_name (optional): Experiment name
    • description (optional): Experiment description
  4. The metrics you gave the experiment now have a wrapped version in experiment.metrics. In order for the metrics to be monitored, you need to call the wrapped version instead of the original. After creating the experiment, write:

    loss_function = experiment.metrics['loss']

    You should treat this new loss_function just as if it were the original. Invoke it inside your training loop, your validation loop, and your test loop.

  5. Within the experiment context, change the for loop to use experiment.loop generator instead of train_loader iterator:

    # `train_loader` is your `` that loads the train data.
    for step, (data, target) in experiment.loop(iterable=train_loader):
        data, target = Variable(data), Variable(target)
        # Perform a training step on the data

    The iterable argument can be any iterable you wish, like a list, a file, a generator function, etc. When used with the iterable parameter, loop yields the index of the step and the data from the iterable.


    Additional implementations of iteration loop

    • Use iterable parameter
      loop can also iterate over an iterable, using the iterable parameter:
    for step, data in experiment.loop(iterable=train_data):
    # Perform a training step on the data
    • Use lambda condition

    There is an optional parameter, condition that can be added here to augment the way the steps are run.

    For example, if you change the above statement to the following:

    Note that this is not the actual loss value - it's a variable that has been created as an example and the following will run the training as long as the loss value is more than 0.5%.

    loss_value = 0.55
    for step in experiment.loop(condition=lambda _: loss_value > 0.5):
  6. Add the experiment.validation context around the call to the validation function.

    if step % val_interval == 0:
        with experiment.validation():

    Make sure to invoke the wrapped metric functions inside the validation function.

  7. Add the experiment.test context around the call to the test function.

    Choose one of the following ways to use the test context.

    Option 1: Using a DataLoader

    If you use a to load the test data, use it so:

    if step % test_interval == 0:
        with experiment.test(model, test_data_object=test_loader):

    Parameter descriptions

    • model: The tested model
    • test_loader: The that loads the test data.

    Option 2: Using an Iterator

    If you use one of the iterators to load the test data, use it so:

    with experiment.test(model, test_data_object=iterator, target_attribute_name='label'):

    Parameter descriptions

    • model: The tested model
    • iterator: The iterator that iterates over the data. Can be an Iterator, a BucketIterator, or a BPTTIterator.
    • target_attribute_name: The attribute name of the target of every batch, so that batch.target_attribute_name is the target. Defaults to 'label'.

    Option 3: Testing Manually

    Otherwise, use the test context manually:

    with test(model, test_iterations=1000):
        test() # call here `test_iterations` times to `confusion_matrix`

    Parameter descriptions

    • model: The tested model.
    • test_iterations: The number of test iterations (batches) that are going to be performed.

    Use the context with the tested model and with the number of test iterations. Then, inside the testing function, call experiment.confusion_matrix(output, target) test_iterations times:

    Parameter descriptions

    • output: A 2D torch.autograd.Variable. The output of the model for a single test batch.
    • target: A 1D torch.autograd.Variable or Array-Like. The targets (labels) of a single test batch.

    If you do implement a testing context, MissingLink automatically adds a confusion matrix, which you can view under the Test tab for the experiment:

    When implementing a testing context, ML adds confusion matrix and test metrics under the PyTorch test

You should have integrated MissingLink's SDK successfully.

  • Inspect the resulting integrated script.
  • Run the new script and see how the MissingLink dashboard helps with monitoring the experiment. A description follows.

Web dashboard monitoring

You can monitor your experiment on your MissingLink dashboard.

monitor your PyTorch experiment on your MissingLink dashboard

Click on the experiment to view your metric graphs.

Click on the PyTorch experiment to view your metric graphs

Next steps

Learn more about integrating with PyTorch to enable the following MissingLink features: