def BatchClipByL2norm(t, upper_bound, name=None):
 assert (upper_bound > 0)
 with tf.op_scope([t, upper_bound], name, 'batch_clip_by_l2norm') as name:
  saved_shape = tf.shape(t)
  batch_size = tf.slice(saved_shape, [0], [1])
  t2 = tf.reshape(t, tf.concat(0, [batch_size, [(-1)]]))
  upper_bound_inv = tf.fill(tf.slice(saved_shape, [0], [1]), tf.constant((1.0 / upper_bound)))
  l2norm_inv = tf.rsqrt((tf.reduce_sum((t2 * t2), [1]) + 1e-06))
  scale = (tf.minimum(l2norm_inv, upper_bound_inv) * upper_bound)
  clipped_t = tf.matmul(tf.diag(scale), t2)
  clipped_t = tf.reshape(clipped_t, saved_shape, name=name)
 return clipped_t

def BatchClipByL2norm ( t , upper_bound , name = None ) : assert ( upper_bound > 0 ) with tf . op_scope ( [ t , upper_bound ] , name , 'batch_clip_by_l2norm' ) as name : saved_shape = tf . shape ( t ) batch_size = tf . slice ( saved_shape , [ 0 ] , [ 1 ] ) t2 = tf . reshape ( t , tf . concat ( 0 , [ batch_size , [ ( - 1 ) ] ] ) ) upper_bound_inv = tf . fill ( tf . slice ( saved_shape , [ 0 ] , [ 1 ] ) , tf . constant ( ( 1.0 / upper_bound ) ) ) l2norm_inv = tf . rsqrt ( ( tf . reduce_sum ( ( t2 * t2 ) , [ 1 ] ) + 1e-06 ) ) scale = ( tf . minimum ( l2norm_inv , upper_bound_inv ) * upper_bound ) clipped_t = tf . matmul ( tf . diag ( scale ) , t2 ) clipped_t = tf . reshape ( clipped_t , saved_shape , name = name ) return clipped_t
clip an array of tensors by l2 norm .