Example #1
0
    def build(self):
        print("building the multiplication model")
        enc_size = self.size_of_env_observation()
        argument_size = IntegerArguments.size_of_arguments
        input_enc = InputLayer(batch_input_shape=(self.batch_size, enc_size), name='input_enc')
        input_arg = InputLayer(batch_input_shape=(self.batch_size, argument_size), name='input_arg')
        input_prg = Embedding(input_dim=PROGRAM_VEC_SIZE, output_dim=PROGRAM_KEY_VEC_SIZE, input_length=1,
                              batch_input_shape=(self.batch_size, 1))

        f_enc = Sequential(name='f_enc')
        f_enc.add(Merge([input_enc, input_arg], mode='concat'))
        f_enc.add(MaxoutDense(128, nb_feature=FIELD_ROW))
        self.f_enc = f_enc

        program_embedding = Sequential(name='program_embedding')
        program_embedding.add(input_prg)

        f_enc_convert = Sequential(name='f_enc_convert')
        f_enc_convert.add(f_enc)
        f_enc_convert.add(RepeatVector(1))

        f_lstm = Sequential(name='f_lstm')
        f_lstm.add(Merge([f_enc_convert, program_embedding], mode='concat'))
        f_lstm.add(LSTM(256, return_sequences=False, stateful=True, W_regularizer=l2(0.0000001)))
        f_lstm.add(Activation('relu', name='relu_lstm_1'))
        f_lstm.add(RepeatVector(1))
        f_lstm.add(LSTM(256, return_sequences=False, stateful=True, W_regularizer=l2(0.0000001)))
        f_lstm.add(Activation('relu', name='relu_lstm_2'))
        plot(f_lstm, to_file='f_lstm.png', show_shapes=True)

        f_end = Sequential(name='f_end')
        f_end.add(f_lstm)
        f_end.add(Dense(1, W_regularizer=l2(0.001)))
        f_end.add(Activation('sigmoid', name='sigmoid_end'))

        f_prog = Sequential(name='f_prog')
        f_prog.add(f_lstm)
        f_prog.add(Dense(PROGRAM_KEY_VEC_SIZE, activation="relu"))
        f_prog.add(Dense(PROGRAM_VEC_SIZE, W_regularizer=l2(0.0001)))
        f_prog.add(Activation('softmax', name='softmax_prog'))
        plot(f_prog, to_file='f_prog.png', show_shapes=True)

        f_args = []
        for ai in range(1, IntegerArguments.max_arg_num+1):
            f_arg = Sequential(name='f_arg%s' % ai)
            f_arg.add(f_lstm)
            f_arg.add(Dense(IntegerArguments.depth, W_regularizer=l2(0.0001)))
            f_arg.add(Activation('softmax', name='softmax_arg%s' % ai))
            f_args.append(f_arg)
        plot(f_arg, to_file='f_arg.png', show_shapes=True)

        self.model = Model([input_enc.input, input_arg.input, input_prg.input],
                           [f_end.output, f_prog.output] + [fa.output for fa in f_args],
                           name="npi")
        self.compile_model()
        plot(self.model, to_file='model.png', show_shapes=True)
Example #2
0
def create_model(seq_length):
    model = Sequential()
    model.add(InputLayer(input_shape=(seq_length, 13)))
    model.add(Bidirectional(LSTM(128)))
    model.add(Dense(1, activation="sigmoid"))
    model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])
    model.summary()
    return model
Example #3
0
def create_model(seq_length):
    model = Sequential()
    model.add(InputLayer(input_shape=(seq_length, 13)))
    model.add(LSTM(64, dropout_U=0.2, dropout_W=0.2))
    model.add(Dense(1, activation="sigmoid"))
    model.compile('rmsprop', 'binary_crossentropy', metrics=['accuracy'])
    model.summary()
    return model
def InputAdj(name=None, dtype=K.floatx(), sparse=False, tensor=None):
    shape = (None, None)
    input_layer = InputLayer(batch_input_shape=shape,
                             name=name,
                             sparse=sparse,
                             dtype=dtype)
    outputs = input_layer._inbound_nodes[0].output_tensors
    if len(outputs) == 1:
        return outputs[0]
    else:
        return outputs
import tensorflow as tf
from keras.engine.topology import InputLayer

from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D

input_shape = (6, 6, 2048)
num_classes_cat_1 = 49
num_classes_cat_2 = 483
num_classes_cat_3 = 5263
num_categories = 5270

with tf.name_scope('network'):
    model = Sequential()
    model.add(InputLayer(input_shape=input_shape))
    model.add(Conv2D(64,
                     kernel_size=(1, 1),
                     activation='relu'))
    model.add(Dropout(0.25))
    model.add(Conv2D(128,
                     kernel_size=(4, 4),
                     activation='relu'))
    model.add(Dropout(0.25))
    model.add(Conv2D(256,
                     kernel_size=(3, 3),
                     activation='relu'))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(num_categories, activation=None))
