(reduce_target, lengths, dim)
return red_avg
Args:
        reduce_target : shape(d_0, d_1,..,d_dim, .., d_k)
        lengths : shape(d0, .., d_(dim-1))
        dim : which dimension to average, should be a python number
Args:
        reduce_target : shape(d_0, d_1,..,d_dim, .., d_k)
        lengths : shape(d0, .., d_(dim-1))
        dim : which dimension to average, should be a python number
Args : reduce_target : shape ( d_0 d_1 .. d_dim .. d_k ) lengths : shape ( d0 .. d_ ( dim - 1 )) dim : which dimension to average should be a python number
def reduce_avg(reduce_target, lengths, dim):
    """
    Args:
        reduce_target : shape(d_0, d_1,..,d_dim, .., d_k)
        lengths : shape(d0, .., d_(dim-1))
        dim : which dimension to average, should be a python number
    """
    shape_of_lengths = lengths.get_shape()
    shape_of_target = reduce_target.get_shape()
    if len(shape_of_lengths) != dim:
        raise ValueError(('Second input tensor should be rank %d, ' +
                         'while it got rank %d') % (dim, len(shape_of_lengths)))
    if len(shape_of_target) < dim+1 :
        raise ValueError(('First input tensor should be at least rank %d, ' +
                         'while it got rank %d') % (dim+1, len(shape_of_target)))

    rank_diff = len(shape_of_target) - len(shape_of_lengths) - 1
    mxlen = tf.shape(reduce_target)[dim]
    mask = mkMask(lengths, mxlen)
    if rank_diff!=0:
        len_shape = tf.concat(axis=0, values=[tf.shape(lengths), [1]*rank_diff])
        mask_shape = tf.concat(axis=0, values=[tf.shape(mask), [1]*rank_diff])
    else:
        len_shape = tf.shape(lengths)
        mask_shape = tf.shape(mask)
    lengths_reshape = tf.reshape(lengths, shape=len_shape)
    mask = tf.reshape(mask, shape=mask_shape)

    mask_target = reduce_target * tf.cast(mask, dtype=reduce_target.dtype)

    red_sum = tf.reduce_sum(mask_target, axis=[dim], keep_dims=False)
    red_avg = red_sum / (tf.to_float(lengths_reshape) + 1e-30)
    return red_avg
def reduce_avg ( reduce_target , lengths , dim ) : shape_of_lengths = lengths . get_shape ( ) shape_of_target = reduce_target . get_shape ( ) if len ( shape_of_lengths ) != dim : raise ValueError ( ( 'Second input tensor should be rank %d, ' + 'while it got rank %d' ) % ( dim , len ( shape_of_lengths ) ) ) if len ( shape_of_target ) < dim + 1 : raise ValueError ( ( 'First input tensor should be at least rank %d, ' + 'while it got rank %d' ) % ( dim + 1 , len ( shape_of_target ) ) ) rank_diff = len ( shape_of_target ) - len ( shape_of_lengths ) - 1 mxlen = tf . shape ( reduce_target ) [ dim ] mask = mkMask ( lengths , mxlen ) if rank_diff != 0 : len_shape = tf . concat ( axis = 0 , values = [ tf . shape ( lengths ) , [ 1 ] * rank_diff ] ) mask_shape = tf . concat ( axis = 0 , values = [ tf . shape ( mask ) , [ 1 ] * rank_diff ] ) else : len_shape = tf . shape ( lengths ) mask_shape = tf . shape ( mask ) lengths_reshape = tf . reshape ( lengths , shape = len_shape ) mask = tf . reshape ( mask , shape = mask_shape ) mask_target = reduce_target * tf . cast ( mask , dtype = reduce_target . dtype ) red_sum = tf . reduce_sum ( mask_target , axis = [ dim ] , keep_dims = False ) red_avg = red_sum / ( tf . to_float ( lengths_reshape ) + 1e-30 ) return red_avg