Skip to content

Integration with a TensorFlow Neural Network (With Estimators)

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

Preparation

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

  • You must have created a new project. Follow the instructions in Creating a project.

Note

Ensure that you can successfully run the basic basic script 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 script with 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 TensorFlowProject 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.TensorFlowProject()
    
  3. First, create a hook factory from within the model_fn:

    hooks = missinglink_project.estimator_hooks(
        display_name='MNIST multilayer perception',
        description='Two fully connected hidden layers',
        hyperparams=params.values(),
    )
    

    Parameter descriptions

    • display_name (optional): Name to be dislayed on the experiment
    • description (optional): Experiment description
    • hyperparams (optional): Hyperparams for the experiment will be logged when the experiment starts
  4. Still inside the model_fn function, create a hook for training and a hook for testing.

    Note

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

    training_hooks = None
    if mode == tf.estimator.ModeKeys.TRAIN:
        train_hook = hooks.create_train_hook(
            train_steps=params.train_steps,
            monitored_metrics={'loss': loss}
        )
    
        training_hooks = [train_hook]
    
    evaluation_hooks = None
    if mode == tf.estimator.ModeKeys.EVAL:
        test_hook = hooks.create_test_hook(expected=labels, predicted=predictions)
    
        evaluation_hooks = [test_hook]
    
  5. Pass the hooks to the creation of EstimatorSpec:

    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=predictions,
        loss=loss,
        train_op=train_op,
        eval_metric_ops=eval_metric_ops,
        training_hooks=training_hooks,
        evaluation_hooks=evaluation_hooks
    )
    


You should have integrated MissingLink's SDK successfully.

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

Web dashboard monitoring

You can monitor your experiment on your MissingLink dashboard.

Click on the experiment to view your metric graphs.

Next steps

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