signature:
def find_contours ( x , level = 0.8 , fully_connected = 'low' , positive_orientation = 'low' ) :
    """Find iso - valued contours in a 2D array for a given level value , returns list of ( n , 2 ) - ndarrays
 see ` skimage . measure . find _ contours < http : / / scikit - image . org / docs / dev / api / skimage . measure . html # skimage . measure . find _ contours > ` _ _ .
 Parameters
 x : 2D ndarray of double .
 Input data in which to find contours .
 level : float
 Value along which to find contours in the array .
 fully _ connected : str
 Either ` low ` or ` high ` . Indicates whether array elements below the given level value are to be considered fully - connected ( and hence elements above the value will only be face connected ) , or vice - versa . ( See notes below for details . )
 positive _ orientation : str
 Either ` low ` or ` high ` . Indicates whether the output contours will produce positively - oriented polygons around islands of low - or high - valued elements . If ` low ` then contours will wind counter - clockwise around elements below the iso - value . Alternately , this means that low - valued elements are always on the left of the contour .
 Returns
 list of ( n , 2 ) - ndarrays
 Each contour is an ndarray of shape ( n , 2 ) , consisting of n ( row , column ) coordinates along the contour ."""

implementation: return skimage . measure . find_contours ( x , level , fully_connected = fully_connected , positive_orientation = positive_orientation )


signature:
def pt2map ( list_points = None , size = ( 100 , 100 ) , val = 1 ) :
    """Inputs a list of points , return a 2D image .
 Parameters
 list _ points : list of 2 int
 [ [ x , y ] , [ x , y ] . . ] for point coordinates .
 size : tuple of 2 int
 ( w , h ) for output size .
 val : float or int
 For the contour value .
 Returns
 numpy . array
 An image ."""

implementation:
if list_points is None :
    raise Exception ( "list_points : list of 2 int" )
i_m = np . zeros ( size )
if len ( list_points ) == 0 :
    return i_m
for xx in list_points :
    for x in xx : # tl . logging . info ( x )
        i_m [ int ( np . round ( x [ 0 ] ) ) ] [ int ( np . round ( x [ 1 ] ) ) ] = val
return i_m


signature:
def binary_dilation ( x , radius = 3 ) :
    """Return fast binary morphological dilation of an image .
 see ` skimage . morphology . binary _ dilation < http : / / scikit - image . org / docs / dev / api / skimage . morphology . html # skimage . morphology . binary _ dilation > ` _ _ .
 Parameters
 x : 2D array
 A binary image .
 radius : int
 For the radius of mask .
 Returns
 numpy . array
 A processed binary image ."""

implementation:
mask = disk ( radius )
x = _binary_dilation ( x , selem = mask )
return x


signature:
def dilation ( x , radius = 3 ) :
    """Return greyscale morphological dilation of an image ,
 see ` skimage . morphology . dilation < http : / / scikit - image . org / docs / dev / api / skimage . morphology . html # skimage . morphology . dilation > ` _ _ .
 Parameters
 x : 2D array
 An greyscale image .
 radius : int
 For the radius of mask .
 Returns
 numpy . array
 A processed greyscale image ."""

implementation:
mask = disk ( radius )
x = dilation ( x , selem = mask )
return x


signature:
def binary_erosion ( x , radius = 3 ) :
    """Return binary morphological erosion of an image ,
 see ` skimage . morphology . binary _ erosion < http : / / scikit - image . org / docs / dev / api / skimage . morphology . html # skimage . morphology . binary _ erosion > ` _ _ .
 Parameters
 x : 2D array
 A binary image .
 radius : int
 For the radius of mask .
 Returns
 numpy . array
 A processed binary image ."""

implementation:
mask = disk ( radius )
x = _binary_erosion ( x , selem = mask )
return x


signature:
def erosion ( x , radius = 3 ) :
    """Return greyscale morphological erosion of an image ,
 see ` skimage . morphology . erosion < http : / / scikit - image . org / docs / dev / api / skimage . morphology . html # skimage . morphology . erosion > ` _ _ .
 Parameters
 x : 2D array
 A greyscale image .
 radius : int
 For the radius of mask .
 Returns
 numpy . array
 A processed greyscale image ."""

implementation:
mask = disk ( radius )
x = _erosion ( x , selem = mask )
return x


signature:
def obj_box_coords_rescale ( coords = None , shape = None ) :
    """Scale down a list of coordinates from pixel unit to the ratio of image size i . e . in the range of [ 0 , 1 ] .
 Parameters
 coords : list of list of 4 ints or None
 For coordinates of more than one images . e . g . [ [ x , y , w , h ] , [ x , y , w , h ] , . . . ] .
 shape : list of 2 int or None
 【 height , width ] .
 Returns
 list of list of 4 numbers
 A list of new bounding boxes .
 Examples
 > > > coords = obj _ box _ coords _ rescale ( coords = [ [ 30 , 40 , 50 , 50 ] , [ 10 , 10 , 20 , 20 ] ] , shape = [ 100 , 100 ] )
 > > > print ( coords )
 [ [ 0.3 , 0.4 , 0.5 , 0.5 ] , [ 0.1 , 0.1 , 0.2 , 0.2 ] ]
 > > > coords = obj _ box _ coords _ rescale ( coords = [ [ 30 , 40 , 50 , 50 ] ] , shape = [ 50 , 100 ] )
 > > > print ( coords )
 [ [ 0.3 , 0.8 , 0.5 , 1.0 ] ]
 > > > coords = obj _ box _ coords _ rescale ( coords = [ [ 30 , 40 , 50 , 50 ] ] , shape = [ 100 , 200 ] )
 > > > print ( coords )
 [ [ 0.15 , 0.4 , 0.25 , 0.5 ] ]
 Returns
 list of 4 numbers
 New coordinates ."""

implementation:
if coords is None :
    coords = [ ]
if shape is None :
    shape = [ 100 , 200 ]
imh , imw = shape [ 0 ] , shape [ 1 ]
imh = imh * 1.0
# * 1.0 for python2 : force division to be float point
imw = imw * 1.0
coords_new = list ( )
for coord in coords :
    if len ( coord ) != 4 :
        raise AssertionError ( "coordinate should be 4 values : [x, y, w, h]" )
    x = coord [ 0 ] / imw
    y = coord [ 1 ] / imh
    w = coord [ 2 ] / imw
    h = coord [ 3 ] / imh
    coords_new . append ( [ x , y , w , h ] )
return coords_new


signature:
def obj_box_coord_rescale ( coord = None , shape = None ) :
    """Scale down one coordinates from pixel unit to the ratio of image size i . e . in the range of [ 0 , 1 ] .
 It is the reverse process of ` ` obj _ box _ coord _ scale _ to _ pixelunit ` ` .
 Parameters
 coords : list of 4 int or None
 One coordinates of one image e . g . [ x , y , w , h ] .
 shape : list of 2 int or None
 For [ height , width ] .
 Returns
 list of 4 numbers
 New bounding box .
 Examples
 > > > coord = tl . prepro . obj _ box _ coord _ rescale ( coord = [ 30 , 40 , 50 , 50 ] , shape = [ 100 , 100 ] )
 [0.3 , 0.4 , 0.5 , 0.5]"""

implementation:
if coord is None :
    coord = [ ]
if shape is None :
    shape = [ 100 , 200 ]
return obj_box_coords_rescale ( coords = [ coord ] , shape = shape ) [ 0 ]


signature:
def obj_box_coord_scale_to_pixelunit ( coord , shape = None ) :
    """Convert one coordinate [ x , y , w ( or x2 ) , h ( or y2 ) ] in ratio format to image coordinate format .
 It is the reverse process of ` ` obj _ box _ coord _ rescale ` ` .
 Parameters
 coord : list of 4 float
 One coordinate of one image [ x , y , w ( or x2 ) , h ( or y2 ) ] in ratio format , i . e value range [ 0 ~ 1 ] .
 shape : tuple of 2 or None
 For [ height , width ] .
 Returns
 list of 4 numbers
 New bounding box .
 Examples
 > > > x , y , x2 , y2 = tl . prepro . obj _ box _ coord _ scale _ to _ pixelunit ( [ 0.2 , 0.3 , 0.5 , 0.7 ] , shape = ( 100 , 200 , 3 ) )
 [40 , 30 , 100 , 70]"""

implementation:
if shape is None :
    shape = [ 100 , 100 ]
imh , imw = shape [ 0 : 2 ]
x = int ( coord [ 0 ] * imw )
x2 = int ( coord [ 2 ] * imw )
y = int ( coord [ 1 ] * imh )
y2 = int ( coord [ 3 ] * imh )
return [ x , y , x2 , y2 ]


signature:
def obj_box_coord_centroid_to_upleft_butright ( coord , to_int = False ) :
    """Convert one coordinate [ x _ center , y _ center , w , h ] to [ x1 , y1 , x2 , y2 ] in up - left and botton - right format .
 Parameters
 coord : list of 4 int / float
 One coordinate .
 to _ int : boolean
 Whether to convert output as integer .
 Returns
 list of 4 numbers
 New bounding box .
 Examples
 > > > coord = obj _ box _ coord _ centroid _ to _ upleft _ butright ( [ 30 , 40 , 20 , 20 ] )
 [20 , 30 , 40 , 50]"""

implementation:
if len ( coord ) != 4 :
    raise AssertionError ( "coordinate should be 4 values : [x, y, w, h]" )
x_center , y_center , w , h = coord
x = x_center - w / 2.
y = y_center - h / 2.
x2 = x + w
y2 = y + h
if to_int :
    return [ int ( x ) , int ( y ) , int ( x2 ) , int ( y2 ) ]
else :
    return [ x , y , x2 , y2 ]


signature:
def obj_box_coord_upleft_butright_to_centroid ( coord ) :
    """Convert one coordinate [ x1 , y1 , x2 , y2 ] to [ x _ center , y _ center , w , h ] .
 It is the reverse process of ` ` obj _ box _ coord _ centroid _ to _ upleft _ butright ` ` .
 Parameters
 coord : list of 4 int / float
 One coordinate .
 Returns
 list of 4 numbers
 New bounding box ."""

implementation:
if len ( coord ) != 4 :
    raise AssertionError ( "coordinate should be 4 values : [x1, y1, x2, y2]" )
x1 , y1 , x2 , y2 = coord
w = x2 - x1
h = y2 - y1
x_c = x1 + w / 2.
y_c = y1 + h / 2.
return [ x_c , y_c , w , h ]


signature:
def obj_box_coord_centroid_to_upleft ( coord ) :
    """Convert one coordinate [ x _ center , y _ center , w , h ] to [ x , y , w , h ] .
 It is the reverse process of ` ` obj _ box _ coord _ upleft _ to _ centroid ` ` .
 Parameters
 coord : list of 4 int / float
 One coordinate .
 Returns
 list of 4 numbers
 New bounding box ."""

implementation:
if len ( coord ) != 4 :
    raise AssertionError ( "coordinate should be 4 values : [x, y, w, h]" )
x_center , y_center , w , h = coord
x = x_center - w / 2.
y = y_center - h / 2.
return [ x , y , w , h ]


signature:
def parse_darknet_ann_str_to_list ( annotations ) :
    r"""Input string format of class , x , y , w , h , return list of list format .
 Parameters
 annotations : str
 The annotations in darkent format " class , x , y , w , h . . . . " seperated by " \ \ n " .
 Returns
 list of list of 4 numbers
 List of bounding box ."""

implementation:
annotations = annotations . split ( "\n" )
ann = [ ]
for a in annotations :
    a = a . split ( )
    if len ( a ) == 5 :
        for i , _v in enumerate ( a ) :
            if i == 0 :
                a [ i ] = int ( a [ i ] )
            else :
                a [ i ] = float ( a [ i ] )
        ann . append ( a )
return ann


signature:
def parse_darknet_ann_list_to_cls_box ( annotations ) :
    """Parse darknet annotation format into two lists for class and bounding box .
 Input list of [ [ class , x , y , w , h ] , . . . ] , return two list of [ class . . . ] and [ [ x , y , w , h ] , . . . ] .
 Parameters
 annotations : list of list
 A list of class and bounding boxes of images e . g . [ [ class , x , y , w , h ] , . . . ]
 Returns
 list of int
 List of class labels .
 list of list of 4 numbers
 List of bounding box ."""

implementation:
class_list = [ ]
bbox_list = [ ]
for ann in annotations :
    class_list . append ( ann [ 0 ] )
    bbox_list . append ( ann [ 1 : ] )
return class_list , bbox_list


signature:
def obj_box_horizontal_flip ( im , coords = None , is_rescale = False , is_center = False , is_random = False ) :
    """Left - right flip the image and coordinates for object detection .
 Parameters
 im : numpy . array
 An image with dimension of [ row , col , channel ] ( default ) .
 coords : list of list of 4 int / float or None
 Coordinates [ [ x , y , w , h ] , [ x , y , w , h ] , . . . ] .
 is _ rescale : boolean
 Set to True , if the input coordinates are rescaled to [ 0 , 1 ] . Default is False .
 is _ center : boolean
 Set to True , if the x and y of coordinates are the centroid ( i . e . darknet format ) . Default is False .
 is _ random : boolean
 If True , randomly flip . Default is False .
 Returns
 numpy . array
 A processed image
 list of list of 4 numbers
 A list of new bounding boxes .
 Examples
 > > > im = np . zeros ( [ 80 , 100 ] ) # as an image with shape width = 100 , height = 80
 > > > im , coords = obj _ box _ left _ right _ flip ( im , coords = [ [ 0.2 , 0.4 , 0.3 , 0.3 ] , [ 0.1 , 0.5 , 0.2 , 0.3 ] ] , is _ rescale = True , is _ center = True , is _ random = False )
 > > > print ( coords )
 [ [ 0.8 , 0.4 , 0.3 , 0.3 ] , [ 0.9 , 0.5 , 0.2 , 0.3 ] ]
 > > > im , coords = obj _ box _ left _ right _ flip ( im , coords = [ [ 0.2 , 0.4 , 0.3 , 0.3 ] ] , is _ rescale = True , is _ center = False , is _ random = False )
 > > > print ( coords )
 [ [ 0.5 , 0.4 , 0.3 , 0.3 ] ]
 > > > im , coords = obj _ box _ left _ right _ flip ( im , coords = [ [ 20 , 40 , 30 , 30 ] ] , is _ rescale = False , is _ center = True , is _ random = False )
 > > > print ( coords )
 [ [ 80 , 40 , 30 , 30 ] ]
 > > > im , coords = obj _ box _ left _ right _ flip ( im , coords = [ [ 20 , 40 , 30 , 30 ] ] , is _ rescale = False , is _ center = False , is _ random = False )
 > > > print ( coords )
 [ [ 50 , 40 , 30 , 30 ] ]"""

