code: def mean_reduce_stats(worker_stats, res_stats=None):
      if not res_stats:
          res_stats = {}
      for stat_key, stat_value in worker_stats[0].items():
          if isinstance(stat_value, numbers.Number):  # loss
              res_stats[stat_key] = np.nanmean(
                  [s.get(stat_key, np.nan) for s in worker_stats]
              )
          elif isinstance(stat_value, torch.Tensor):  # Accuracy
              res_stats[stat_key] = torch.mean(
                  torch.stack(
                      [stats[stat_key] for stats in worker_stats]
                  )
              )
          elif isinstance(stat_value, dict):  # profile
              res_stats[stat_key] = mean_reduce_stats(
                  [stats[stat_key] for stats in worker_stats]
              )
          else:
              res_stats[stat_key] = stat_value
      return res_stats
  
imports: import time
  import numbers
  import socket
  import collections
  import numpy
  from contextlib import closing, contextmanager
  import torch
  from bigdl.dllib.utils.log4Error import *
label_desc: Use image node_id 8 for calling a global function with example usage: mean_reduce_stats(worker_stats, res_stats) and returns: res_stats