def _sum_clones_gradients(clone_grads):
 sum_grads = []
 for grad_and_vars in zip(*clone_grads):
  grads = []
  var = grad_and_vars[0][1]
  for (g, v) in grad_and_vars:
   assert (v == var)
   if (g is not None):
    grads.append(g)
  if grads:
   if (len(grads) > 1):
    sum_grad = tf.add_n(grads, name=(var.op.name + '/sum_grads'))
   else:
    sum_grad = grads[0]
   sum_grads.append((sum_grad, var))
 return sum_grads

def _sum_clones_gradients ( clone_grads ) : sum_grads = [ ] for grad_and_vars in zip ( * clone_grads ) : grads = [ ] var = grad_and_vars [ 0 ] [ 1 ] for ( g , v ) in grad_and_vars : assert ( v == var ) if ( g is not None ) : grads . append ( g ) if grads : if ( len ( grads ) > 1 ) : sum_grad = tf . add_n ( grads , name = ( var . op . name + '/sum_grads' ) ) else : sum_grad = grads [ 0 ] sum_grads . append ( ( sum_grad , var ) ) return sum_grads
calculate the sum gradient for each shared variable across all clones .