Example #6
0
 def __init__(self, *args, **kwargs):
     fake_model = Sequential()
     fake_model.add(InputLayer(input_shape=(1, 1, 1)))
     super().__init__(fake_model, *args, **kwargs)
    def init_models_ellipse(self, input_shape=9):
        print "init model"
        input_tensor = Input(batch_shape=(self.BATCH_SIZE, input_shape),
                             dtype='float32',
                             name='input_tensor')

        input_lay_0 = InputLayer(batch_input_shape=(self.BATCH_SIZE,
                                                    input_shape),
                                 name='input_lay_seq_0')

        model = Sequential(name='main_seq')

        model.add(Dropout(0.5, input_shape=(input_shape, )))
        model.add(
            MaxoutDense(output_dim=2048,
                        nb_feature=2,
                        weights=dense_weight_init_values(
                            model.output_shape[-1], 2048, nb_feature=2),
                        name='maxout_0'))

        model.add(Dropout(0.5))
        model.add(
            MaxoutDense(output_dim=2048,
                        nb_feature=2,
                        weights=dense_weight_init_values(
                            model.output_shape[-1], 2048, nb_feature=2),
                        name='maxout_1'))

        model.add(Dropout(0.5))
        model.add(
            Dense(units=37,
                  activation='relu',
                  kernel_initializer=initializers.RandomNormal(stddev=0.01),
                  bias_initializer=initializers.Constant(value=0.1),
                  name='dense_output'))

        model_seq = model([input_tensor])

        CATEGORISED = False  # FXME has to be implemented

        output_layer_norm = Lambda(function=OptimisedDivGalaxyOutput,
                                   output_shape=lambda x: x,
                                   arguments={
                                       'normalised': True,
                                       'categorised': CATEGORISED
                                   })(model_seq)
        output_layer_noNorm = Lambda(function=OptimisedDivGalaxyOutput,
                                     output_shape=lambda x: x,
                                     arguments={
                                         'normalised': False,
                                         'categorised': CATEGORISED
                                     })(model_seq)

        model_norm = Model(inputs=[input_tensor],
                           outputs=output_layer_norm,
                           name='full_model_norm_ellipse')
        model_norm_metrics = Model(inputs=[input_tensor],
                                   outputs=output_layer_norm,
                                   name='full_model_metrics_ellipse')
        model_noNorm = Model(inputs=[input_tensor],
                             outputs=output_layer_noNorm,
                             name='full_model_noNorm_ellipse')

        self.models = {
            'model_norm_ellipse': model_norm,
            'model_norm_metrics_ellipse': model_norm_metrics,
            'model_noNorm_ellipse': model_noNorm
        }

        self._compile_models(postfix='_ellipse')

        return self.models
    def init_models(self):
        print "init model"
        input_tensor = Input(
            batch_shape=(self.BATCH_SIZE, self.NUM_INPUT_FEATURES,
                         self.input_sizes[0][0], self.input_sizes[0][1]),
            dtype='float32',
            name='input_tensor')

        input_tensor_45 = Input(
            batch_shape=(self.BATCH_SIZE, self.NUM_INPUT_FEATURES,
                         self.input_sizes[1][0], self.input_sizes[1][1]),
            dtype='float32',
            name='input_tensor_45')

        input_lay_0 = InputLayer(
            batch_input_shape=(self.BATCH_SIZE, self.NUM_INPUT_FEATURES,
                               self.input_sizes[0][0], self.input_sizes[0][1]),
            name='input_lay_seq_0')

        input_lay_1 = InputLayer(
            batch_input_shape=(self.BATCH_SIZE, self.NUM_INPUT_FEATURES,
                               self.input_sizes[1][0], self.input_sizes[1][1]),
            name='input_lay_seq_1')

        model = Sequential(name='main_seq')

        N_INPUT_VARIATION = 2  # depends on the kaggle input settings
        include_flip = self.include_flip

        num_views = N_INPUT_VARIATION * (2 if include_flip else 1)

        model.add(
            Merge(
                [input_lay_0, input_lay_1],
                mode=kaggle_input,
                output_shape=lambda x:
                ((input_lay_0.output_shape[0] + input_lay_1.output_shape[0]
                  ) * 2 * N_INPUT_VARIATION, self.NUM_INPUT_FEATURES, self.
                 PART_SIZE, self.PART_SIZE),
                arguments={
                    'part_size': self.PART_SIZE,
                    'n_input_var': N_INPUT_VARIATION,
                    'include_flip': include_flip,
                    'random_flip': False
                },
                name='input_merge'))

        # needed for the pylearn moduls used by kerasCudaConvnetConv2DLayer and
        # kerasCudaConvnetPooling2DLayer
        model.add(fPermute((1, 2, 3, 0), name='input_perm'))

        model.add(
            kerasCudaConvnetConv2DLayer(n_filters=32,
                                        filter_size=6,
                                        untie_biases=True,
                                        name='conv_0'))
        model.add(kerasCudaConvnetPooling2DLayer(name='pool_0'))

        model.add(
            kerasCudaConvnetConv2DLayer(n_filters=64,
                                        filter_size=5,
                                        untie_biases=True,
                                        name='conv_1'))
        model.add(kerasCudaConvnetPooling2DLayer(name='pool_1'))

        model.add(
            kerasCudaConvnetConv2DLayer(n_filters=128,
                                        filter_size=3,
                                        untie_biases=True,
                                        name='conv_2'))
        model.add(
            kerasCudaConvnetConv2DLayer(n_filters=128,
                                        filter_size=3,
                                        weights_std=0.1,
                                        untie_biases=True,
                                        name='conv_3'))

        model.add(kerasCudaConvnetPooling2DLayer(name='pool_2'))

        model.add(fPermute((3, 0, 1, 2), name='cuda_out_perm'))

        model.add(
            Lambda(function=kaggle_MultiRotMergeLayer_output,
                   output_shape=lambda x:
                   (x[0] // 4 // N_INPUT_VARIATION,
                    (x[1] * x[2] * x[3] * 4 * num_views)),
                   arguments={'num_views': num_views},
                   name='conv_out_merge'))

        model.add(Dropout(0.5))
        model.add(
            MaxoutDense(output_dim=2048,
                        nb_feature=2,
                        weights=dense_weight_init_values(
                            model.output_shape[-1], 2048, nb_feature=2),
                        name='maxout_0'))

        model.add(Dropout(0.5))
        model.add(
            MaxoutDense(output_dim=2048,
                        nb_feature=2,
                        weights=dense_weight_init_values(
                            model.output_shape[-1], 2048, nb_feature=2),
                        name='maxout_1'))

        model.add(Dropout(0.5))
        model.add(
            Dense(units=37,
                  activation='relu',
                  kernel_initializer=initializers.RandomNormal(stddev=0.01),
                  bias_initializer=initializers.Constant(value=0.1),
                  name='dense_output'))

        model_seq = model([input_tensor, input_tensor_45])

        CATEGORISED = False  # FXME has to be implemented

        output_layer_norm = Lambda(function=OptimisedDivGalaxyOutput,
                                   output_shape=lambda x: x,
                                   arguments={
                                       'normalised': True,
                                       'categorised': CATEGORISED
                                   })(model_seq)
        output_layer_noNorm = Lambda(function=OptimisedDivGalaxyOutput,
                                     output_shape=lambda x: x,
                                     arguments={
                                         'normalised': False,
                                         'categorised': CATEGORISED
                                     })(model_seq)

        model_norm = Model(inputs=[input_tensor, input_tensor_45],
                           outputs=output_layer_norm,
                           name='full_model_norm')
        model_norm_metrics = Model(inputs=[input_tensor, input_tensor_45],
                                   outputs=output_layer_norm,
                                   name='full_model_metrics')
        model_noNorm = Model(inputs=[input_tensor, input_tensor_45],
                             outputs=output_layer_noNorm,
                             name='full_model_noNorm')

        self.models = {
            'model_norm': model_norm,
            'model_norm_metrics': model_norm_metrics,
            'model_noNorm': model_noNorm
        }

        self._compile_models()

        return self.models