implementation:
if coords is None :
    coords = [ ]
def _flip ( im , coords ) :
    im = flip_axis ( im , axis = 1 , is_random = False )
    coords_new = list ( )
    for coord in coords :
        if len ( coord ) != 4 :
            raise AssertionError ( "coordinate should be 4 values : [x, y, w, h]" )
        if is_rescale :
            if is_center : # x _ center ' = 1 - x
                x = 1. - coord [ 0 ]
            else : # x _ center ' = 1 - x - w
                x = 1. - coord [ 0 ] - coord [ 2 ]
        else :
            if is_center : # x ' = im . width - x
                x = im . shape [ 1 ] - coord [ 0 ]
            else : # x ' = im . width - x - w
                x = im . shape [ 1 ] - coord [ 0 ] - coord [ 2 ]
        coords_new . append ( [ x , coord [ 1 ] , coord [ 2 ] , coord [ 3 ] ] )
    return im , coords_new
if is_random :
    factor = np . random . uniform ( - 1 , 1 )
    if factor > 0 :
        return _flip ( im , coords )
    else :
        return im , coords
else :
    return _flip ( im , coords )


signature:
def obj_box_imresize ( im , coords = None , size = None , interp = 'bicubic' , mode = None , is_rescale = False ) :
    """Resize an image , and compute the new bounding box coordinates .
 Parameters
 im : numpy . array
 An image with dimension of [ row , col , channel ] ( default ) .
 coords : list of list of 4 int / float or None
 Coordinates [ [ x , y , w , h ] , [ x , y , w , h ] , . . . ]
 size interp and mode : args
 See ` ` tl . prepro . imresize ` ` .
 is _ rescale : boolean
 Set to True , if the input coordinates are rescaled to [ 0 , 1 ] , then return the original coordinates . Default is False .
 Returns
 numpy . array
 A processed image
 list of list of 4 numbers
 A list of new bounding boxes .
 Examples
 > > > im = np . zeros ( [ 80 , 100 , 3 ] ) # as an image with shape width = 100 , height = 80
 > > > _ , coords = obj _ box _ imresize ( im , coords = [ [ 20 , 40 , 30 , 30 ] , [ 10 , 20 , 20 , 20 ] ] , size = [ 160 , 200 ] , is _ rescale = False )
 > > > print ( coords )
 [ [ 40 , 80 , 60 , 60 ] , [ 20 , 40 , 40 , 40 ] ]
 > > > _ , coords = obj _ box _ imresize ( im , coords = [ [ 20 , 40 , 30 , 30 ] ] , size = [ 40 , 100 ] , is _ rescale = False )
 > > > print ( coords )
 [ [ 20 , 20 , 30 , 15 ] ]
 > > > _ , coords = obj _ box _ imresize ( im , coords = [ [ 20 , 40 , 30 , 30 ] ] , size = [ 60 , 150 ] , is _ rescale = False )
 > > > print ( coords )
 [ [ 30 , 30 , 45 , 22 ] ]
 > > > im2 , coords = obj _ box _ imresize ( im , coords = [ [ 0.2 , 0.4 , 0.3 , 0.3 ] ] , size = [ 160 , 200 ] , is _ rescale = True )
 > > > print ( coords , im2 . shape )
 [ [ 0.2 , 0.4 , 0.3 , 0.3 ] ] ( 160 , 200 , 3)"""

implementation:
if coords is None :
    coords = [ ]
if size is None :
    size = [ 100 , 100 ]
imh , imw = im . shape [ 0 : 2 ]
imh = imh * 1.0
# * 1.0 for python2 : force division to be float point
imw = imw * 1.0
im = imresize ( im , size = size , interp = interp , mode = mode )
if is_rescale is False :
    coords_new = list ( )
    for coord in coords :
        if len ( coord ) != 4 :
            raise AssertionError ( "coordinate should be 4 values : [x, y, w, h]" )
        # x ' = x * ( imw ' / imw )
        x = int ( coord [ 0 ] * ( size [ 1 ] / imw ) )
        # y ' = y * ( imh ' / imh )
        # tl . logging . info ( ' > > ' , coord [ 1 ] , size [ 0 ] , imh )
        y = int ( coord [ 1 ] * ( size [ 0 ] / imh ) )
        # w ' = w * ( imw ' / imw )
        w = int ( coord [ 2 ] * ( size [ 1 ] / imw ) )
        # h ' = h * ( imh ' / imh )
        h = int ( coord [ 3 ] * ( size [ 0 ] / imh ) )
        coords_new . append ( [ x , y , w , h ] )
    return im , coords_new
else :
    return im , coords


signature:
def pad_sequences ( sequences , maxlen = None , dtype = 'int32' , padding = 'post' , truncating = 'pre' , value = 0. ) :
    """Pads each sequence to the same length :
 the length of the longest sequence .
 If maxlen is provided , any sequence longer
 than maxlen is truncated to maxlen .
 Truncation happens off either the beginning ( default ) or
 the end of the sequence .
 Supports post - padding and pre - padding ( default ) .
 Parameters
 sequences : list of list of int
 All sequences where each row is a sequence .
 maxlen : int
 Maximum length .
 dtype : numpy . dtype or str
 Data type to cast the resulting sequence .
 padding : str
 Either ' pre ' or ' post ' , pad either before or after each sequence .
 truncating : str
 Either ' pre ' or ' post ' , remove values from sequences larger than maxlen either in the beginning or in the end of the sequence
 value : float
 Value to pad the sequences to the desired value .
 Returns
 x : numpy . array
 With dimensions ( number _ of _ sequences , maxlen )
 Examples
 > > > sequences = [ [ 1,1,1,1,1 ] , [ 2,2,2 ] , [ 3,3 ] ]
 > > > sequences = pad _ sequences ( sequences , maxlen = None , dtype = ' int32 ' ,
 . . . padding = ' post ' , truncating = ' pre ' , value = 0 . )
 [ [ 1 1 1 1 1]
 [2 2 2 0 0]
 [3 3 0 0 0 ] ]"""

implementation:
lengths = [ len ( s ) for s in sequences ]
nb_samples = len ( sequences )
if maxlen is None :
    maxlen = np . max ( lengths )
# take the sample shape from the first non empty sequence
# checking for consistency in the main loop below .
sample_shape = tuple ( )
for s in sequences :
    if len ( s ) > 0 :
        sample_shape = np . asarray ( s ) . shape [ 1 : ]
        break
x = ( np . ones ( ( nb_samples , maxlen ) + sample_shape ) * value ) . astype ( dtype )
for idx , s in enumerate ( sequences ) :
    if len ( s ) == 0 :
        continue
        # empty list was found
    if truncating == 'pre' :
        trunc = s [ - maxlen : ]
    elif truncating == 'post' :
        trunc = s [ : maxlen ]
    else :
        raise ValueError ( 'Truncating type "%s" not understood' % truncating )
    # check ` trunc ` has expected shape
    trunc = np . asarray ( trunc , dtype = dtype )
    if trunc . shape [ 1 : ] != sample_shape :
        raise ValueError ( 'Shape of sample %s of sequence at position %s is different from expected shape %s' % ( trunc . shape [ 1 : ] , idx , sample_shape ) )
    if padding == 'post' :
        x [ idx , : len ( trunc ) ] = trunc
    elif padding == 'pre' :
        x [ idx , - len ( trunc ) : ] = trunc
    else :
        raise ValueError ( 'Padding type "%s" not understood' % padding )
return x . tolist ( )


signature:
def remove_pad_sequences ( sequences , pad_id = 0 ) :
    """Remove padding .
 Parameters
 sequences : list of list of int
 All sequences where each row is a sequence .
 pad _ id : int
 The pad ID .
 Returns
 list of list of int
 The processed sequences .
 Examples
 > > > sequences = [ [ 2,3,4,0,0 ] , [ 5,1,2,3,4,0,0,0 ] , [ 4,5,0,2,4,0,0,0 ] ]
 > > > print ( remove _ pad _ sequences ( sequences , pad _ id = 0 ) )
 [ [ 2 , 3 , 4 ] , [ 5 , 1 , 2 , 3 , 4 ] , [ 4 , 5 , 0 , 2 , 4 ] ]"""

implementation:
sequences_out = copy . deepcopy ( sequences )
for i , _ in enumerate ( sequences ) : # for j in range ( len ( sequences [ i ] ) ) :
# if sequences [ i ] [ j ] = = pad _ id :
# sequences _ out [ i ] = sequences _ out [ i ] [ : j ]
# break
    for j in range ( 1 , len ( sequences [ i ] ) ) :
        if sequences [ i ] [ - j ] != pad_id :
            sequences_out [ i ] = sequences_out [ i ] [ 0 : - j + 1 ]
            break
return sequences_out


signature:
def process_sequences ( sequences , end_id = 0 , pad_val = 0 , is_shorten = True , remain_end_id = False ) :
    """Set all tokens ( ids ) after END token to the padding value , and then shorten ( option ) it to the maximum sequence length in this batch .
 Parameters
 sequences : list of list of int
 All sequences where each row is a sequence .
 end _ id : int
 The special token for END .
 pad _ val : int
 Replace the ` end _ id ` and the IDs after ` end _ id ` to this value .
 is _ shorten : boolean
 Shorten the sequences . Default is True .
 remain _ end _ id : boolean
 Keep an ` end _ id ` in the end . Default is False .
 Returns
 list of list of int
 The processed sequences .
 Examples
 > > > sentences _ ids = [ [ 4 , 3 , 5 , 3 , 2 , 2 , 2 , 2 ] , < - - end _ id is 2
 . . . [ 5 , 3 , 9 , 4 , 9 , 2 , 2 , 3 ] ] < - - end _ id is 2
 > > > sentences _ ids = precess _ sequences ( sentences _ ids , end _ id = vocab . end _ id , pad _ val = 0 , is _ shorten = True )
 [ [ 4 , 3 , 5 , 3 , 0 ] , [ 5 , 3 , 9 , 4 , 9 ] ]"""

implementation:
max_length = 0
for _ , seq in enumerate ( sequences ) :
    is_end = False
    for i_w , n in enumerate ( seq ) :
        if n == end_id and is_end == False : # 1st time to see end _ id
            is_end = True
            if max_length < i_w :
                max_length = i_w
            if remain_end_id is False :
                seq [ i_w ] = pad_val
                # set end _ id to pad _ val
        elif is_end == True :
            seq [ i_w ] = pad_val
if remain_end_id is True :
    max_length += 1
if is_shorten :
    for i , seq in enumerate ( sequences ) :
        sequences [ i ] = seq [ : max_length ]
return sequences


signature:
def sequences_add_start_id ( sequences , start_id = 0 , remove_last = False ) :
    """Add special start token ( id ) in the beginning of each sequence .
 Parameters
 sequences : list of list of int
 All sequences where each row is a sequence .
 start _ id : int
 The start ID .
 remove _ last : boolean
 Remove the last value of each sequences . Usually be used for removing the end ID .
 Returns
 list of list of int
 The processed sequences .
 Examples
 > > > sentences _ ids = [ [ 4,3,5,3,2,2,2,2 ] , [ 5,3,9,4,9,2,2,3 ] ]
 > > > sentences _ ids = sequences _ add _ start _ id ( sentences _ ids , start _ id = 2)
 [ [ 2 , 4 , 3 , 5 , 3 , 2 , 2 , 2 , 2 ] , [ 2 , 5 , 3 , 9 , 4 , 9 , 2 , 2 , 3 ] ]
 > > > sentences _ ids = sequences _ add _ start _ id ( sentences _ ids , start _ id = 2 , remove _ last = True )
 [ [ 2 , 4 , 3 , 5 , 3 , 2 , 2 , 2 ] , [ 2 , 5 , 3 , 9 , 4 , 9 , 2 , 2 ] ]
 For Seq2seq
 > > > input = [ a , b , c ]
 > > > target = [ x , y , z ]
 > > > decode _ seq = [ start _ id , a , b ] < - - sequences _ add _ start _ id ( input , start _ id , True )"""

implementation:
sequences_out = [ [ ] for _ in range ( len ( sequences ) ) ]
# [ [ ] ] * len ( sequences )
for i , _ in enumerate ( sequences ) :
    if remove_last :
        sequences_out [ i ] = [ start_id ] + sequences [ i ] [ : - 1 ]
    else :
        sequences_out [ i ] = [ start_id ] + sequences [ i ]
return sequences_out


signature:
def sequences_add_end_id ( sequences , end_id = 888 ) :
    """Add special end token ( id ) in the end of each sequence .
 Parameters
 sequences : list of list of int
 All sequences where each row is a sequence .
 end _ id : int
 The end ID .
 Returns
 list of list of int
 The processed sequences .
 Examples
 > > > sequences = [ [ 1,2,3 ] , [ 4,5,6,7 ] ]
 > > > print ( sequences _ add _ end _ id ( sequences , end _ id = 999 ) )
 [ [ 1 , 2 , 3 , 999 ] , [ 4 , 5 , 6 , 999 ] ]"""

implementation:
sequences_out = [ [ ] for _ in range ( len ( sequences ) ) ]
# [ [ ] ] * len ( sequences )
for i , _ in enumerate ( sequences ) :
    sequences_out [ i ] = sequences [ i ] + [ end_id ]
return sequences_out


