Edit a TensorFlow v2 training model for deep learning insights

Before uploading a TensorFlow v2 training model, edit the model to work with the deep learning insights feature in Deep Learning Impact.

About this task

In order for the deep learning insights feature to work with your single-node TensorFlow v2, your model must call additional Deep Learning Impact APIs in order to collect training metrics. Training metrics are what are displayed in the training insight charts.

Procedure

  1. The model must import the monitor_tf module, a Python module for metric collection.
    import monitor_tf
  2. If the TensorFlow model uses Keras API model.fit() for training, pass a TFKerasMonitorCallback to the fit() function. Example:
    from monitor_tf import TFKerasMonitorCallback
    model=...
    model.compile
    monitor=TFKerasMonitorCallback(test_ds)
    model.fit(train_ds, epochs=1, steps_per_epoch=1, callbacks=[monitor])
    Note: Using this method the following insights are unavailable: activation histogram, gradient histogram, gradient ratio or weight ratio.
  3. If the TensorFlow model uses a custom loop for training, use TFKerasMonitor. Example:
    code example:
    from monitor_tf import TFKerasMonitor
    model=...
    tfmonitor = TFKerasMonitor(model)
    for epoch in range(100):
      print('Start of epoch %d' % (epoch,))
    
      for step, (x_batch, y_batch) in enumerate(train_ds):
        ...
        tfmonitor.log_train(step, grads, loss_value, accuracy, x_batch)
        ...
     
      for step, (x_batch, y_batch) in enumerate(test_ds):
          ...
          tfmonitor.log_test(step, loss_value, accuracy)
          ...

Results

The edited TensorFlow model is ready for training insights.