def pad(x, width, val=0, batch_ndim=1):
 input_shape = x.shape
 input_ndim = x.ndim
 output_shape = list(input_shape)
 indices = [slice(None) for _ in output_shape]
 if isinstance(width, int):
  widths = ([width] * (input_ndim - batch_ndim))
 else:
  widths = width
 for (k, w) in enumerate(widths):
  try:
   (l, r) = w
  except TypeError:
   l = r = w
  output_shape[(k + batch_ndim)] += (l + r)
  indices[(k + batch_ndim)] = slice(l, (l + input_shape[(k + batch_ndim)]))
 if val:
  out = (T.ones(output_shape) * val)
 else:
  out = T.zeros(output_shape)
 return T.set_subtensor(out[tuple(indices)], x)

def pad ( x , width , val = 0 , batch_ndim = 1 ) : input_shape = x . shape input_ndim = x . ndim output_shape = list ( input_shape ) indices = [ slice ( None ) for _ in output_shape ] if isinstance ( width , int ) : widths = ( [ width ] * ( input_ndim - batch_ndim ) ) else : widths = width for ( k , w ) in enumerate ( widths ) : try : ( l , r ) = w except TypeError : l = r = w output_shape [ ( k + batch_ndim ) ] += ( l + r ) indices [ ( k + batch_ndim ) ] = slice ( l , ( l + input_shape [ ( k + batch_ndim ) ] ) ) if val : out = ( T . ones ( output_shape ) * val ) else : out = T . zeros ( output_shape ) return T . set_subtensor ( out [ tuple ( indices ) ] , x )
apply standard padding .