signature:
def sequences_add_end_id_after_pad ( sequences , end_id = 888 , pad_id = 0 ) :
    """Add special end token ( id ) in the end of each sequence .
 Parameters
 sequences : list of list of int
 All sequences where each row is a sequence .
 end _ id : int
 The end ID .
 pad _ id : int
 The pad ID .
 Returns
 list of list of int
 The processed sequences .
 Examples
 > > > sequences = [ [ 1,2,0,0 ] , [ 1,2,3,0 ] , [ 1,2,3,4 ] ]
 > > > print ( sequences _ add _ end _ id _ after _ pad ( sequences , end _ id = 99 , pad _ id = 0 ) )
 [ [ 1 , 2 , 99 , 0 ] , [ 1 , 2 , 3 , 99 ] , [ 1 , 2 , 3 , 4 ] ]"""

implementation:
# sequences _ out = [ [ ] for _ in range ( len ( sequences ) ) ] # [ [ ] ] * len ( sequences )
sequences_out = copy . deepcopy ( sequences )
# # add a pad to all
# for i in range ( len ( sequences ) ) :
# for j in range ( len ( sequences [ i ] ) ) :
# sequences _ out [ i ] . append ( pad _ id )
# # pad - - > end
# max _ len = 0
for i , v in enumerate ( sequences ) :
    for j , _v2 in enumerate ( v ) :
        if sequences [ i ] [ j ] == pad_id :
            sequences_out [ i ] [ j ] = end_id
            # if j > max _ len :
            # max _ len = j
            break
# # remove pad if too long
# for i in range ( len ( sequences ) ) :
# for j in range ( len ( sequences [ i ] ) ) :
# sequences _ out [ i ] = sequences _ out [ i ] [ : max _ len + 1]
return sequences_out


signature:
def sequences_get_mask ( sequences , pad_val = 0 ) :
    """Return mask for sequences .
 Parameters
 sequences : list of list of int
 All sequences where each row is a sequence .
 pad _ val : int
 The pad value .
 Returns
 list of list of int
 The mask .
 Examples
 > > > sentences _ ids = [ [ 4 , 0 , 5 , 3 , 0 , 0 ] ,
 . . . [ 5 , 3 , 9 , 4 , 9 , 0 ] ]
 > > > mask = sequences _ get _ mask ( sentences _ ids , pad _ val = 0)
 [ [ 1 1 1 1 0 0]
 [1 1 1 1 1 0 ] ]"""

implementation:
mask = np . ones_like ( sequences )
for i , seq in enumerate ( sequences ) :
    for i_w in reversed ( range ( len ( seq ) ) ) :
        if seq [ i_w ] == pad_val :
            mask [ i , i_w ] = 0
        else :
            break
            # < - - exit the for loop , prepcess next sequence
return mask


signature:
def keypoint_random_crop ( image , annos , mask = None , size = ( 368 , 368 ) ) :
    """Randomly crop an image and corresponding keypoints without influence scales , given by ` ` keypoint _ random _ resize _ shortestedge ` ` .
 Parameters
 image : 3 channel image
 The given image for augmentation .
 annos : list of list of floats
 The keypoints annotation of people .
 mask : single channel image or None
 The mask if available .
 size : tuple of int
 The size of returned image .
 Returns
 preprocessed image , annotation , mask"""

implementation:
_target_height = size [ 0 ]
_target_width = size [ 1 ]
target_size = ( _target_width , _target_height )
if len ( np . shape ( image ) ) == 2 :
    image = cv2 . cvtColor ( image , cv2 . COLOR_GRAY2RGB )
height , width , _ = np . shape ( image )
for _ in range ( 50 ) :
    x = random . randrange ( 0 , width - target_size [ 0 ] ) if width > target_size [ 0 ] else 0
    y = random . randrange ( 0 , height - target_size [ 1 ] ) if height > target_size [ 1 ] else 0
    # check whether any face is inside the box to generate a reasonably - balanced datasets
    for joint in annos :
        if x <= joint [ 0 ] [ 0 ] < x + target_size [ 0 ] and y <= joint [ 0 ] [ 1 ] < y + target_size [ 1 ] :
            break
def pose_crop ( image , annos , mask , x , y , w , h ) : # TODO : speed up with affine transform
# adjust image
    target_size = ( w , h )
    img = image
    resized = img [ y : y + target_size [ 1 ] , x : x + target_size [ 0 ] , : ]
    resized_mask = mask [ y : y + target_size [ 1 ] , x : x + target_size [ 0 ] ]
    # adjust meta data
    adjust_joint_list = [ ]
    for joint in annos :
        adjust_joint = [ ]
        for point in joint :
            if point [ 0 ] < - 10 or point [ 1 ] < - 10 :
                adjust_joint . append ( ( - 1000 , - 1000 ) )
                continue
            new_x , new_y = point [ 0 ] - x , point [ 1 ] - y
            # should not crop outside the image
            if new_x > w - 1 or new_y > h - 1 :
                adjust_joint . append ( ( - 1000 , - 1000 ) )
                continue
            adjust_joint . append ( ( new_x , new_y ) )
        adjust_joint_list . append ( adjust_joint )
    return resized , adjust_joint_list , resized_mask
return pose_crop ( image , annos , mask , x , y , target_size [ 0 ] , target_size [ 1 ] )


signature:
def keypoint_random_flip ( image , annos , mask = None , prob = 0.5 , flip_list = ( 0 , 1 , 5 , 6 , 7 , 2 , 3 , 4 , 11 , 12 , 13 , 8 , 9 , 10 , 15 , 14 , 17 , 16 , 18 ) ) :
    """Flip an image and corresponding keypoints .
 Parameters
 image : 3 channel image
 The given image for augmentation .
 annos : list of list of floats
 The keypoints annotation of people .
 mask : single channel image or None
 The mask if available .
 prob : float , 0 to 1
 The probability to flip the image , if 1 , always flip the image .
 flip _ list : tuple of int
 Denotes how the keypoints number be changed after flipping which is required for pose estimation task .
 The left and right body should be maintained rather than switch .
 ( Default COCO format ) .
 Set to an empty tuple if you don ' t need to maintain left and right information .
 Returns
 preprocessed image , annos , mask"""

implementation:
_prob = np . random . uniform ( 0 , 1.0 )
if _prob < prob :
    return image , annos , mask
_ , width , _ = np . shape ( image )
image = cv2 . flip ( image , 1 )
mask = cv2 . flip ( mask , 1 )
new_joints = [ ]
for people in annos : # TODO : speed up with affine transform
    new_keypoints = [ ]
    for k in flip_list :
        point = people [ k ]
        if point [ 0 ] < 0 or point [ 1 ] < 0 :
            new_keypoints . append ( ( - 1000 , - 1000 ) )
            continue
        if point [ 0 ] > image . shape [ 1 ] - 1 or point [ 1 ] > image . shape [ 0 ] - 1 :
            new_keypoints . append ( ( - 1000 , - 1000 ) )
            continue
        if ( width - point [ 0 ] ) > image . shape [ 1 ] - 1 :
            new_keypoints . append ( ( - 1000 , - 1000 ) )
            continue
        new_keypoints . append ( ( width - point [ 0 ] , point [ 1 ] ) )
    new_joints . append ( new_keypoints )
annos = new_joints
return image , annos , mask


signature:
def keypoint_random_resize ( image , annos , mask = None , zoom_range = ( 0.8 , 1.2 ) ) :
    """Randomly resize an image and corresponding keypoints .
 The height and width of image will be changed independently , so the scale will be changed .
 Parameters
 image : 3 channel image
 The given image for augmentation .
 annos : list of list of floats
 The keypoints annotation of people .
 mask : single channel image or None
 The mask if available .
 zoom _ range : tuple of two floats
 The minimum and maximum factor to zoom in or out , e . g ( 0.5 , 1 ) means zoom out 1 ~ 2 times .
 Returns
 preprocessed image , annos , mask"""

implementation:
height = image . shape [ 0 ]
width = image . shape [ 1 ]
_min , _max = zoom_range
scalew = np . random . uniform ( _min , _max )
scaleh = np . random . uniform ( _min , _max )
neww = int ( width * scalew )
newh = int ( height * scaleh )
dst = cv2 . resize ( image , ( neww , newh ) , interpolation = cv2 . INTER_AREA )
if mask is not None :
    mask = cv2 . resize ( mask , ( neww , newh ) , interpolation = cv2 . INTER_AREA )
# adjust meta data
adjust_joint_list = [ ]
for joint in annos : # TODO : speed up with affine transform
    adjust_joint = [ ]
    for point in joint :
        if point [ 0 ] < - 100 or point [ 1 ] < - 100 :
            adjust_joint . append ( ( - 1000 , - 1000 ) )
            continue
        adjust_joint . append ( ( int ( point [ 0 ] * scalew + 0.5 ) , int ( point [ 1 ] * scaleh + 0.5 ) ) )
    adjust_joint_list . append ( adjust_joint )
if mask is not None :
    return dst , adjust_joint_list , mask
else :
    return dst , adjust_joint_list , None


signature:
def Vgg19 ( rgb ) :
    """Build the VGG 19 Model
 Parameters
 rgb : rgb image placeholder [ batch , height , width , 3 ] values scaled [ 0 , 1]"""

implementation:
start_time = time . time ( )
print ( "build model started" )
rgb_scaled = rgb * 255.0
# Convert RGB to BGR
red , green , blue = tf . split ( rgb_scaled , 3 , 3 )
if red . get_shape ( ) . as_list ( ) [ 1 : ] != [ 224 , 224 , 1 ] :
    raise Exception ( "image size unmatch" )
if green . get_shape ( ) . as_list ( ) [ 1 : ] != [ 224 , 224 , 1 ] :
    raise Exception ( "image size unmatch" )
if blue . get_shape ( ) . as_list ( ) [ 1 : ] != [ 224 , 224 , 1 ] :
    raise Exception ( "image size unmatch" )
bgr = tf . concat ( [ blue - VGG_MEAN [ 0 ] , green - VGG_MEAN [ 1 ] , red - VGG_MEAN [ 2 ] , ] , axis = 3 )
if bgr . get_shape ( ) . as_list ( ) [ 1 : ] != [ 224 , 224 , 3 ] :
    raise Exception ( "image size unmatch" )
