def BL(template, dropout=0.1, regularizer=None, constraint=None):
    """
    
    inputs
    ----
    template: a list of network dimensions including input and output, e.g., [[40,10], [120,5], [3,1]]
    dropout: dropout percentage
    regularizer: keras regularizer object
    constraint: keras constraint object
    
    outputs
    ------
    keras model object
    """

    inputs = keras.layers.Input(template[0])

    x = inputs
    for k in range(1, len(template) - 1):
        x = Layers.BL(template[k], regularizer, constraint)(x)
        x = keras.layers.Activation('relu')(x)
        x = keras.layers.Dropout(dropout)(x)

    x = Layers.BL(template[-1], regularizer, constraint)(x)
    outputs = keras.layers.Activation('softmax')(x)

    model = keras.Model(inputs=inputs, outputs=outputs)

    optimizer = keras.optimizers.Adam(0.01)

    model.compile(optimizer, 'categorical_crossentropy', [
        'acc',
    ])

    return model
def get_nonlinear_encoder(inputs,
                          input_shape,
                          target_shape,
                          prefix=None,
                          complexity='low',
                          conv_regularizer=None,
                          conv_constraint=None,
                          bl_regularizer=None,
                          bl_constraint=None):

    h_scale = np.log2(np.floor(float(input_shape[0]) / target_shape[0]))
    w_scale = np.log2(np.floor(float(input_shape[1]) / target_shape[1]))

    # number of upsampling step
    nb_downsampling = max(1, int(min(h_scale, w_scale)))

    if complexity == 'low':
        nb_conv_per_block = 3 if target_shape[0] == target_shape[
            1] and target_shape[0] == 6 else 4
    else:
        nb_conv_per_block = 4 if target_shape[0] == target_shape[
            1] and target_shape[0] == 6 else 3

    nb_filter1 = 32
    nb_filter2 = 64

    for down_iter in range(nb_downsampling):
        suffix = '_' + str(down_iter)
        inputs = downsampling_block(inputs, nb_filter1, nb_filter2,
                                    nb_conv_per_block, prefix, suffix,
                                    conv_regularizer, conv_constraint)

        nb_filter1 = nb_filter2
        nb_filter2 = min(nb_filter2 * 2, 256)

    if nb_downsampling == 1 and complexity == 'high':
        inputs = downsampling_block(inputs, nb_filter1, nb_filter2,
                                    nb_conv_per_block, prefix, '_1',
                                    conv_regularizer, conv_constraint, False)

    name = 'nonlinear_encoder_bl' if prefix is None else prefix + 'nonlinear_encoder_bl'

    outputs = Layers.BL(target_shape,
                        kernel_regularizer=bl_regularizer,
                        kernel_constraint=bl_constraint,
                        name=name)(inputs)

    return outputs
示例#3
0
def TABL(template,
         dropout=0.1,
         projection_regularizer=None,
         projection_constraint=None,
         attention_regularizer=None,
         attention_constraint=None):
    """
    Temporal Attention augmented Bilinear Layer network, refer to the paper https://arxiv.org/abs/1712.00975
    
    inputs
    ----
    template: a list of network dimensions including input and output, e.g., [[40,10], [120,5], [3,1]]
    dropout: dropout percentage
    projection_regularizer: keras regularizer object for projection matrices
    projection_constraint: keras constraint object for projection matrices
    attention_regularizer: keras regularizer object for attention matrices
    attention_constraint: keras constraint object for attention matrices
    
    outputs
    ------
    keras model object
    """

    inputs = keras.layers.Input(template[0])

    x = inputs
    for k in range(1, len(template) - 1):
        x = Layers.BL(template[k], projection_regularizer,
                      projection_constraint)(x)
        x = keras.layers.Activation('relu')(x)
        x = keras.layers.Dropout(dropout)(x)

    x = Layers.TABL(template[-1], projection_regularizer,
                    projection_constraint, attention_regularizer,
                    attention_constraint)(x)
    outputs = keras.layers.Activation('softmax')(x)

    model = keras.Model(inputs=inputs, outputs=outputs)

    optimizer = keras.optimizers.Adam(0.01)

    model.compile(optimizer, 'categorical_crossentropy', [
        'acc',
    ])

    return model