code: def average_multi_opt_gradients(multi_device_gradients):
      """
      This function averages the gradients generated by each optimiser
      """
      if not multi_device_gradients:
          # nothing to average
          return multi_device_gradients
  
      if isinstance(multi_device_gradients[0], dict):
          # multi_device_gradients is a list of N dictionaries, for N devices
          # each dictionary is a pair of optimiser_name: device_gradient
          optimiser_names = sorted(multi_device_gradients[0])
          ave_gradients = dict()
          for opt_name in optimiser_names:
              multi_device_grad = [
                  device_gradient.get(opt_name)
                  for device_gradient in multi_device_gradients
              ]
              ave_gradients[opt_name] = average_gradients(
                  multi_device_grad
              )
          return ave_gradients
      # multi_device_gradients is a list of N device_gradients, for N devices
      return average_gradients(multi_device_gradients)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import datetime
  import os
  import re
  from functools import partial
  import numpy
  import tensorflow
  from scipy import ndimage
  from six import string_types
label_desc: Use image node_id 3 for calling a global function with example usage: average_multi_opt_gradients(multi_device_gradients) and returns: average_gradients, multi_device_gradients, ave_gradients