# input layer
net_in = InputLayer ( bgr , name = 'input' )
# conv1
net = Conv2dLayer ( net_in , act = tf . nn . relu , shape = [ 3 , 3 , 3 , 64 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv1_1' )
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 64 , 64 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv1_2' )
net = PoolLayer ( net , ksize = [ 1 , 2 , 2 , 1 ] , strides = [ 1 , 2 , 2 , 1 ] , padding = 'SAME' , pool = tf . nn . max_pool , name = 'pool1' )
# conv2
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 64 , 128 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv2_1' )
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 128 , 128 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv2_2' )
net = PoolLayer ( net , ksize = [ 1 , 2 , 2 , 1 ] , strides = [ 1 , 2 , 2 , 1 ] , padding = 'SAME' , pool = tf . nn . max_pool , name = 'pool2' )
# conv3
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 128 , 256 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv3_1' )
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 256 , 256 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv3_2' )
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 256 , 256 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv3_3' )
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 256 , 256 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv3_4' )
net = PoolLayer ( net , ksize = [ 1 , 2 , 2 , 1 ] , strides = [ 1 , 2 , 2 , 1 ] , padding = 'SAME' , pool = tf . nn . max_pool , name = 'pool3' )
# conv4
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 256 , 512 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv4_1' )
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 512 , 512 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv4_2' )
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 512 , 512 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv4_3' )
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 512 , 512 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv4_4' )
net = PoolLayer ( net , ksize = [ 1 , 2 , 2 , 1 ] , strides = [ 1 , 2 , 2 , 1 ] , padding = 'SAME' , pool = tf . nn . max_pool , name = 'pool4' )
# conv5
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 512 , 512 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv5_1' )
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 512 , 512 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv5_2' )
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 512 , 512 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv5_3' )
net = Conv2dLayer ( net , act = tf . nn . relu , shape = [ 3 , 3 , 512 , 512 ] , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' , name = 'conv5_4' )
net = PoolLayer ( net , ksize = [ 1 , 2 , 2 , 1 ] , strides = [ 1 , 2 , 2 , 1 ] , padding = 'SAME' , pool = tf . nn . max_pool , name = 'pool5' )
# fc 6 ~ 8
net = FlattenLayer ( net , name = 'flatten' )
net = DenseLayer ( net , n_units = 4096 , act = tf . nn . relu , name = 'fc6' )
net = DenseLayer ( net , n_units = 4096 , act = tf . nn . relu , name = 'fc7' )
net = DenseLayer ( net , n_units = 1000 , act = None , name = 'fc8' )
print ( "build model finished: %fs" % ( time . time ( ) - start_time ) )
return net


signature:
def Vgg19_simple_api ( rgb ) :
    """Build the VGG 19 Model
 Parameters
 rgb : rgb image placeholder [ batch , height , width , 3 ] values scaled [ 0 , 1]"""

implementation:
start_time = time . time ( )
print ( "build model started" )
rgb_scaled = rgb * 255.0
# Convert RGB to BGR
red , green , blue = tf . split ( rgb_scaled , 3 , 3 )
if red . get_shape ( ) . as_list ( ) [ 1 : ] != [ 224 , 224 , 1 ] :
    raise Exception ( "image size unmatch" )
if green . get_shape ( ) . as_list ( ) [ 1 : ] != [ 224 , 224 , 1 ] :
    raise Exception ( "image size unmatch" )
if blue . get_shape ( ) . as_list ( ) [ 1 : ] != [ 224 , 224 , 1 ] :
    raise Exception ( "image size unmatch" )
bgr = tf . concat ( [ blue - VGG_MEAN [ 0 ] , green - VGG_MEAN [ 1 ] , red - VGG_MEAN [ 2 ] , ] , axis = 3 )
if bgr . get_shape ( ) . as_list ( ) [ 1 : ] != [ 224 , 224 , 3 ] :
    raise Exception ( "image size unmatch" )
# input layer
net_in = InputLayer ( bgr , name = 'input' )
# conv1
net = Conv2d ( net_in , 64 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv1_1' )
net = Conv2d ( net , n_filter = 64 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv1_2' )
net = MaxPool2d ( net , filter_size = ( 2 , 2 ) , strides = ( 2 , 2 ) , padding = 'SAME' , name = 'pool1' )
# conv2
net = Conv2d ( net , n_filter = 128 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv2_1' )
net = Conv2d ( net , n_filter = 128 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv2_2' )
net = MaxPool2d ( net , filter_size = ( 2 , 2 ) , strides = ( 2 , 2 ) , padding = 'SAME' , name = 'pool2' )
# conv3
net = Conv2d ( net , n_filter = 256 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv3_1' )
net = Conv2d ( net , n_filter = 256 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv3_2' )
net = Conv2d ( net , n_filter = 256 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv3_3' )
net = Conv2d ( net , n_filter = 256 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv3_4' )
net = MaxPool2d ( net , filter_size = ( 2 , 2 ) , strides = ( 2 , 2 ) , padding = 'SAME' , name = 'pool3' )
# conv4
net = Conv2d ( net , n_filter = 512 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv4_1' )
net = Conv2d ( net , n_filter = 512 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv4_2' )
net = Conv2d ( net , n_filter = 512 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv4_3' )
net = Conv2d ( net , n_filter = 512 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv4_4' )
net = MaxPool2d ( net , filter_size = ( 2 , 2 ) , strides = ( 2 , 2 ) , padding = 'SAME' , name = 'pool4' )
# conv5
net = Conv2d ( net , n_filter = 512 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv5_1' )
net = Conv2d ( net , n_filter = 512 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv5_2' )
net = Conv2d ( net , n_filter = 512 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv5_3' )
net = Conv2d ( net , n_filter = 512 , filter_size = ( 3 , 3 ) , strides = ( 1 , 1 ) , act = tf . nn . relu , padding = 'SAME' , name = 'conv5_4' )
net = MaxPool2d ( net , filter_size = ( 2 , 2 ) , strides = ( 2 , 2 ) , padding = 'SAME' , name = 'pool5' )
# fc 6 ~ 8
net = FlattenLayer ( net , name = 'flatten' )
net = DenseLayer ( net , n_units = 4096 , act = tf . nn . relu , name = 'fc6' )
net = DenseLayer ( net , n_units = 4096 , act = tf . nn . relu , name = 'fc7' )
net = DenseLayer ( net , n_units = 1000 , act = None , name = 'fc8' )
print ( "build model finished: %fs" % ( time . time ( ) - start_time ) )
return net


signature:
def prepro ( I ) :
    """Prepro 210x160x3 uint8 frame into 6400 ( 80x80 ) 1D float vector ."""


signature:
def discount_episode_rewards ( rewards = None , gamma = 0.99 , mode = 0 ) :
    """Take 1D float array of rewards and compute discounted rewards for an
 episode . When encount a non - zero value , consider as the end a of an episode .
 Parameters
 rewards : list
 List of rewards
 gamma : float
 Discounted factor
 mode : int
 Mode for computing the discount rewards .
 - If mode = = 0 , reset the discount process when encount a non - zero reward ( Ping - pong game ) .
 - If mode = = 1 , would not reset the discount process .
 Returns
 list of float
 The discounted rewards .
 Examples
 > > > rewards = np . asarray ( [ 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 ] )
 > > > gamma = 0.9
 > > > discount _ rewards = tl . rein . discount _ episode _ rewards ( rewards , gamma )
 > > > print ( discount _ rewards )
 [ 0.72899997 0.81 0.8999998 1 . 0.72899997 0.81
 0.8999998 1 . 0.72899997 0.81 0.8999998 1 . ]
 > > > discount _ rewards = tl . rein . discount _ episode _ rewards ( rewards , gamma , mode = 1)
 > > > print ( discount _ rewards )
 [ 1.52110755 1.69011939 1.87791049 2.08656716 1.20729685 1.34144104
 1.49048996 1.65610003 0.72899997 0.81 0.8999998 1 . ]"""

implementation:
if rewards is None :
    raise Exception ( "rewards should be a list" )
discounted_r = np . zeros_like ( rewards , dtype = np . float32 )
running_add = 0
for t in reversed ( xrange ( 0 , rewards . size ) ) :
    if mode == 0 :
        if rewards [ t ] != 0 :
            running_add = 0
    running_add = running_add * gamma + rewards [ t ]
    discounted_r [ t ] = running_add
return discounted_r


signature:
def cross_entropy_reward_loss ( logits , actions , rewards , name = None ) :
    """Calculate the loss for Policy Gradient Network .
 Parameters
 logits : tensor
 The network outputs without softmax . This function implements softmax inside .
 actions : tensor or placeholder
 The agent actions .
 rewards : tensor or placeholder
 The rewards .
 Returns
 Tensor
 The TensorFlow loss function .
 Examples
 > > > states _ batch _ pl = tf . placeholder ( tf . float32 , shape = [ None , D ] )
 > > > network = InputLayer ( states _ batch _ pl , name = ' input ' )
 > > > network = DenseLayer ( network , n _ units = H , act = tf . nn . relu , name = ' relu1 ' )
 > > > network = DenseLayer ( network , n _ units = 3 , name = ' out ' )
 > > > probs = network . outputs
 > > > sampling _ prob = tf . nn . softmax ( probs )
 > > > actions _ batch _ pl = tf . placeholder ( tf . int32 , shape = [ None ] )
 > > > discount _ rewards _ batch _ pl = tf . placeholder ( tf . float32 , shape = [ None ] )
 > > > loss = tl . rein . cross _ entropy _ reward _ loss ( probs , actions _ batch _ pl , discount _ rewards _ batch _ pl )
 > > > train _ op = tf . train . RMSPropOptimizer ( learning _ rate , decay _ rate ) . minimize ( loss )"""

implementation:
cross_entropy = tf . nn . sparse_softmax_cross_entropy_with_logits ( labels = actions , logits = logits , name = name )
return tf . reduce_sum ( tf . multiply ( cross_entropy , rewards ) )


signature:
def log_weight ( probs , weights , name = 'log_weight' ) :
    """Log weight .
 Parameters
 probs : tensor
 If it is a network output , usually we should scale it to [ 0 , 1 ] via softmax .
 weights : tensor
 The weights .
 Returns
 Tensor
 The Tensor after appling the log weighted expression ."""

implementation:
with tf . variable_scope ( name ) :
    exp_v = tf . reduce_mean ( tf . log ( probs ) * weights )
    return exp_v


signature:
def choice_action_by_probs ( probs = ( 0.5 , 0.5 ) , action_list = None ) :
    """Choice and return an an action by given the action probability distribution .
 Parameters
 probs : list of float .
 The probability distribution of all actions .
 action _ list : None or a list of int or others
 A list of action in integer , string or others . If None , returns an integer range between 0 and len ( probs ) - 1.
 Returns
 float int or str
 The chosen action .
 Examples
 > > > for _ in range ( 5 ) :
 > > > a = choice _ action _ by _ probs ( [ 0.2 , 0.4 , 0.4 ] )
 > > > print ( a )
 > > > for _ in range ( 3 ) :
 > > > a = choice _ action _ by _ probs ( [ 0.5 , 0.5 ] , [ ' a ' , ' b ' ] )
 > > > print ( a )"""

implementation:
if action_list is None :
    n_action = len ( probs )
    action_list = np . arange ( n_action )
else :
    if len ( action_list ) != len ( probs ) :
        raise Exception ( "number of actions should equal to number of probabilities." )
return np . random . choice ( action_list , p = probs )


signature:
def cross_entropy ( output , target , name = None ) :
    """Softmax cross - entropy operation , returns the TensorFlow expression of cross - entropy for two distributions ,
 it implements softmax internally . See ` ` tf . nn . sparse _ softmax _ cross _ entropy _ with _ logits ` ` .
 Parameters
 output : Tensor
 A batch of distribution with shape : [ batch _ size , num of classes ] .
 target : Tensor
 A batch of index with shape : [ batch _ size , ] .
 name : string
 Name of this loss .
 Examples
 > > > ce = tl . cost . cross _ entropy ( y _ logits , y _ target _ logits , ' my _ loss ' )
 References
 - About cross - entropy : ` < https : / / en . wikipedia . org / wiki / Cross _ entropy > ` _ _ .
 - The code is borrowed from : ` < https : / / en . wikipedia . org / wiki / Cross _ entropy > ` _ _ ."""

implementation:
if name is None :
    raise Exception ( "Please give a unique name to tl.cost.cross_entropy for TF1.0+" )
return tf . reduce_mean ( tf . nn . sparse_softmax_cross_entropy_with_logits ( labels = target , logits = output ) , name = name )


signature:
def sigmoid_cross_entropy ( output , target , name = None ) :
    """Sigmoid cross - entropy operation , see ` ` tf . nn . sigmoid _ cross _ entropy _ with _ logits ` ` .
 Parameters
 output : Tensor
 A batch of distribution with shape : [ batch _ size , num of classes ] .
 target : Tensor
 A batch of index with shape : [ batch _ size , ] .
 name : string
 Name of this loss ."""

implementation: return tf . reduce_mean ( tf . nn . sigmoid_cross_entropy_with_logits ( labels = target , logits = output ) , name = name )


signature:
def binary_cross_entropy ( output , target , epsilon = 1e-8 , name = 'bce_loss' ) :
    """Binary cross entropy operation .
 Parameters
 output : Tensor
 Tensor with type of ` float32 ` or ` float64 ` .
 target : Tensor
 The target distribution , format the same with ` output ` .
 epsilon : float
 A small value to avoid output to be zero .
 name : str
 An optional name to attach to this function .
 References
 - ` ericjang - DRAW < https : / / github . com / ericjang / draw / blob / master / draw . py # L73 > ` _ _"""

implementation:
# with ops . op _ scope ( [ output , target ] , name , " bce _ loss " ) as name :
# output = ops . convert _ to _ tensor ( output , name = " preds " )
# target = ops . convert _ to _ tensor ( targets , name = " target " )
# with tf . name _ scope ( name ) :
return tf . reduce_mean ( tf . reduce_sum ( - ( target * tf . log ( output + epsilon ) + ( 1. - target ) * tf . log ( 1. - output + epsilon ) ) , axis = 1 ) , name = name )


signature:
def mean_squared_error ( output , target , is_mean = False , name = "mean_squared_error" ) :
    """Return the TensorFlow expression of mean - square - error ( L2 ) of two batch of data .
 Parameters
 output : Tensor
 2D , 3D or 4D tensor i . e . [ batch _ size , n _ feature ] , [ batch _ size , height , width ] or [ batch _ size , height , width , channel ] .
 target : Tensor
 The target distribution , format the same with ` output ` .
 is _ mean : boolean
 Whether compute the mean or sum for each example .
 - If True , use ` ` tf . reduce _ mean ` ` to compute the loss between one target and predict data .
 - If False , use ` ` tf . reduce _ sum ` ` ( default ) .
 name : str
 An optional name to attach to this function .
 References
 - ` Wiki Mean Squared Error < https : / / en . wikipedia . org / wiki / Mean _ squared _ error > ` _ _"""

implementation:
# with tf . name _ scope ( name ) :
if output . get_shape ( ) . ndims == 2 : # [ batch _ size , n _ feature ]
    if is_mean :
        mse = tf . reduce_mean ( tf . reduce_mean ( tf . squared_difference ( output , target ) , 1 ) , name = name )
    else :
        mse = tf . reduce_mean ( tf . reduce_sum ( tf . squared_difference ( output , target ) , 1 ) , name = name )
elif output . get_shape ( ) . ndims == 3 : # [ batch _ size , w , h ]
    if is_mean :
        mse = tf . reduce_mean ( tf . reduce_mean ( tf . squared_difference ( output , target ) , [ 1 , 2 ] ) , name = name )
    else :
        mse = tf . reduce_mean ( tf . reduce_sum ( tf . squared_difference ( output , target ) , [ 1 , 2 ] ) , name = name )
elif output . get_shape ( ) . ndims == 4 : # [ batch _ size , w , h , c ]
    if is_mean :
        mse = tf . reduce_mean ( tf . reduce_mean ( tf . squared_difference ( output , target ) , [ 1 , 2 , 3 ] ) , name = name )
    else :
        mse = tf . reduce_mean ( tf . reduce_sum ( tf . squared_difference ( output , target ) , [ 1 , 2 , 3 ] ) , name = name )
else :
    raise Exception ( "Unknow dimension" )
return mse


signature:
def normalized_mean_square_error ( output , target , name = "normalized_mean_squared_error_loss" ) :
    """Return the TensorFlow expression of normalized mean - square - error of two distributions .
 Parameters
 output : Tensor
 2D , 3D or 4D tensor i . e . [ batch _ size , n _ feature ] , [ batch _ size , height , width ] or [ batch _ size , height , width , channel ] .
 target : Tensor
 The target distribution , format the same with ` output ` .
 name : str
 An optional name to attach to this function ."""

implementation:
# with tf . name _ scope ( " normalized _ mean _ squared _ error _ loss " ) :
if output . get_shape ( ) . ndims == 2 : # [ batch _ size , n _ feature ]
    nmse_a = tf . sqrt ( tf . reduce_sum ( tf . squared_difference ( output , target ) , axis = 1 ) )
    nmse_b = tf . sqrt ( tf . reduce_sum ( tf . square ( target ) , axis = 1 ) )
elif output . get_shape ( ) . ndims == 3 : # [ batch _ size , w , h ]
    nmse_a = tf . sqrt ( tf . reduce_sum ( tf . squared_difference ( output , target ) , axis = [ 1 , 2 ] ) )
    nmse_b = tf . sqrt ( tf . reduce_sum ( tf . square ( target ) , axis = [ 1 , 2 ] ) )
elif output . get_shape ( ) . ndims == 4 : # [ batch _ size , w , h , c ]
    nmse_a = tf . sqrt ( tf . reduce_sum ( tf . squared_difference ( output , target ) , axis = [ 1 , 2 , 3 ] ) )
    nmse_b = tf . sqrt ( tf . reduce_sum ( tf . square ( target ) , axis = [ 1 , 2 , 3 ] ) )
nmse = tf . reduce_mean ( nmse_a / nmse_b , name = name )
return nmse


signature:
def absolute_difference_error ( output , target , is_mean = False , name = "absolute_difference_error_loss" ) :
    """Return the TensorFlow expression of absolute difference error ( L1 ) of two batch of data .
 Parameters
 output : Tensor
 2D , 3D or 4D tensor i . e . [ batch _ size , n _ feature ] , [ batch _ size , height , width ] or [ batch _ size , height , width , channel ] .
 target : Tensor
 The target distribution , format the same with ` output ` .
 is _ mean : boolean
 Whether compute the mean or sum for each example .
 - If True , use ` ` tf . reduce _ mean ` ` to compute the loss between one target and predict data .
 - If False , use ` ` tf . reduce _ sum ` ` ( default ) .
 name : str
 An optional name to attach to this function ."""

implementation:
# with tf . name _ scope ( " absolute _ difference _ error _ loss " ) :
if output . get_shape ( ) . ndims == 2 : # [ batch _ size , n _ feature ]
    if is_mean :
        loss = tf . reduce_mean ( tf . reduce_mean ( tf . abs ( output - target ) , 1 ) , name = name )
    else :
        loss = tf . reduce_mean ( tf . reduce_sum ( tf . abs ( output - target ) , 1 ) , name = name )
elif output . get_shape ( ) . ndims == 3 : # [ batch _ size , w , h ]
    if is_mean :
        loss = tf . reduce_mean ( tf . reduce_mean ( tf . abs ( output - target ) , [ 1 , 2 ] ) , name = name )
    else :
        loss = tf . reduce_mean ( tf . reduce_sum ( tf . abs ( output - target ) , [ 1 , 2 ] ) , name = name )
elif output . get_shape ( ) . ndims == 4 : # [ batch _ size , w , h , c ]
    if is_mean :
        loss = tf . reduce_mean ( tf . reduce_mean ( tf . abs ( output - target ) , [ 1 , 2 , 3 ] ) , name = name )
    else :
        loss = tf . reduce_mean ( tf . reduce_sum ( tf . abs ( output - target ) , [ 1 , 2 , 3 ] ) , name = name )
else :
    raise Exception ( "Unknow dimension" )
return loss


signature:
def dice_coe ( output , target , loss_type = 'jaccard' , axis = ( 1 , 2 , 3 ) , smooth = 1e-5 ) :
    """Soft dice ( Sørensen or Jaccard ) coefficient for comparing the similarity
 of two batch of data , usually be used for binary image segmentation
 i . e . labels are binary . The coefficient between 0 to 1 , 1 means totally match .
 Parameters
 output : Tensor
 A distribution with shape : [ batch _ size , . . . . ] , ( any dimensions ) .
 target : Tensor
 The target distribution , format the same with ` output ` .
 loss _ type : str
 ` ` jaccard ` ` or ` ` sorensen ` ` , default is ` ` jaccard ` ` .
 axis : tuple of int
 All dimensions are reduced , default ` ` [ 1,2,3 ] ` ` .
 smooth : float
 This small value will be added to the numerator and denominator .
 - If both output and target are empty , it makes sure dice is 1.
 - If either output or target are empty ( all pixels are background ) , dice = ` ` ` smooth / ( small _ value + smooth ) ` ` , then if smooth is very small , dice close to 0 ( even the image values lower than the threshold ) , so in this case , higher smooth can have a higher dice .
 Examples
 > > > outputs = tl . act . pixel _ wise _ softmax ( network . outputs )
 > > > dice _ loss = 1 - tl . cost . dice _ coe ( outputs , y _ )
 References
 - ` Wiki - Dice < https : / / en . wikipedia . org / wiki / Sørensen – Dice _ coefficient > ` _ _"""

implementation:
inse = tf . reduce_sum ( output * target , axis = axis )
if loss_type == 'jaccard' :
    l = tf . reduce_sum ( output * output , axis = axis )
    r = tf . reduce_sum ( target * target , axis = axis )
elif loss_type == 'sorensen' :
    l = tf . reduce_sum ( output , axis = axis )
    r = tf . reduce_sum ( target , axis = axis )
else :
    raise Exception ( "Unknow loss_type" )
# old axis = [ 0,1,2,3]
# dice = 2 * ( inse ) / ( l + r )
# epsilon = 1e - 5
# dice = tf . clip _ by _ value ( dice , 0 , 1.0 - epsilon ) # if all empty , dice = 1
# new haodong
dice = ( 2. * inse + smooth ) / ( l + r + smooth )
dice = tf . reduce_mean ( dice , name = 'dice_coe' )
return dice


signature:
def dice_hard_coe ( output , target , threshold = 0.5 , axis = ( 1 , 2 , 3 ) , smooth = 1e-5 ) :
    """Non - differentiable Sørensen – Dice coefficient for comparing the similarity
 of two batch of data , usually be used for binary image segmentation i . e . labels are binary .
 The coefficient between 0 to 1 , 1 if totally match .
 Parameters
 output : tensor
 A distribution with shape : [ batch _ size , . . . . ] , ( any dimensions ) .
 target : tensor
 The target distribution , format the same with ` output ` .
 threshold : float
 The threshold value to be true .
 axis : tuple of integer
 All dimensions are reduced , default ` ` ( 1,2,3 ) ` ` .
 smooth : float
 This small value will be added to the numerator and denominator , see ` ` dice _ coe ` ` .
 References
 - ` Wiki - Dice < https : / / en . wikipedia . org / wiki / Sørensen – Dice _ coefficient > ` _ _"""

implementation:
output = tf . cast ( output > threshold , dtype = tf . float32 )
target = tf . cast ( target > threshold , dtype = tf . float32 )
inse = tf . reduce_sum ( tf . multiply ( output , target ) , axis = axis )
l = tf . reduce_sum ( output , axis = axis )
r = tf . reduce_sum ( target , axis = axis )
# old axis = [ 0,1,2,3]
# hard _ dice = 2 * ( inse ) / ( l + r )
# epsilon = 1e - 5
# hard _ dice = tf . clip _ by _ value ( hard _ dice , 0 , 1.0 - epsilon )
# new haodong
hard_dice = ( 2. * inse + smooth ) / ( l + r + smooth )
hard_dice = tf . reduce_mean ( hard_dice , name = 'hard_dice' )
return hard_dice


signature:
def iou_coe ( output , target , threshold = 0.5 , axis = ( 1 , 2 , 3 ) , smooth = 1e-5 ) :
    """Non - differentiable Intersection over Union ( IoU ) for comparing the
 similarity of two batch of data , usually be used for evaluating binary image segmentation .
 The coefficient between 0 to 1 , and 1 means totally match .
 Parameters
 output : tensor
 A batch of distribution with shape : [ batch _ size , . . . . ] , ( any dimensions ) .
 target : tensor
 The target distribution , format the same with ` output ` .
 threshold : float
 The threshold value to be true .
 axis : tuple of integer
 All dimensions are reduced , default ` ` ( 1,2,3 ) ` ` .
 smooth : float
 This small value will be added to the numerator and denominator , see ` ` dice _ coe ` ` .
 Notes
 - IoU cannot be used as training loss , people usually use dice coefficient for training , IoU and hard - dice for evaluating ."""

implementation:
pre = tf . cast ( output > threshold , dtype = tf . float32 )
truth = tf . cast ( target > threshold , dtype = tf . float32 )
inse = tf . reduce_sum ( tf . multiply ( pre , truth ) , axis = axis )
# AND
union = tf . reduce_sum ( tf . cast ( tf . add ( pre , truth ) >= 1 , dtype = tf . float32 ) , axis = axis )
# OR
# old axis = [ 0,1,2,3]
# epsilon = 1e - 5
# batch _ iou = inse / ( union + epsilon )
# new haodong
batch_iou = ( inse + smooth ) / ( union + smooth )
iou = tf . reduce_mean ( batch_iou , name = 'iou_coe' )
return iou


signature:
def cross_entropy_seq ( logits , target_seqs , batch_size = None ) : # , batch _ size = 1 , num _ steps = None ) :
    """Returns the expression of cross - entropy of two sequences , implement
 softmax internally . Normally be used for fixed length RNN outputs , see ` PTB example < https : / / github . com / tensorlayer / tensorlayer / blob / master / example / tutorial _ ptb _ lstm _ state _ is _ tuple . py > ` _ _ .
 Parameters
 logits : Tensor
 2D tensor with shape of ` [ batch _ size * n _ steps , n _ classes ] ` .
 target _ seqs : Tensor
 The target sequence , 2D tensor ` [ batch _ size , n _ steps ] ` , if the number of step is dynamic , please use ` ` tl . cost . cross _ entropy _ seq _ with _ mask ` ` instead .
 batch _ size : None or int .
 Whether to divide the cost by batch size .
 - If integer , the return cost will be divided by ` batch _ size ` .
 - If None ( default ) , the return cost will not be divided by anything .
 Examples
 > > > see ` PTB example < https : / / github . com / tensorlayer / tensorlayer / blob / master / example / tutorial _ ptb _ lstm _ state _ is _ tuple . py > ` _ _ . for more details
 > > > input _ data = tf . placeholder ( tf . int32 , [ batch _ size , n _ steps ] )
 > > > targets = tf . placeholder ( tf . int32 , [ batch _ size , n _ steps ] )
 > > > # build the network
 > > > print ( net . outputs )
 ( batch _ size * n _ steps , n _ classes )
 > > > cost = tl . cost . cross _ entropy _ seq ( network . outputs , targets )"""

implementation:
sequence_loss_by_example_fn = tf . contrib . legacy_seq2seq . sequence_loss_by_example
loss = sequence_loss_by_example_fn ( [ logits ] , [ tf . reshape ( target_seqs , [ - 1 ] ) ] , [ tf . ones_like ( tf . reshape ( target_seqs , [ - 1 ] ) , dtype = tf . float32 ) ] )
# [ tf . ones ( [ batch _ size * num _ steps ] ) ] )
cost = tf . reduce_sum ( loss )
# / batch _ size
if batch_size is not None :
    cost = cost / batch_size
return cost


signature:
def cross_entropy_seq_with_mask ( logits , target_seqs , input_mask , return_details = False , name = None ) :
    """Returns the expression of cross - entropy of two sequences , implement
 softmax internally . Normally be used for Dynamic RNN with Synced sequence input and output .
 Parameters
 logits : Tensor
 2D tensor with shape of [ batch _ size * ? , n _ classes ] , ` ? ` means dynamic IDs for each example .
 - Can be get from ` DynamicRNNLayer ` by setting ` ` return _ seq _ 2d ` ` to ` True ` .
 target _ seqs : Tensor
 int of tensor , like word ID . [ batch _ size , ? ] , ` ? ` means dynamic IDs for each example .
 input _ mask : Tensor
 The mask to compute loss , it has the same size with ` target _ seqs ` , normally 0 or 1.
 return _ details : boolean
 Whether to return detailed losses .
 - If False ( default ) , only returns the loss .
 - If True , returns the loss , losses , weights and targets ( see source code ) .
 Examples
 > > > batch _ size = 64
 > > > vocab _ size = 10000
 > > > embedding _ size = 256
 > > > input _ seqs = tf . placeholder ( dtype = tf . int64 , shape = [ batch _ size , None ] , name = " input " )
 > > > target _ seqs = tf . placeholder ( dtype = tf . int64 , shape = [ batch _ size , None ] , name = " target " )
 > > > input _ mask = tf . placeholder ( dtype = tf . int64 , shape = [ batch _ size , None ] , name = " mask " )
 > > > net = tl . layers . EmbeddingInputlayer (
 . . . inputs = input _ seqs ,
 . . . vocabulary _ size = vocab _ size ,
 . . . embedding _ size = embedding _ size ,
 . . . name = ' seq _ embedding ' )
 > > > net = tl . layers . DynamicRNNLayer ( net ,
 . . . cell _ fn = tf . contrib . rnn . BasicLSTMCell ,
 . . . n _ hidden = embedding _ size ,
 . . . dropout = ( 0.7 if is _ train else None ) ,
 . . . sequence _ length = tl . layers . retrieve _ seq _ length _ op2 ( input _ seqs ) ,
 . . . return _ seq _ 2d = True ,
 . . . name = ' dynamicrnn ' )
 > > > print ( net . outputs )
 ( ? , 256)
 > > > net = tl . layers . DenseLayer ( net , n _ units = vocab _ size , name = " output " )
 > > > print ( net . outputs )
 ( ? , 10000)
 > > > loss = tl . cost . cross _ entropy _ seq _ with _ mask ( net . outputs , target _ seqs , input _ mask )"""

implementation:
targets = tf . reshape ( target_seqs , [ - 1 ] )
# to one vector
weights = tf . to_float ( tf . reshape ( input_mask , [ - 1 ] ) )
# to one vector like targets
losses = tf . nn . sparse_softmax_cross_entropy_with_logits ( logits = logits , labels = targets , name = name ) * weights
# losses = tf . reduce _ mean ( tf . nn . sparse _ softmax _ cross _ entropy _ with _ logits ( logits = logits , labels = targets , name = name ) ) # for TF1.0 and others
loss = tf . divide ( tf . reduce_sum ( losses ) , # loss from mask . reduce _ sum before element - wise mul with mask ! !
tf . reduce_sum ( weights ) , name = "seq_loss_with_mask" )
if return_details :
    return loss , losses , weights , targets
else :
    return loss


signature:
def cosine_similarity ( v1 , v2 ) :
    """Cosine similarity [ - 1 , 1 ] .
 Parameters
 v1 , v2 : Tensor
 Tensor with the same shape [ batch _ size , n _ feature ] .
 References
 - ` Wiki < https : / / en . wikipedia . org / wiki / Cosine _ similarity > ` _ _ ."""

implementation: return tf . reduce_sum ( tf . multiply ( v1 , v2 ) , 1 ) / ( tf . sqrt ( tf . reduce_sum ( tf . multiply ( v1 , v1 ) , 1 ) ) * tf . sqrt ( tf . reduce_sum ( tf . multiply ( v2 , v2 ) , 1 ) ) )


signature:
def ramp ( x , v_min = 0 , v_max = 1 , name = None ) :
    """Ramp activation function .
 Parameters
 x : Tensor
 input .
 v _ min : float
 cap input to v _ min as a lower bound .
 v _ max : float
 cap input to v _ max as a upper bound .
 name : str
 The function name ( optional ) .
 Returns
 Tensor
 A ` ` Tensor ` ` in the same type as ` ` x ` ` ."""

implementation: return tf . clip_by_value ( x , clip_value_min = v_min , clip_value_max = v_max , name = name )


signature:
def leaky_relu ( x , alpha = 0.2 , name = "leaky_relu" ) :
    """leaky _ relu can be used through its shortcut : : func : ` tl . act . lrelu ` .
 This function is a modified version of ReLU , introducing a nonzero gradient for negative input . Introduced by the paper :
 ` Rectifier Nonlinearities Improve Neural Network Acoustic Models [ A . L . Maas et al . , 2013 ] < https : / / ai . stanford . edu / ~ amaas / papers / relu _ hybrid _ icml2013 _ final . pdf > ` _ _
 The function return the following results :
 - When x < 0 : ` ` f ( x ) = alpha _ low * x ` ` .
 - When x > = 0 : ` ` f ( x ) = x ` ` .
 Parameters
 x : Tensor
 Support input type ` ` float ` ` , ` ` double ` ` , ` ` int32 ` ` , ` ` int64 ` ` , ` ` uint8 ` ` , ` ` int16 ` ` , or ` ` int8 ` ` .
 alpha : float
 Slope .
 name : str
 The function name ( optional ) .
 Examples
 > > > import tensorlayer as tl
 > > > net = tl . layers . DenseLayer ( net , 100 , act = lambda x : tl . act . lrelu ( x , 0.2 ) , name = ' dense ' )
 Returns
 Tensor
 A ` ` Tensor ` ` in the same type as ` ` x ` ` .
 References
 - ` Rectifier Nonlinearities Improve Neural Network Acoustic Models [ A . L . Maas et al . , 2013 ] < https : / / ai . stanford . edu / ~ amaas / papers / relu _ hybrid _ icml2013 _ final . pdf > ` _ _"""

implementation:
if not ( 0 < alpha <= 1 ) :
    raise ValueError ( "`alpha` value must be in [0, 1]`" )
with tf . name_scope ( name , "leaky_relu" ) as name_scope :
    x = tf . convert_to_tensor ( x , name = "features" )
    return tf . maximum ( x , alpha * x , name = name_scope )


signature:
def leaky_relu6 ( x , alpha = 0.2 , name = "leaky_relu6" ) :
    """: func : ` leaky _ relu6 ` can be used through its shortcut : : func : ` tl . act . lrelu6 ` .
 This activation function is a modified version : func : ` leaky _ relu ` introduced by the following paper :
 ` Rectifier Nonlinearities Improve Neural Network Acoustic Models [ A . L . Maas et al . , 2013 ] < https : / / ai . stanford . edu / ~ amaas / papers / relu _ hybrid _ icml2013 _ final . pdf > ` _ _
 This activation function also follows the behaviour of the activation function : func : ` tf . nn . relu6 ` introduced by the following paper :
 ` Convolutional Deep Belief Networks on CIFAR - 10 [ A . Krizhevsky , 2010 ] < http : / / www . cs . utoronto . ca / ~ kriz / conv - cifar10 - aug2010 . pdf > ` _ _
 The function return the following results :
 - When x < 0 : ` ` f ( x ) = alpha _ low * x ` ` .
 - When x in [ 0 , 6 ] : ` ` f ( x ) = x ` ` .
 - When x > 6 : ` ` f ( x ) = 6 ` ` .
 Parameters
 x : Tensor
 Support input type ` ` float ` ` , ` ` double ` ` , ` ` int32 ` ` , ` ` int64 ` ` , ` ` uint8 ` ` , ` ` int16 ` ` , or ` ` int8 ` ` .
 alpha : float
 Slope .
 name : str
 The function name ( optional ) .
 Examples
 > > > import tensorlayer as tl
 > > > net = tl . layers . DenseLayer ( net , 100 , act = lambda x : tl . act . leaky _ relu6 ( x , 0.2 ) , name = ' dense ' )
 Returns
 Tensor
 A ` ` Tensor ` ` in the same type as ` ` x ` ` .
 References
 - ` Rectifier Nonlinearities Improve Neural Network Acoustic Models [ A . L . Maas et al . , 2013 ] < https : / / ai . stanford . edu / ~ amaas / papers / relu _ hybrid _ icml2013 _ final . pdf > ` _ _
 - ` Convolutional Deep Belief Networks on CIFAR - 10 [ A . Krizhevsky , 2010 ] < http : / / www . cs . utoronto . ca / ~ kriz / conv - cifar10 - aug2010 . pdf > ` _ _"""

implementation:
if not isinstance ( alpha , tf . Tensor ) and not ( 0 < alpha <= 1 ) :
    raise ValueError ( "`alpha` value must be in [0, 1]`" )
with tf . name_scope ( name , "leaky_relu6" ) as name_scope :
    x = tf . convert_to_tensor ( x , name = "features" )
    return tf . minimum ( tf . maximum ( x , alpha * x ) , 6 , name = name_scope )


signature:
def leaky_twice_relu6 ( x , alpha_low = 0.2 , alpha_high = 0.2 , name = "leaky_relu6" ) :
    """: func : ` leaky _ twice _ relu6 ` can be used through its shortcut : : func : ` : func : ` tl . act . ltrelu6 ` .
 This activation function is a modified version : func : ` leaky _ relu ` introduced by the following paper :
 ` Rectifier Nonlinearities Improve Neural Network Acoustic Models [ A . L . Maas et al . , 2013 ] < https : / / ai . stanford . edu / ~ amaas / papers / relu _ hybrid _ icml2013 _ final . pdf > ` _ _
 This activation function also follows the behaviour of the activation function : func : ` tf . nn . relu6 ` introduced by the following paper :
 ` Convolutional Deep Belief Networks on CIFAR - 10 [ A . Krizhevsky , 2010 ] < http : / / www . cs . utoronto . ca / ~ kriz / conv - cifar10 - aug2010 . pdf > ` _ _
 This function push further the logic by adding ` leaky ` behaviour both below zero and above six .
 The function return the following results :
 - When x < 0 : ` ` f ( x ) = alpha _ low * x ` ` .
 - When x in [ 0 , 6 ] : ` ` f ( x ) = x ` ` .
 - When x > 6 : ` ` f ( x ) = 6 + ( alpha _ high * ( x - 6 ) ) ` ` .
 Parameters
 x : Tensor
 Support input type ` ` float ` ` , ` ` double ` ` , ` ` int32 ` ` , ` ` int64 ` ` , ` ` uint8 ` ` , ` ` int16 ` ` , or ` ` int8 ` ` .
 alpha _ low : float
 Slope for x < 0 : ` ` f ( x ) = alpha _ low * x ` ` .
 alpha _ high : float
 Slope for x < 6 : ` ` f ( x ) = 6 ( alpha _ high * ( x - 6 ) ) ` ` .
 name : str
 The function name ( optional ) .
 Examples
 > > > import tensorlayer as tl
 > > > net = tl . layers . DenseLayer ( net , 100 , act = lambda x : tl . act . leaky _ twice _ relu6 ( x , 0.2 , 0.2 ) , name = ' dense ' )
 Returns
 Tensor
 A ` ` Tensor ` ` in the same type as ` ` x ` ` .
 References
 - ` Rectifier Nonlinearities Improve Neural Network Acoustic Models [ A . L . Maas et al . , 2013 ] < https : / / ai . stanford . edu / ~ amaas / papers / relu _ hybrid _ icml2013 _ final . pdf > ` _ _
 - ` Convolutional Deep Belief Networks on CIFAR - 10 [ A . Krizhevsky , 2010 ] < http : / / www . cs . utoronto . ca / ~ kriz / conv - cifar10 - aug2010 . pdf > ` _ _"""

implementation:
if not isinstance ( alpha_high , tf . Tensor ) and not ( 0 < alpha_high <= 1 ) :
    raise ValueError ( "`alpha_high` value must be in [0, 1]`" )
if not isinstance ( alpha_low , tf . Tensor ) and not ( 0 < alpha_low <= 1 ) :
    raise ValueError ( "`alpha_low` value must be in [0, 1]`" )
with tf . name_scope ( name , "leaky_twice_relu6" ) as name_scope :
    x = tf . convert_to_tensor ( x , name = "features" )
    x_is_above_0 = tf . minimum ( x , 6 * ( 1 - alpha_high ) + alpha_high * x )
    x_is_below_0 = tf . minimum ( alpha_low * x , 0 )
    return tf . maximum ( x_is_above_0 , x_is_below_0 , name = name_scope )


signature:
def swish ( x , name = 'swish' ) :
    """Swish function .
 See ` Swish : a Self - Gated Activation Function < https : / / arxiv . org / abs / 1710.05941 > ` _ _ .
 Parameters
 x : Tensor
 input .
 name : str
 function name ( optional ) .
 Returns
 Tensor
 A ` ` Tensor ` ` in the same type as ` ` x ` ` ."""

implementation:
with tf . name_scope ( name ) :
    x = tf . nn . sigmoid ( x ) * x
return x


signature:
def pixel_wise_softmax ( x , name = 'pixel_wise_softmax' ) :
    """Return the softmax outputs of images , every pixels have multiple label , the sum of a pixel is 1.
 Usually be used for image segmentation .
 Parameters
 x : Tensor
 input .
 - For 2d image , 4D tensor ( batch _ size , height , weight , channel ) , where channel > = 2.
 - For 3d image , 5D tensor ( batch _ size , depth , height , weight , channel ) , where channel > = 2.
 name : str
 function name ( optional )
 Returns
 Tensor
 A ` ` Tensor ` ` in the same type as ` ` x ` ` .
 Examples
 > > > outputs = pixel _ wise _ softmax ( network . outputs )
 > > > dice _ loss = 1 - dice _ coe ( outputs , y _ , epsilon = 1e - 5)
 References
 - ` tf . reverse < https : / / www . tensorflow . org / versions / master / api _ docs / python / array _ ops . html # reverse > ` _ _"""

implementation:
with tf . name_scope ( name ) :
    return tf . nn . softmax ( x )


signature:
def _conv_linear ( args , filter_size , num_features , bias , bias_start = 0.0 , scope = None ) :
    """convolution :
 Parameters
 args : tensor
 4D Tensor or a list of 4D , batch x n , Tensors .
 filter _ size : tuple of int
 Filter height and width .
 num _ features : int
 Nnumber of features .
 bias _ start : float
 Starting value to initialize the bias ; 0 by default .
 scope : VariableScope
 For the created subgraph ; defaults to " Linear " .
 Returns
 - A 4D Tensor with shape [ batch h w num _ features ]
 Raises
 - ValueError : if some of the arguments has unspecified or wrong shape ."""

implementation:
# Calculate the total size of arguments on dimension 1.
total_arg_size_depth = 0
shapes = [ a . get_shape ( ) . as_list ( ) for a in args ]
for shape in shapes :
    if len ( shape ) != 4 :
        raise ValueError ( "Linear is expecting 4D arguments: %s" % str ( shapes ) )
    if not shape [ 3 ] :
        raise ValueError ( "Linear expects shape[4] of arguments: %s" % str ( shapes ) )
    else :
        total_arg_size_depth += shape [ 3 ]
dtype = [ a . dtype for a in args ] [ 0 ]
# Now the computation .
with tf . variable_scope ( scope or "Conv" ) :
    matrix = tf . get_variable ( "Matrix" , [ filter_size [ 0 ] , filter_size [ 1 ] , total_arg_size_depth , num_features ] , dtype = dtype )
    if len ( args ) == 1 :
        res = tf . nn . conv2d ( args [ 0 ] , matrix , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' )
    else :
        res = tf . nn . conv2d ( tf . concat ( args , 3 ) , matrix , strides = [ 1 , 1 , 1 , 1 ] , padding = 'SAME' )
    if not bias :
        return res
    bias_term = tf . get_variable ( "Bias" , [ num_features ] , dtype = dtype , initializer = tf . constant_initializer ( bias_start , dtype = dtype ) )
return res + bias_term


signature:
def advanced_indexing_op ( inputs , index ) :
    """Advanced Indexing for Sequences , returns the outputs by given sequence lengths .
 When return the last output : class : ` DynamicRNNLayer ` uses it to get the last outputs with the sequence lengths .
 Parameters
 inputs : tensor for data
 With shape of [ batch _ size , n _ step ( max ) , n _ features ]
 index : tensor for indexing
 Sequence length in Dynamic RNN . [ batch _ size ]
 Examples
 > > > import numpy as np
 > > > import tensorflow as tf
 > > > import tensorlayer as tl
 > > > batch _ size , max _ length , n _ features = 3 , 5 , 2
 > > > z = np . random . uniform ( low = - 1 , high = 1 , size = [ batch _ size , max _ length , n _ features ] ) . astype ( np . float32)
 > > > b _ z = tf . constant ( z )
 > > > sl = tf . placeholder ( dtype = tf . int32 , shape = [ batch _ size ] )
 > > > o = advanced _ indexing _ op ( b _ z , sl )
 > > > sess = tf . InteractiveSession ( )
 > > > tl . layers . initialize _ global _ variables ( sess )
 > > > order = np . asarray ( [ 1,1,2 ] )
 > > > print ( " real " , z [ 0 ] [ order [ 0 ] - 1 ] , z [ 1 ] [ order [ 1 ] - 1 ] , z [ 2 ] [ order [ 2 ] - 1 ] )
 > > > y = sess . run ( [ o ] , feed _ dict = { sl : order } )
 > > > print ( " given " , order )
 > > > print ( " out " , y )
 real [ - 0.93021595 0.53820813 ] [ - 0.92548317 - 0.77135968 ] [ 0.89952248 0.19149846]
 given [ 1 1 2]
 out [ array ( [ [ - 0.93021595 , 0.53820813 ] ,
 [ - 0.92548317 , - 0.77135968 ] ,
 [ 0.89952248 , 0.19149846 ] ] , dtype = float32 ) ]
 References
 - Modified from TFlearn ( the original code is used for fixed length rnn ) , ` references < https : / / github . com / tflearn / tflearn / blob / master / tflearn / layers / recurrent . py > ` _ _ ."""

implementation:
batch_size = tf . shape ( inputs ) [ 0 ]
# max _ length = int ( inputs . get _ shape ( ) [ 1 ] ) # for fixed length rnn , length is given
max_length = tf . shape ( inputs ) [ 1 ]
# for dynamic _ rnn , length is unknown
dim_size = int ( inputs . get_shape ( ) [ 2 ] )
index = tf . range ( 0 , batch_size ) * max_length + ( index - 1 )
flat = tf . reshape ( inputs , [ - 1 , dim_size ] )
relevant = tf . gather ( flat , index )
return relevant


signature:
def retrieve_seq_length_op ( data ) :
    """An op to compute the length of a sequence from input shape of [ batch _ size , n _ step ( max ) , n _ features ] ,
 it can be used when the features of padding ( on right hand side ) are all zeros .
 Parameters
 data : tensor
 [ batch _ size , n _ step ( max ) , n _ features ] with zero padding on right hand side .
 Examples
 > > > data = [ [ [ 1 ] , [ 2 ] , [ 0 ] , [ 0 ] , [ 0 ] ] ,
 . . . [ [ 1 ] , [ 2 ] , [ 3 ] , [ 0 ] , [ 0 ] ] ,
 . . . [ [ 1 ] , [ 2 ] , [ 6 ] , [ 1 ] , [ 0 ] ] ]
 > > > data = np . asarray ( data )
 > > > print ( data . shape )
 (3 , 5 , 1)
 > > > data = tf . constant ( data )
 > > > sl = retrieve _ seq _ length _ op ( data )
 > > > sess = tf . InteractiveSession ( )
 > > > tl . layers . initialize _ global _ variables ( sess )
 > > > y = sl . eval ( )
 [2 3 4]
 Multiple features
 > > > data = [ [ [ 1,2 ] , [ 2,2 ] , [ 1,2 ] , [ 1,2 ] , [ 0,0 ] ] ,
 . . . [ [ 2,3 ] , [ 2,4 ] , [ 3,2 ] , [ 0,0 ] , [ 0,0 ] ] ,
 . . . [ [ 3,3 ] , [ 2,2 ] , [ 5,3 ] , [ 1,2 ] , [ 0,0 ] ] ]
 > > > print ( sl )
 [4 3 4]
 References
 Borrow from ` TFlearn < https : / / github . com / tflearn / tflearn / blob / master / tflearn / layers / recurrent . py > ` _ _ ."""

implementation:
with tf . name_scope ( 'GetLength' ) :
    used = tf . sign ( tf . reduce_max ( tf . abs ( data ) , 2 ) )
    length = tf . reduce_sum ( used , 1 )
    return tf . cast ( length , tf . int32 )


signature:
def retrieve_seq_length_op2 ( data ) :
    """An op to compute the length of a sequence , from input shape of [ batch _ size , n _ step ( max ) ] ,
 it can be used when the features of padding ( on right hand side ) are all zeros .
 Parameters
 data : tensor
 [ batch _ size , n _ step ( max ) ] with zero padding on right hand side .
 Examples
 > > > data = [ [ 1,2,0,0,0 ] ,
 . . . [ 1,2,3,0,0 ] ,
 . . . [ 1,2,6,1,0 ] ]
 > > > o = retrieve _ seq _ length _ op2 ( data )
 > > > sess = tf . InteractiveSession ( )
 > > > tl . layers . initialize _ global _ variables ( sess )
 > > > print ( o . eval ( ) )
 [2 3 4]"""

implementation: return tf . reduce_sum ( tf . cast ( tf . greater ( data , tf . zeros_like ( data ) ) , tf . int32 ) , 1 )


signature:
def retrieve_seq_length_op3 ( data , pad_val = 0 ) : # HangSheng : return tensor for sequence length , if input is tf . string
    """Return tensor for sequence length , if input is ` ` tf . string ` ` ."""

implementation:
data_shape_size = data . get_shape ( ) . ndims
if data_shape_size == 3 :
    return tf . reduce_sum ( tf . cast ( tf . reduce_any ( tf . not_equal ( data , pad_val ) , axis = 2 ) , dtype = tf . int32 ) , 1 )
elif data_shape_size == 2 :
    return tf . reduce_sum ( tf . cast ( tf . not_equal ( data , pad_val ) , dtype = tf . int32 ) , 1 )
elif data_shape_size == 1 :
    raise ValueError ( "retrieve_seq_length_op3: data has wrong shape!" )
else :
    raise ValueError ( "retrieve_seq_length_op3: handling data_shape_size %s hasn't been implemented!" % ( data_shape_size ) )


signature:
def zero_state ( self , batch_size , dtype = LayersConfig . tf_dtype ) :
    """Return zero - filled state tensor ( s ) .
 Args :
 batch _ size : int , float , or unit Tensor representing the batch size .
 Returns :
 tensor of shape ' [ batch _ size x shape [ 0 ] x shape [ 1 ] x num _ features ]
 filled with zeros"""

implementation:
shape = self . shape
num_features = self . num_features
# TODO : TypeError : ' NoneType ' object is not subscriptable
zeros = tf . zeros ( [ batch_size , shape [ 0 ] , shape [ 1 ] , num_features * 2 ] , dtype = dtype )
return zeros


signature:
def state_size ( self ) :
    """State size of the LSTMStateTuple ."""

implementation: return ( LSTMStateTuple ( self . _num_units , self . _num_units ) if self . _state_is_tuple else 2 * self . _num_units )


signature:
def _to_bc_h_w ( self , x , x_shape ) :
    """( b , h , w , c ) - > ( b * c , h , w )"""

implementation:
x = tf . transpose ( x , [ 0 , 3 , 1 , 2 ] )
x = tf . reshape ( x , ( - 1 , x_shape [ 1 ] , x_shape [ 2 ] ) )
return x


implementation:
x = tf . reshape ( x , ( - 1 , x_shape [ 4 ] , x_shape [ 1 ] , x_shape [ 2 ] , x_shape [ 3 ] ) )
x = tf . transpose ( x , [ 0 , 2 , 3 , 4 , 1 ] )
return x


signature:
def _tf_repeat ( self , a , repeats ) :
    """Tensorflow version of np . repeat for 1D"""

implementation:
# https : / / github . com / tensorflow / tensorflow / issues / 8521
if len ( a . get_shape ( ) ) != 1 :
    raise AssertionError ( "This is not a 1D Tensor" )
a = tf . expand_dims ( a , - 1 )
a = tf . tile ( a , [ 1 , repeats ] )
a = self . tf_flatten ( a )
return a


signature:
def _tf_batch_map_coordinates ( self , inputs , coords ) :
    """Batch version of tf _ map _ coordinates
 Only supports 2D feature maps
 Parameters
 inputs : ` ` tf . Tensor ` `
 shape = ( b * c , h , w )
 coords : ` ` tf . Tensor ` `
 shape = ( b * c , h , w , n , 2)
 Returns
 ` ` tf . Tensor ` `
 A Tensor with the shape as ( b * c , h , w , n )"""

implementation:
input_shape = inputs . get_shape ( )
coords_shape = coords . get_shape ( )
batch_channel = tf . shape ( inputs ) [ 0 ]
input_h = int ( input_shape [ 1 ] )
input_w = int ( input_shape [ 2 ] )
kernel_n = int ( coords_shape [ 3 ] )
n_coords = input_h * input_w * kernel_n
coords_lt = tf . cast ( tf . floor ( coords ) , 'int32' )
coords_rb = tf . cast ( tf . ceil ( coords ) , 'int32' )
coords_lb = tf . stack ( [ coords_lt [ : , : , : , : , 0 ] , coords_rb [ : , : , : , : , 1 ] ] , axis = - 1 )
coords_rt = tf . stack ( [ coords_rb [ : , : , : , : , 0 ] , coords_lt [ : , : , : , : , 1 ] ] , axis = - 1 )
idx = self . _tf_repeat ( tf . range ( batch_channel ) , n_coords )
vals_lt = self . _get_vals_by_coords ( inputs , coords_lt , idx , ( batch_channel , input_h , input_w , kernel_n ) )
vals_rb = self . _get_vals_by_coords ( inputs , coords_rb , idx , ( batch_channel , input_h , input_w , kernel_n ) )
vals_lb = self . _get_vals_by_coords ( inputs , coords_lb , idx , ( batch_channel , input_h , input_w , kernel_n ) )
vals_rt = self . _get_vals_by_coords ( inputs , coords_rt , idx , ( batch_channel , input_h , input_w , kernel_n ) )
coords_offset_lt = coords - tf . cast ( coords_lt , 'float32' )
vals_t = vals_lt + ( vals_rt - vals_lt ) * coords_offset_lt [ : , : , : , : , 0 ]
vals_b = vals_lb + ( vals_rb - vals_lb ) * coords_offset_lt [ : , : , : , : , 0 ]
mapped_vals = vals_t + ( vals_b - vals_t ) * coords_offset_lt [ : , : , : , : , 1 ]
return mapped_vals


signature:
def _tf_batch_map_offsets ( self , inputs , offsets , grid_offset ) :
    """Batch map offsets into input
 Parameters
 inputs : ` ` tf . Tensor ` `
 shape = ( b , h , w , c )
 offsets : ` ` tf . Tensor ` `
 shape = ( b , h , w , 2 * n )
 grid _ offset : ` tf . Tensor ` `
 Offset grids shape = ( h , w , n , 2)
 Returns
 ` ` tf . Tensor ` `
 A Tensor with the shape as ( b , h , w , c )"""

implementation:
input_shape = inputs . get_shape ( )
batch_size = tf . shape ( inputs ) [ 0 ]
kernel_n = int ( int ( offsets . get_shape ( ) [ 3 ] ) / 2 )
input_h = input_shape [ 1 ]
input_w = input_shape [ 2 ]
channel = input_shape [ 3 ]
# inputs ( b , h , w , c ) - - > ( b * c , h , w )
inputs = self . _to_bc_h_w ( inputs , input_shape )
# offsets ( b , h , w , 2 * n ) - - > ( b , h , w , n , 2)
offsets = tf . reshape ( offsets , ( batch_size , input_h , input_w , kernel_n , 2 ) )
# offsets ( b , h , w , n , 2 ) - - > ( b * c , h , w , n , 2)
# offsets = tf . tile ( offsets , [ channel , 1 , 1 , 1 , 1 ] )
coords = tf . expand_dims ( grid_offset , 0 )
# grid _ offset - - > ( 1 , h , w , n , 2)
coords = tf . tile ( coords , [ batch_size , 1 , 1 , 1 , 1 ] ) + offsets
# grid _ offset - - > ( b , h , w , n , 2)
# clip out of bound
coords = tf . stack ( [ tf . clip_by_value ( coords [ : , : , : , : , 0 ] , 0.0 , tf . cast ( input_h - 1 , 'float32' ) ) , tf . clip_by_value ( coords [ : , : , : , : , 1 ] , 0.0 , tf . cast ( input_w - 1 , 'float32' ) ) ] , axis = - 1 )
coords = tf . tile ( coords , [ channel , 1 , 1 , 1 , 1 ] )
mapped_vals = self . _tf_batch_map_coordinates ( inputs , coords )
# ( b * c , h , w , n ) - - > ( b , h , w , n , c )
mapped_vals = self . _to_b_h_w_n_c ( mapped_vals , [ batch_size , input_h , input_w , kernel_n , channel ] )
return mapped_vals


signature:
def minibatches ( inputs = None , targets = None , batch_size = None , allow_dynamic_batch_size = False , shuffle = False ) :
    """Generate a generator that input a group of example in numpy . array and
 their labels , return the examples and labels by the given batch size .
 Parameters
 inputs : numpy . array
 The input features , every row is a example .
 targets : numpy . array
 The labels of inputs , every row is a example .
 batch _ size : int
 The batch size .
 allow _ dynamic _ batch _ size : boolean
 Allow the use of the last data batch in case the number of examples is not a multiple of batch _ size , this may result in unexpected behaviour if other functions expect a fixed - sized batch - size .
 shuffle : boolean
 Indicating whether to use a shuffling queue , shuffle the dataset before return .
 Examples
 > > > X = np . asarray ( [ [ ' a ' , ' a ' ] , [ ' b ' , ' b ' ] , [ ' c ' , ' c ' ] , [ ' d ' , ' d ' ] , [ ' e ' , ' e ' ] , [ ' f ' , ' f ' ] ] )
 > > > y = np . asarray ( [ 0,1,2,3,4,5 ] )
 > > > for batch in tl . iterate . minibatches ( inputs = X , targets = y , batch _ size = 2 , shuffle = False ) :
 > > > print ( batch )
 ( array ( [ [ ' a ' , ' a ' ] , [ ' b ' , ' b ' ] ] , dtype = ' < U1 ' ) , array ( [ 0 , 1 ] ) )
 ( array ( [ [ ' c ' , ' c ' ] , [ ' d ' , ' d ' ] ] , dtype = ' < U1 ' ) , array ( [ 2 , 3 ] ) )
 ( array ( [ [ ' e ' , ' e ' ] , [ ' f ' , ' f ' ] ] , dtype = ' < U1 ' ) , array ( [ 4 , 5 ] ) )
 Notes
 If you have two inputs and one label and want to shuffle them together , e . g . X1 ( 1000 , 100 ) , X2 ( 1000 , 80 ) and Y ( 1000 , 1 ) , you can stack them together ( ` np . hstack ( ( X1 , X2 ) ) ` )
 into ( 1000 , 180 ) and feed to ` ` inputs ` ` . After getting a batch , you can split it back into X1 and X2."""

implementation:
if len ( inputs ) != len ( targets ) :
    raise AssertionError ( "The length of inputs and targets should be equal" )
if shuffle :
    indices = np . arange ( len ( inputs ) )
    np . random . shuffle ( indices )
# for start _ idx in range ( 0 , len ( inputs ) - batch _ size + 1 , batch _ size ) :
# chulei : handling the case where the number of samples is not a multiple of batch _ size , avoiding wasting samples
for start_idx in range ( 0 , len ( inputs ) , batch_size ) :
    end_idx = start_idx + batch_size
    if end_idx > len ( inputs ) :
        if allow_dynamic_batch_size :
            end_idx = len ( inputs )
        else :
            break
    if shuffle :
        excerpt = indices [ start_idx : end_idx ]
    else :
        excerpt = slice ( start_idx , end_idx )
    if ( isinstance ( inputs , list ) or isinstance ( targets , list ) ) and ( shuffle == True ) : # zsdonghao : for list indexing when shuffle = = True
        yield [ inputs [ i ] for i in excerpt ] , [ targets [ i ] for i in excerpt ]
    else :
        yield inputs [ excerpt ] , targets [ excerpt ]


signature:
def seq_minibatches ( inputs , targets , batch_size , seq_length , stride = 1 ) :
    """Generate a generator that return a batch of sequence inputs and targets .
 If ` batch _ size = 100 ` and ` seq _ length = 5 ` , one return will have 500 rows ( examples ) .
 Parameters
 inputs : numpy . array
 The input features , every row is a example .
 targets : numpy . array
 The labels of inputs , every element is a example .
 batch _ size : int
 The batch size .
 seq _ length : int
 The sequence length .
 stride : int
 The stride step , default is 1.
 Examples
 Synced sequence input and output .
 > > > X = np . asarray ( [ [ ' a ' , ' a ' ] , [ ' b ' , ' b ' ] , [ ' c ' , ' c ' ] , [ ' d ' , ' d ' ] , [ ' e ' , ' e ' ] , [ ' f ' , ' f ' ] ] )
 > > > y = np . asarray ( [ 0 , 1 , 2 , 3 , 4 , 5 ] )
 > > > for batch in tl . iterate . seq _ minibatches ( inputs = X , targets = y , batch _ size = 2 , seq _ length = 2 , stride = 1 ) :
 > > > print ( batch )
 ( array ( [ [ ' a ' , ' a ' ] , [ ' b ' , ' b ' ] , [ ' b ' , ' b ' ] , [ ' c ' , ' c ' ] ] , dtype = ' < U1 ' ) , array ( [ 0 , 1 , 1 , 2 ] ) )
 ( array ( [ [ ' c ' , ' c ' ] , [ ' d ' , ' d ' ] , [ ' d ' , ' d ' ] , [ ' e ' , ' e ' ] ] , dtype = ' < U1 ' ) , array ( [ 2 , 3 , 3 , 4 ] ) )
 Many to One
 > > > return _ last = True
 > > > num _ steps = 2
 > > > X = np . asarray ( [ [ ' a ' , ' a ' ] , [ ' b ' , ' b ' ] , [ ' c ' , ' c ' ] , [ ' d ' , ' d ' ] , [ ' e ' , ' e ' ] , [ ' f ' , ' f ' ] ] )
 > > > Y = np . asarray ( [ 0,1,2,3,4,5 ] )
 > > > for batch in tl . iterate . seq _ minibatches ( inputs = X , targets = Y , batch _ size = 2 , seq _ length = num _ steps , stride = 1 ) :
 > > > x , y = batch
 > > > if return _ last :
 > > > tmp _ y = y . reshape ( ( - 1 , num _ steps ) + y . shape [ 1 : ] )
 > > > y = tmp _ y [ : , - 1]
 > > > print ( x , y )
 [ [ ' a ' ' a ' ]
 [ ' b ' ' b ' ]
 [ ' b ' ' b ' ]
 [ ' c ' ' c ' ] ] [ 1 2]
 [ [ ' c ' ' c ' ]
 [ ' d ' ' d ' ]
 [ ' d ' ' d ' ]
 [ ' e ' ' e ' ] ] [ 3 4]"""

implementation:
if len ( inputs ) != len ( targets ) :
    raise AssertionError ( "The length of inputs and targets should be equal" )
n_loads = ( batch_size * stride ) + ( seq_length - stride )
for start_idx in range ( 0 , len ( inputs ) - n_loads + 1 , ( batch_size * stride ) ) :
    seq_inputs = np . zeros ( ( batch_size , seq_length ) + inputs . shape [ 1 : ] , dtype = inputs . dtype )
    seq_targets = np . zeros ( ( batch_size , seq_length ) + targets . shape [ 1 : ] , dtype = targets . dtype )
    for b_idx in xrange ( batch_size ) :
        start_seq_idx = start_idx + ( b_idx * stride )
        end_seq_idx = start_seq_idx + seq_length
        seq_inputs [ b_idx ] = inputs [ start_seq_idx : end_seq_idx ]
        seq_targets [ b_idx ] = targets [ start_seq_idx : end_seq_idx ]
    flatten_inputs = seq_inputs . reshape ( ( - 1 , ) + inputs . shape [ 1 : ] )
    flatten_targets = seq_targets . reshape ( ( - 1 , ) + targets . shape [ 1 : ] )
    yield flatten_inputs , flatten_targets


signature:
def seq_minibatches2 ( inputs , targets , batch_size , num_steps ) :
    """Generate a generator that iterates on two list of words . Yields ( Returns ) the source contexts and
 the target context by the given batch _ size and num _ steps ( sequence _ length ) .
 In TensorFlow ' s tutorial , this generates the ` batch _ size ` pointers into the raw PTB data , and allows minibatch iteration along these pointers .
 Parameters
 inputs : list of data
 The context in list format ; note that context usually be represented by splitting by space , and then convert to unique word IDs .
 targets : list of data
 The context in list format ; note that context usually be represented by splitting by space , and then convert to unique word IDs .
 batch _ size : int
 The batch size .
 num _ steps : int
 The number of unrolls . i . e . sequence length
 Yields
 Pairs of the batched data , each a matrix of shape [ batch _ size , num _ steps ] .
 Raises
 ValueError : if batch _ size or num _ steps are too high .
 Examples
 > > > X = [ i for i in range ( 20 ) ]
 > > > Y = [ i for i in range ( 20,40 ) ]
 > > > for batch in tl . iterate . seq _ minibatches2 ( X , Y , batch _ size = 2 , num _ steps = 3 ) :
 . . . x , y = batch
 . . . print ( x , y )
 [ [ 0 . 1 . 2 . ]
 [ 10 . 11 . 12 . ] ]
 [ [ 20 . 21 . 22 . ]
 [ 30 . 31 . 32 . ] ]
 [ [ 3 . 4 . 5 . ]
 [ 13 . 14 . 15 . ] ]
 [ [ 23 . 24 . 25 . ]
 [ 33 . 34 . 35 . ] ]
 [ [ 6 . 7 . 8 . ]
 [ 16 . 17 . 18 . ] ]
 [ [ 26 . 27 . 28 . ]
 [ 36 . 37 . 38 . ] ]
 Notes
 - Hint , if the input data are images , you can modify the source code ` data = np . zeros ( [ batch _ size , batch _ len ) ` to ` data = np . zeros ( [ batch _ size , batch _ len , inputs . shape [ 1 ] , inputs . shape [ 2 ] , inputs . shape [ 3 ] ] ) ` ."""

implementation:
if len ( inputs ) != len ( targets ) :
    raise AssertionError ( "The length of inputs and targets should be equal" )
data_len = len ( inputs )
batch_len = data_len // batch_size
# data = np . zeros ( [ batch _ size , batch _ len ] )
data = np . zeros ( ( batch_size , batch_len ) + inputs . shape [ 1 : ] , dtype = inputs . dtype )
data2 = np . zeros ( [ batch_size , batch_len ] )
for i in range ( batch_size ) :
    data [ i ] = inputs [ batch_len * i : batch_len * ( i + 1 ) ]
    data2 [ i ] = targets [ batch_len * i : batch_len * ( i + 1 ) ]
epoch_size = ( batch_len - 1 ) // num_steps
if epoch_size == 0 :
    raise ValueError ( "epoch_size == 0, decrease batch_size or num_steps" )
for i in range ( epoch_size ) :
    x = data [ : , i * num_steps : ( i + 1 ) * num_steps ]
    x2 = data2 [ : , i * num_steps : ( i + 1 ) * num_steps ]
    yield ( x , x2 )


signature:
def ptb_iterator ( raw_data , batch_size , num_steps ) :
    """Generate a generator that iterates on a list of words , see ` PTB example < https : / / github . com / tensorlayer / tensorlayer / blob / master / example / tutorial _ ptb _ lstm _ state _ is _ tuple . py > ` _ _ .
 Yields the source contexts and the target context by the given batch _ size and num _ steps ( sequence _ length ) .
 In TensorFlow ' s tutorial , this generates ` batch _ size ` pointers into the raw
 PTB data , and allows minibatch iteration along these pointers .
 Parameters
 raw _ data : a list
 the context in list format ; note that context usually be
 represented by splitting by space , and then convert to unique
 word IDs .
 batch _ size : int
 the batch size .
 num _ steps : int
 the number of unrolls . i . e . sequence _ length
 Yields
 Pairs of the batched data , each a matrix of shape [ batch _ size , num _ steps ] .
 The second element of the tuple is the same data time - shifted to the
 right by one .
 Raises
 ValueError : if batch _ size or num _ steps are too high .
 Examples
 > > > train _ data = [ i for i in range ( 20 ) ]
 > > > for batch in tl . iterate . ptb _ iterator ( train _ data , batch _ size = 2 , num _ steps = 3 ) :
 > > > x , y = batch
 > > > print ( x , y )
 [ [ 0 1 2 ] < - - - x 1st subset / iteration
 [10 11 12 ] ]
 [ [ 1 2 3 ] < - - - y
 [11 12 13 ] ]
 [ [ 3 4 5 ] < - - - 1st batch input 2nd subset / iteration
 [13 14 15 ] ] < - - - 2nd batch input
 [ [ 4 5 6 ] < - - - 1st batch target
 [14 15 16 ] ] < - - - 2nd batch target
 [ [ 6 7 8 ] 3rd subset / iteration
 [16 17 18 ] ]
 [ [ 7 8 9]
 [17 18 19 ] ]"""

implementation:
raw_data = np . array ( raw_data , dtype = np . int32 )
data_len = len ( raw_data )
batch_len = data_len // batch_size
data = np . zeros ( [ batch_size , batch_len ] , dtype = np . int32 )
for i in range ( batch_size ) :
    data [ i ] = raw_data [ batch_len * i : batch_len * ( i + 1 ) ]
epoch_size = ( batch_len - 1 ) // num_steps
if epoch_size == 0 :
    raise ValueError ( "epoch_size == 0, decrease batch_size or num_steps" )
for i in range ( epoch_size ) :
    x = data [ : , i * num_steps : ( i + 1 ) * num_steps ]
    y = data [ : , i * num_steps + 1 : ( i + 1 ) * num_steps + 1 ]
    yield ( x , y )


signature:
def deconv2d_bilinear_upsampling_initializer ( shape ) :
    """Returns the initializer that can be passed to DeConv2dLayer for initializing the
 weights in correspondence to channel - wise bilinear up - sampling .
 Used in segmentation approaches such as [ FCN ] ( https : / / arxiv . org / abs / 1605.06211)
 Parameters
 shape : tuple of int
 The shape of the filters , [ height , width , output _ channels , in _ channels ] .
 It must match the shape passed to DeConv2dLayer .
 Returns
 ` ` tf . constant _ initializer ` `
 A constant initializer with weights set to correspond to per channel bilinear upsampling
 when passed as W _ int in DeConv2dLayer
 Examples
 - Upsampling by a factor of 2 , ie e . g 100 - > 200
 > > > import tensorflow as tf
 > > > import tensorlayer as tl
 > > > rescale _ factor = 2
 > > > imsize = 128
 > > > num _ channels = 3
 > > > filter _ shape = ( 5 , 5)
 > > > filter _ size = ( 2 * rescale _ factor - rescale _ factor % 2 ) # Corresponding bilinear filter size
 > > > num _ in _ channels = 3
 > > > num _ out _ channels = 3
 > > > deconv _ filter _ shape = ( filter _ size , filter _ size , num _ out _ channels , num _ in _ channels )
 > > > x = tf . placeholder ( tf . float32 , ( 1 , imsize , imsize , num _ channels ) )
 > > > net = tl . layers . InputLayer ( x , name = ' input _ layer ' )
 > > > bilinear _ init = deconv2d _ bilinear _ upsampling _ initializer ( shape = filter _ shape )
 > > > net = tl . layers . DeConv2dLayer ( net ,
 . . . shape = filter _ shape ,
 . . . output _ shape = ( 1 , imsize * rescale _ factor , imsize * rescale _ factor , num _ out _ channels ) ,
 . . . strides = ( 1 , rescale _ factor , rescale _ factor , 1 ) ,
 . . . W _ init = bilinear _ init ,
 . . . padding = ' SAME ' ,
 . . . act = None , name = ' g / h1 / decon2d ' )"""

implementation:
if shape [ 0 ] != shape [ 1 ] :
    raise Exception ( 'deconv2d_bilinear_upsampling_initializer only supports symmetrical filter sizes' )
if shape [ 3 ] < shape [ 2 ] :
    raise Exception ( 'deconv2d_bilinear_upsampling_initializer behaviour is not defined for num_in_channels < num_out_channels ' )
filter_size = shape [ 0 ]
num_out_channels = shape [ 2 ]
num_in_channels = shape [ 3 ]
# Create bilinear filter kernel as numpy array
bilinear_kernel = np . zeros ( [ filter_size , filter_size ] , dtype = np . float32 )
scale_factor = ( filter_size + 1 ) // 2
if filter_size % 2 == 1 :
    center = scale_factor - 1
else :
    center = scale_factor - 0.5
for x in range ( filter_size ) :
    for y in range ( filter_size ) :
        bilinear_kernel [ x , y ] = ( 1 - abs ( x - center ) / scale_factor ) * ( 1 - abs ( y - center ) / scale_factor )
weights = np . zeros ( ( filter_size , filter_size , num_out_channels , num_in_channels ) )
for i in range ( num_out_channels ) :
    weights [ : , : , i , i ] = bilinear_kernel
# assign numpy array to constant _ initalizer and pass to get _ variable
return tf . constant_initializer ( value = weights , dtype = LayersConfig . tf_dtype )