Example #9
0
def define_mlp(input_shape,
               nb_classes,
               mlp_params=(3, 2048),
               other_params=(0., 0.5, 2.),
               hidden_drop=True,
               acol_params=(5, 0, 1, 1, 0, 0.000001, 1, 'average', False),
               init='identity_vstacked',
               null_node=False,
               truncated=False):

    nb_layers, nb_nodes = mlp_params
    p_i, p_hl, m_n = other_params
    K, p, c1, c2, c3, c4, balance_type, pooling, trainable = acol_params
    if hidden_drop and m_n:
        W_constraint = maxnorm(m_n)
    else:
        W_constraint = None

    if pooling == 'average':
        AcolPooling = AveragePooling
    elif pooling == 'max':
        AcolPooling = MaxPooling

    model = Sequential()
    if hidden_drop and p_i:
        model.add(Dropout(p_i, input_shape=input_shape))
    else:
        model.add(InputLayer(input_shape=input_shape))
    for layer in range(nb_layers):
        model.add(Dense(nb_nodes, activation='relu',
                        W_constraint=W_constraint))
        if hidden_drop and p_hl:
            model.add(Dropout(p_hl))

    if balance_type == 0:
        model.add(
            Dense(nb_classes * K,
                  activity_regularizer=activity_acol_for_dropout(c1),
                  name='L-1'))
    elif balance_type < 3:
        model.add(
            Dense(nb_classes * K,
                  activity_regularizer=activity_acol(c1, c2, c3, c4,
                                                     balance_type),
                  name='L-1'))
    else:
        model.add(
            Dense(nb_classes * K,
                  activity_regularizer=activity_acol_null(
                      c1, c2, c3, c4, K, balance_type),
                  name='L-1'))
    model.add(Dropout(p))

    if not truncated:
        model.add(Activation('softmax', name='L-1_activation'))
        if null_node:
            model.add(
                AcolPooling(nb_classes + 1,
                            trainable=trainable,
                            init='column_vstacked_nullnode',
                            name='AcolPooling'))
        else:
            model.add(
                AcolPooling(nb_classes,
                            trainable=trainable,
                            init=init,
                            name='AcolPooling'))

    return model