(alphas, path, widths, ignore_skip=False)
return ops, path_compact, widths_compact
alphas: [alphas0, ..., alphas3]
alphas: [alphas0, ..., alphas3]
alphas : [ alphas0 ... alphas3 ]
def alphas2ops_path_width(alphas, path, widths, ignore_skip=False):
    '''
    alphas: [alphas0, ..., alphas3]
    '''
    assert len(path) == len(widths) + 1, "len(path) %d, len(widths) %d"%(len(path), len(widths))
    ops = []
    path_compact = []
    widths_compact = []
    pos2alpha_skips = [] # (pos, alpha of skip) to be prunned
    min_len = int(np.round(len(path) / 3.)) + path[-1] * 2
    # keep record of position(s) of skip_connect
    for i in range(len(path)):
        scale = path[i]
        if ignore_skip:
            alphas[scale][i-scale][0] = -float('inf')
        op = alphas[scale][i-scale].argmax()
        if op == 0 and (i == len(path)-1 or path[i] == path[i+1]):
            # alpha not softmax yet
            pos2alpha_skips.append((i, F.softmax(alphas[scale][i-scale], dim=-1)[0]))

    pos_skips = [ pos for pos, alpha in pos2alpha_skips ]
    pos_downs = [ pos for pos in range(len(path)-1) if path[pos] < path[pos+1] ]
    if len(pos_downs) > 0:
        pos_downs.append(len(path))
        for i in range(len(pos_downs)-1):
            # cannot be all skip_connect between each downsample-pair
            # including the last down to the path-end
            pos1 = pos_downs[i]; pos2 = pos_downs[i+1]
            if pos1+1 in pos_skips and pos2-1 in pos_skips and pos_skips.index(pos2-1) - pos_skips.index(pos1+1) == (pos2-1) - (pos1+1):
                min_skip = [1, -1] # score, pos
                for j in range(pos1+1, pos2):
                    scale = path[j]
                    score = F.softmax(alphas[scale][j-scale], dim=-1)[0]
                    if score <= min_skip[0]:
                        min_skip = [score, j]
                alphas[path[min_skip[1]]][min_skip[1]-path[min_skip[1]]][0] = -float('inf')

    if len(pos2alpha_skips) > len(path) - min_len:
        pos2alpha_skips = sorted(pos2alpha_skips, key=lambda x: x[1], reverse=True)[:len(path) - min_len]
    pos_skips = [ pos for pos, alpha in pos2alpha_skips ]
    for i in range(len(path)):
        scale = path[i]
        if i < len(widths): width = widths[i]
        op = alphas[scale][i-scale].argmax()
        if op == 0:
            if i in pos_skips:
                # remove the last width if the last layer (skip_connect) is to be prunned
                if i == len(path) - 1: widths_compact = widths_compact[:-1]
                continue
            else:
                alphas[scale][i-scale][0] = -float('inf')
                op = alphas[scale][i-scale].argmax()
        path_compact.append(scale)
        if i < len(widths): widths_compact.append(width)
        ops.append(op)
    assert len(path_compact) >= min_len
    return ops, path_compact, widths_compact
def alphas2ops_path_width ( alphas , path , widths , ignore_skip = False ) : assert len ( path ) == len ( widths ) + 1 , "len(path) %d, len(widths) %d" % ( len ( path ) , len ( widths ) ) ops = [ ] path_compact = [ ] widths_compact = [ ] pos2alpha_skips = [ ] # (pos, alpha of skip) to be prunned min_len = int ( np . round ( len ( path ) / 3. ) ) + path [ - 1 ] * 2 # keep record of position(s) of skip_connect for i in range ( len ( path ) ) : scale = path [ i ] if ignore_skip : alphas [ scale ] [ i - scale ] [ 0 ] = - float ( 'inf' ) op = alphas [ scale ] [ i - scale ] . argmax ( ) if op == 0 and ( i == len ( path ) - 1 or path [ i ] == path [ i + 1 ] ) : # alpha not softmax yet pos2alpha_skips . append ( ( i , F . softmax ( alphas [ scale ] [ i - scale ] , dim = - 1 ) [ 0 ] ) ) pos_skips = [ pos for pos , alpha in pos2alpha_skips ] pos_downs = [ pos for pos in range ( len ( path ) - 1 ) if path [ pos ] < path [ pos + 1 ] ] if len ( pos_downs ) > 0 : pos_downs . append ( len ( path ) ) for i in range ( len ( pos_downs ) - 1 ) : # cannot be all skip_connect between each downsample-pair # including the last down to the path-end pos1 = pos_downs [ i ] pos2 = pos_downs [ i + 1 ] if pos1 + 1 in pos_skips and pos2 - 1 in pos_skips and pos_skips . index ( pos2 - 1 ) - pos_skips . index ( pos1 + 1 ) == ( pos2 - 1 ) - ( pos1 + 1 ) : min_skip = [ 1 , - 1 ] # score, pos for j in range ( pos1 + 1 , pos2 ) : scale = path [ j ] score = F . softmax ( alphas [ scale ] [ j - scale ] , dim = - 1 ) [ 0 ] if score <= min_skip [ 0 ] : min_skip = [ score , j ] alphas [ path [ min_skip [ 1 ] ] ] [ min_skip [ 1 ] - path [ min_skip [ 1 ] ] ] [ 0 ] = - float ( 'inf' ) if len ( pos2alpha_skips ) > len ( path ) - min_len : pos2alpha_skips = sorted ( pos2alpha_skips , key = lambda x : x [ 1 ] , reverse = True ) [ : len ( path ) - min_len ] pos_skips = [ pos for pos , alpha in pos2alpha_skips ] for i in range ( len ( path ) ) : scale = path [ i ] if i < len ( widths ) : width = widths [ i ] op = alphas [ scale ] [ i - scale ] . argmax ( ) if op == 0 : if i in pos_skips : # remove the last width if the last layer (skip_connect) is to be prunned if i == len ( path ) - 1 : widths_compact = widths_compact [ : - 1 ] continue else : alphas [ scale ] [ i - scale ] [ 0 ] = - float ( 'inf' ) op = alphas [ scale ] [ i - scale ] . argmax ( ) path_compact . append ( scale ) if i < len ( widths ) : widths_compact . append ( width ) ops . append ( op ) assert len ( path_compact ) >= min_len return ops , path_compact , widths_compact