def build_model_combine_features(self, load_weight=False):
        cnn_branch = Sequential()
        cnn_branch.add(
            Conv2D(filters=16, kernel_size=5, strides=1, padding='valid', activation='relu', input_shape=(11, 11, 3),
                   name='Conv1'))
        cnn_branch.add(Conv2D(filters=24, kernel_size=3, strides=1, padding='same', activation='relu', name='Conv2'))
        cnn_branch.add(Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu', name='Conv3'))
        cnn_branch.add(MaxPooling2D(pool_size=(3, 3), strides=2))
        cnn_branch.add(Conv2D(filters=64, kernel_size=3, strides=1, padding='same', activation='relu', name='Conv4'))
        cnn_branch.add(MaxPooling2D(pool_size=(3, 3), strides=2))
        cnn_branch.add(Conv2D(filters=96, kernel_size=3, strides=1, padding='same', activation='relu', name='Conv5'))
        cnn_branch.add(Flatten())

        location_branch = Sequential()
        location_branch.add(Dense(2, input_shape=(2,), activation='relu'))

        model = Concatenate([location_branch, cnn_branch])
        model.add(Dense(500, activation='relu'))
        model.add(Dense(2, activation='softmax'))
        model.compile(optimizer=Adam(lr=self.lr), loss='categorical_crossentropy', metrics=['accuracy'])

        if load_weight:
            print("Loading weight...")
            model.load_weight(WEIGHT_DIR + "")
            print("Weight loaded.")

        return model
    def build_bi_singlelstm_model(self, word_vector):
        print("load model")
        left = Input(shape=(self.maxlen, ))
        left_embedding = Embedding(self.vocab_num + 1,
                                   self.embedding_dim,
                                   weights=[word_vector],
                                   input_length=self.maxlen,
                                   mask_zero=True,
                                   trainable=True)(left)

        right = Input(shape=(self.maxlen, ))
        right_embedding = Embedding(self.dictFeatureNum,
                                    self.dict_embedding_size,
                                    input_length=self.maxlen,
                                    mask_zero=True,
                                    trainable=True)(right)  ## 0~24

        model = Concatenate(axis=-1)([left_embedding, right_embedding])
        model = Bidirectional(
            LSTM(self.bilstm_hidden_dim,
                 recurrent_dropout=0.1,
                 return_sequences=True))(model)
        model = LSTM(self.lstm_hidden_dim,
                     recurrent_dropout=self.dropout_rate,
                     return_sequences=True)(model)
        out = TimeDistributed(Dense(self.cate_num,
                                    activation="softmax"))(model)
        model = Model(input=[left, right], output=out)
        adam = optimizers.Adam(lr=self.lr)
        model.compile(optimizer=adam,
                      loss="binary_crossentropy",
                      metrics=["accuracy"])
        return model
Example #3
0
def fcnmodel(num_classes, yolo_size, img_model, img_type="vgg16"):
    # get input objects
    # get genres as input labels
    yolo_model = Sequential()
    yolo_model.add(
        Dense(units=yolo_size, activation="relu", input_shape=(1, yolo_size)))
    yolo_model.add(Dense(units=1024, activation="relu"))
    yolo_model.add(Dense(units=4096, activation="relu"))
    yolo_model.add(Flatten())
    yolo_model.add(BatchNormalization())

    if (img_type == "vgg16"):
        img_model.add(Flatten())
        img_model.add(BatchNormalization())

    model = Concatenate()([yolo_model.output, img_model.output])
    #model = Flatten()(model)
    model = Dense(units=4096, activation='relu')(model)
    model = Dense(units=num_classes, activation="sigmoid")(model)

    model = models.Model([yolo_model.input, img_model.input],
                         model,
                         name='fcnet')

    top3_acc = functools.partial(metric.top_categorical_accuracy,
                                 num_classes=num_classes)
    top3_acc.__name__ = 'top3_accuracy'
    model.compile(optimizer='adam',
                  loss=keras.losses.binary_crossentropy,
                  metrics=[
                      top3_acc, metrics.categorical_accuracy,
                      metrics.binary_accuracy
                  ])
    return model
Example #4
0
    def build_model_combine_features(self, load_weight=False):
        cnn_branch = Sequential()
        cnn_branch.add(
            Conv2D(filters=16,
                   kernel_size=5,
                   strides=1,
                   padding='valid',
                   activation='relu',
                   input_shape=(11, 11, 3),
                   name='Conv1'))
        cnn_branch.add(
            Conv2D(filters=24,
                   kernel_size=3,
                   strides=1,
                   padding='same',
                   activation='relu',
                   name='Conv2'))
        cnn_branch.add(
            Conv2D(filters=32,
                   kernel_size=3,
                   strides=1,
                   padding='same',
                   activation='relu',
                   name='Conv3'))
        cnn_branch.add(MaxPooling2D(pool_size=(3, 3), strides=2))
        cnn_branch.add(
            Conv2D(filters=64,
                   kernel_size=3,
                   strides=1,
                   padding='same',
                   activation='relu',
                   name='Conv4'))
        cnn_branch.add(MaxPooling2D(pool_size=(3, 3), strides=2))
        cnn_branch.add(
            Conv2D(filters=96,
                   kernel_size=3,
                   strides=1,
                   padding='same',
                   activation='relu',
                   name='Conv5'))
        cnn_branch.add(Flatten())

        location_branch = Sequential()
        location_branch.add(Dense(2, input_shape=(2, ), activation='relu'))

        model = Concatenate([location_branch, cnn_branch])
        model.add(Dense(500, activation='relu'))
        model.add(Dense(2, activation='softmax'))
        model.compile(optimizer=Adam(lr=self.lr),
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])

        if load_weight:
            print("Loading weight...")
            model.load_weight(WEIGHT_DIR + "")
            print("Weight loaded.")

        return model
Example #5
0
def get_2d_lstm_model():

    input_A = Input(shape=(None, 108, 192, 3))

    image_model = TimeDistributed(
        MobileNet(input_shape=(108, 192, 3),
                  alpha=0.25,
                  depth_multiplier=1,
                  dropout=1e-3,
                  include_top=False,
                  weights=None,
                  input_tensor=None,
                  pooling='avg'))(input_A)

    image_model = Bidirectional(LSTM(16, activation=None))(image_model)

    image_model = BatchNormalization()(image_model)

    image_model = Activation('relu')(image_model)

    input_B = Input(shape=(128, 259, 1))

    audio_model = MobileNet(input_shape=(128, 259, 1),
                            alpha=0.25,
                            depth_multiplier=1,
                            dropout=1e-3,
                            include_top=False,
                            weights=None,
                            input_tensor=None,
                            pooling='avg')(input_B)

    audio_model = Dense(32)(audio_model)

    audio_model = BatchNormalization()(audio_model)

    audio_model = Activation('relu')(audio_model)

    model = Concatenate()([image_model, audio_model])

    model = Dense(1, activation='sigmoid')(model)

    model = Model(inputs=[input_A, input_B], outputs=model)

    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    print_summary(model, line_length=120)
    #plot_model(model, show_shapes=True, to_file='model.png')

    return model
Example #6
0
def EnhancedHybridResSppNet(class_num, enhanced_class_num):
    _input = Input(shape=(None, None, 3))
    model = _input
    model = ZeroPadding2D((3, 3))(model)
    model = Conv2D(64, (7, 7), strides=(2, 2))(model)
    model = BatchNormalization(axis=3)(model)
    model = Activation('relu')(model)
    model = MaxPooling2D((3, 3), strides=(2, 2))(model)

    model = conv_block(model,
                       3, [64, 64, 256],
                       stage=2,
                       block='a',
                       strides=(1, 1))
    model = identity_block(model, 3, [64, 64, 256], stage=2, block='b')
    model = identity_block(model, 3, [64, 64, 256], stage=2, block='c')

    model = conv_block(model, 3, [128, 128, 512], stage=3, block='a')
    model = identity_block(model, 3, [128, 128, 512], stage=3, block='b')
    model = identity_block(model, 3, [128, 128, 512], stage=3, block='c')
    model = identity_block(model, 3, [128, 128, 512], stage=3, block='d')

    model = MaxPooling2D((2, 2))(model)

    model = SpatialPyramidPooling([1, 2, 4])(model)

    model1 = Dense(units=class_num)(model)
    model1 = Activation(activation="softmax")(model1)
    model1 = Model(_input, model1)
    model1.compile(loss="categorical_crossentropy",
                   optimizer=RMSprop(lr=1e-4, decay=1e-6),
                   metrics=['accuracy'])

    model2 = Dense(units=enhanced_class_num)(model)
    model2 = Activation(activation="softmax")(model2)
    model2 = Model(_input, model2)
    model2.compile(loss="categorical_crossentropy",
                   optimizer=RMSprop(lr=1e-4, decay=1e-6),
                   metrics=['accuracy'])

    input2 = Input(shape=(100, ))

    model3 = Concatenate((input2, model))
    model3 = Dense(units=class_num)(model3)
    model3 = Activation(activation="softmax")(model3)
    model3 = Model(inputs=[_input, input2], outputs=model3)
    model3.compile(loss="categorical_crossentropy",
                   optimizer=RMSprop(lr=1e-4, decay=1e-6),
                   metrics=['accuracy'])

    return model1, model2, model3
Example #7
0
def get_3d_cnn_model_image_audio():
    input_A = Input(shape=(4, 108, 192, 3))

    image_model = Convolution3D(filters=32,
                                kernel_size=(3, 3, 3),
                                activation='relu',
                                padding='same',
                                data_format='channels_last')(input_A)

    image_model = MaxPooling3D(pool_size=(2, 2, 2))(image_model)

    image_model = Flatten()(image_model)

    image_model = Dense(32)(image_model)

    image_model = BatchNormalization()(image_model)

    image_model = Activation('relu')(image_model)

    input_B = Input(shape=(128, 345, 1))

    audio_model = MobileNet(input_shape=(128, 345, 1),
                            alpha=0.25,
                            depth_multiplier=1,
                            dropout=1e-3,
                            include_top=False,
                            weights=None,
                            input_tensor=None,
                            pooling='avg')(input_B)

    audio_model = Dense(32)(audio_model)

    audio_model = BatchNormalization()(audio_model)

    audio_model = Activation('relu')(audio_model)

    model = Concatenate()([image_model, audio_model])

    model = Dense(1, activation='sigmoid')(model)

    model = Model(inputs=[input_A, input_B], outputs=model)

    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    print_summary(model, line_length=120)

    return model
Example #8
0
def model_cnn():
    # 1D Conv Layer with multiple possible kernel sizes
    inputs = Input(shape=(3, 2304))

    model = Conv1D(filters=300,
                   kernel_size=3,
                   padding='valid',
                   activation='relu',
                   kernel_regularizer=regularizers.l2(0.001),
                   strides=1)(inputs)
    model = GlobalMaxPooling1D()(model)

    flat_input = Flatten()(inputs)
    flat_input = Dense(1024,
                       activation='relu',
                       kernel_regularizer=regularizers.l2(0.01),
                       activity_regularizer=regularizers.l2(0.01))(flat_input)
    flat_input = Dropout(0.5)(flat_input)

    flat_input = Dense(512,
                       activation='relu',
                       kernel_regularizer=regularizers.l2(0.01),
                       activity_regularizer=regularizers.l2(0.01))(flat_input)
    flat_input = Dropout(0.5)(flat_input)

    model = Concatenate()([model, flat_input])

    model = Dense(264,
                  activation='relu',
                  kernel_regularizer=regularizers.l2(0.01))(model)
    model = Dropout(0.5)(model)
    model = Dense(64,
                  activation='relu',
                  kernel_regularizer=regularizers.l2(0.01),
                  activity_regularizer=regularizers.l2(0.01))(model)
    model = Dropout(0.3)(model)
    predictions = Dense(4, activation='softmax')(model)
    model = Model(inputs=inputs, outputs=predictions)
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    return model
    def build_crf_model(self, word_vector):
        print("load model")
        left = Input(shape=(self.maxlen, ))
        left_embedding = Embedding(self.vocab_num + 1,
                                   self.embedding_dim,
                                   weights=[word_vector],
                                   input_length=self.maxlen,
                                   mask_zero=True,
                                   trainable=True)(left)

        right = Input(shape=(self.maxlen, ))
        right_embedding = Embedding(self.dictFeatureNum,
                                    self.dict_embedding_size,
                                    input_length=self.maxlen,
                                    mask_zero=True,
                                    trainable=True)(right)  ## 0~24

        model = Concatenate(axis=-1)([left_embedding, right_embedding])

        model = Bidirectional(
            LSTM(self.bilstm_hidden_dim,
                 recurrent_dropout=self.dropout_rate,
                 return_sequences=True))(model)
        model = LSTM(self.lstm_hidden_dim,
                     recurrent_dropout=self.dropout_rate,
                     return_sequences=True)(model)
        model = TimeDistributed(Dense(50, activation="relu"))(model)
        # model = TimeDistributed(Dense(50, activation="relu"))(model)
        crf = CRF(self.cate_num)
        out = crf(model)
        adam = optimizers.Adam(lr=self.lr)
        # output = Dense(self.catenum, activation='softmax')(single_drop)
        model = Model(input=[left, right], output=out)
        model.compile(optimizer=adam,
                      loss=crf.loss_function,
                      metrics=[crf.accuracy])
        return model
y = squeeze_excite_block(y)

y = Conv1D(128, 3, padding = "same", kernel_initializer = "he_uniform")(y)
y = BatchNormalization()(y)
y = Activation("relu")(y)

y = GlobalAveragePooling1D()(y)

regressor = Concatenate(axis = -1)([x, y])

#Adding the output layer
regressor_final = Dense(units = 2, activation = 'softmax')(regressor)
regressor = Model(inputs = ip, outputs = regressor_final)

#Compiling the RNN
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')

y_train = to_categorical(y_train, len(np.unique(y_train)))

#Fitting the RNN to the training set
regressor.fit(X_train, y_train, epochs = 20, batch_size = 32)

# Part 3 - Making the predictions and visualising the results

# Getting the real stock price of 2017
dataset_test = pd.read_csv('AAPL_test.csv')
real_stock_price = dataset_test.iloc[:, 1:2].values

#Getting the predicted stock price of 2017
dataset_total = pd.concat((dataset_train['Open'], dataset_test['Open']), axis = 0)
inputs = dataset_total[len(dataset_total) - len(dataset_test) - 100:].values
def main(data_path, output_path):

    X_trainS1, X_trainS2, X_trainS3, Y_train, X_valS1, X_valS2, X_valS3, Y_val = load_data(
        data_path)

    epochs = 100
    batch_size = 256
    kernel_size = 3
    pool_size = 2
    dropout_rate = 0.15
    n_classes = 6

    f_act = 'relu'

    # 三个子模型的输入数据
    main_input1 = Input(shape=(128, 3), name='main_input1')
    main_input2 = Input(shape=(128, 3), name='main_input2')
    main_input3 = Input(shape=(128, 3), name='main_input3')

    def cnn_lstm_cell(main_input):
        """
        基于DeepConvLSTM算法, 创建子模型
        :param main_input: 输入数据
        :return: 子模型
        """
        sub_model = Conv1D(512,
                           kernel_size,
                           input_shape=(128, 3),
                           activation=f_act,
                           padding='same')(main_input)
        sub_model = BatchNormalization()(sub_model)
        sub_model = MaxPooling1D(pool_size=pool_size)(sub_model)
        sub_model = Dropout(dropout_rate)(sub_model)
        sub_model = Conv1D(64, kernel_size, activation=f_act,
                           padding='same')(sub_model)
        sub_model = BatchNormalization()(sub_model)
        sub_model = MaxPooling1D(pool_size=pool_size)(sub_model)
        sub_model = Dropout(dropout_rate)(sub_model)
        sub_model = Conv1D(32, kernel_size, activation=f_act,
                           padding='same')(sub_model)
        sub_model = BatchNormalization()(sub_model)
        sub_model = MaxPooling1D(pool_size=pool_size)(sub_model)
        sub_model = LSTM(128, return_sequences=True)(sub_model)
        sub_model = LSTM(128, return_sequences=True)(sub_model)
        sub_model = LSTM(128)(sub_model)
        main_output = Dropout(dropout_rate)(sub_model)
        return main_output

    first_model = cnn_lstm_cell(main_input1)
    second_model = cnn_lstm_cell(main_input2)
    third_model = cnn_lstm_cell(main_input3)

    model = Concatenate()([first_model, second_model, third_model])  # 合并模型
    model = Dropout(0.4)(model)
    model = Dense(n_classes)(model)
    model = BatchNormalization()(model)
    output = Activation('softmax', name="softmax")(model)

    model = Model([main_input1, main_input2, main_input3], output)
    model.compile(loss='categorical_crossentropy',
                  optimizer='rmsprop',
                  metrics=['accuracy'])

    #    graph_path = os.path.join(output_path, "merged_model.png")
    #    plot_model(model, to_file=graph_path, show_shapes=True)  # 绘制模型图

    metrics = Metrics()  # 度量FPR
    history = model.fit([X_trainS1, X_trainS2, X_trainS3],
                        Y_train,
                        batch_size=batch_size,
                        validation_data=([X_valS1, X_valS2, X_valS3], Y_val),
                        epochs=epochs,
                        callbacks=[metrics])  # 增加FPR输出

    model_path = os.path.join(output_path, "merged_dcl.h5")
    model.save(model_path)  # 存储模型
    print(history.history)
Example #12
0
def create_conditional_model(**kwargs):
    #
    #  Parse settings
    #
    dropout = kwargs.get("dropout", -1.)
    leaky_relu = kwargs.get("leaky_relu", 0.2)
    name = kwargs.get("name", "model")
    num_outputs = kwargs.get("num_outputs")
    num_conditions = kwargs.get("num_conditions")
    num_observables = kwargs.get("num_observables")
    use_batch_norm = kwargs.get("batch_norm", False)
    verbose = kwargs.get("verbose", True)

    use_dropout = False
    if dropout > 0: use_dropout = True

    data_layers = kwargs.get("data_layers", (10, ))
    condition_layers = kwargs.get("condition_layers", (10, ))
    combined_layers = kwargs.get("combined_layers", (
        20,
        20,
    ))
    #
    #  Print stage
    #
    if verbose:
        print(
            f"Creating model with {num_observables} observables and {num_conditions} conditions"
        )
    #
    #  Create input layers
    #
    data_input = Input((num_observables, ))
    condition_input = Input((num_conditions, ))
    #
    #  Create initially separate layers for the condition and data
    #
    model_data = data_input
    for layer_size in data_layers:
        model_data = Dense(layer_size)(model_data)
        model_data = LeakyReLU(leaky_relu)(model_data)
        if use_batch_norm: model_data = BatchNormalization()(model_data)
        if use_dropout: model_data = Dropout(dropout)(model_data)

    model_condition = condition_input
    for layer_size in condition_layers:
        model_condition = Dense(layer_size)(model_condition)
        model_condition = LeakyReLU(leaky_relu)(model_condition)
        if use_batch_norm:
            model_condition = BatchNormalization()(model_condition)
        if use_dropout: model_condition = Dropout(dropout)(model_condition)
    #
    #  Concatenate the condition and data latent states
    #
    model = Concatenate()([discriminator_data, discriminator_condition])
    #
    #  Create final model layers
    #
    for layer_size in combined_layers:
        model = Dense(layer_size)(model)
        model = LeakyReLU(leaky_relu)(model)
        if use_batch_norm: model = BatchNormalization()(model)
        if use_dropout: model = Dropout(dropout)(model)
    #
    #  Compile model
    #
    model = Dense(num_outputs, activation="linear")(model)
    model = Model(name=name,
                  inputs=[data_input, condition_input],
                  outputs=[model])
    model.compile(loss="mse", optimizer=Adam())
    if verbose: model.summary()
    #
    #  return model
    #
    return model
Example #13
0
def create_critic_generator_wgan(**kwargs):
    #
    #  Parse settings
    #
    dropout = kwargs.get("dropout", -1.)
    GAN_noise_size = kwargs.get("GAN_noise_size", 3)
    leaky_relu = kwargs.get("leaky_relu", 0.2)
    num_observables = kwargs.get("num_observables")
    num_conditions = kwargs.get("num_conditions")
    verbose = kwargs.get("verbose", True)
    use_batch_norm = kwargs.get("batch_norm", False)

    use_dropout = False
    if dropout > 0: use_dropout = True

    critic_data_layers = kwargs.get("critic_data_layers", (10, ))
    critic_condition_layers = kwargs.get("critic_condition_layers", (10, ))
    critic_combined_layers = kwargs.get("critic_combined_layers", (
        20,
        20,
    ))

    generator_noise_layers = kwargs.get("generator_noise_layers", (20, ))
    generator_condition_layers = kwargs.get("generator_condition_layers",
                                            (10, ))
    generator_combined_layers = kwargs.get("generator_combined_layers", (
        20,
        20,
    ))
    #
    #  Print stage
    #
    if verbose:
        print(
            f"Creating WGAN with {num_observables} observables and {num_conditions} conditions"
        )
    #
    #  Create input layers
    #
    data_input = Input((num_observables, ))
    condition_input = Input((num_conditions, ))
    noise_input = Input((GAN_noise_size, ))
    #
    #  Create initially separate layers for the condition and data (critic)
    #
    critic_data = data_input
    for layer_size in critic_data_layers:
        critic_data = Dense(layer_size)(critic_data)
        critic_data = LeakyReLU(leaky_relu)(critic_data)
        if use_dropout: critic_data = Dropout(dropout)(critic_data)

    critic_condition = condition_input
    for layer_size in critic_condition_layers:
        critic_condition = Dense(layer_size)(critic_condition)
        critic_condition = LeakyReLU(leaky_relu)(critic_condition)
        if use_dropout: critic_condition = Dropout(dropout)(critic_condition)
    #
    #  Concatenate the condition and data latent states (critic)
    #
    critic = Concatenate()([critic_data, critic_condition])
    #
    #  Create final critic layers
    #
    for layer_size in critic_combined_layers:
        critic = Dense(layer_size)(critic)
        critic = LeakyReLU(leaky_relu)(critic)
        if use_dropout: critic = Dropout(dropout)(critic)
    #
    #  Compile critic model
    #
    critic = Dense(1, activation="linear")(critic)
    critic = Model(name="Critic",
                   inputs=[data_input, condition_input],
                   outputs=[critic])
    critic.compile(loss=wasserstein_loss,
                   optimizer=RMSprop(learning_rate=5e-5, rho=0))
    if verbose: critic.summary()
    #
    #  Create initially separate layers for the noise and data (generator)
    #
    generator_noise = noise_input
    for layer_size in generator_noise_layers:
        generator_noise = Dense(layer_size)(generator_noise)
        generator_noise = LeakyReLU(leaky_relu)(generator_noise)
        if use_batch_norm:
            generator_noise = BatchNormalization()(generator_noise)

    generator_condition = condition_input
    for layer_size in generator_condition_layers:
        generator_condition = Dense(layer_size)(generator_condition)
        generator_condition = LeakyReLU(leaky_relu)(generator_condition)
        if use_batch_norm:
            generator_condition = BatchNormalization()(generator_condition)
    #
    #  Concatenate the condition and noise latent states (generator)
    #
    generator = Concatenate()([generator_noise, generator_condition])
    #
    #  Create final generator layers
    #
    for layer_size in generator_combined_layers:
        generator = Dense(layer_size)(generator)
        generator = LeakyReLU(leaky_relu)(generator)
        if use_batch_norm: generator = BatchNormalization()(generator)
    #
    #  Compile generator model
    #
    generator = Dense(num_observables, activation="linear")(generator)
    generator = Model(name="Generator",
                      inputs=[noise_input, condition_input],
                      outputs=[generator])
    if verbose: generator.summary()
    #
    #  Create and compile GAN
    #
    GAN = critic([generator([noise_input, condition_input]), condition_input])
    GAN = Model([noise_input, condition_input], GAN, name="GAN")
    critic.trainable = False
    GAN.compile(loss=wasserstein_loss,
                optimizer=RMSprop(learning_rate=5e-5, rho=0))
    if verbose: GAN.summary()
    #
    #  return critic, generator, GAN
    #
    return critic, generator, GAN
Example #14
0
def create_conditional_discriminator(**kwargs):
    #
    #  Parse settings
    #
    dropout = kwargs.get("dropout", -1.)
    leaky_relu = kwargs.get("leaky_relu", 0.2)
    num_categories = kwargs.get("num_categories")
    num_conditions = kwargs.get("num_conditions")
    num_observables = kwargs.get("num_observables")
    use_batch_norm = kwargs.get("batch_norm", False)
    verbose = kwargs.get("verbose", True)

    use_dropout = False
    if dropout > 0: use_dropout = True

    data_layers = kwargs.get("data_layers", (10, ))
    condition_layers = kwargs.get("condition_layers", (10, ))
    combined_layers = kwargs.get("combined_layers", (
        20,
        20,
    ))
    #
    #  Print stage
    #
    if verbose:
        print(
            f"Creating discriminator with {num_observables} observables and {num_conditions} conditions"
        )
    #
    #  Create input layers
    #
    data_input = Input((num_observables, ))
    condition_input = Input((num_conditions, ))
    #
    #  Create initially separate layers for the condition and data
    #
    discriminator_data = data_input
    for layer_size in data_layers:
        discriminator_data = Dense(layer_size)(discriminator_data)
        discriminator_data = LeakyReLU(leaky_relu)(discriminator_data)
        if use_batch_norm:
            discriminator_data = BatchNormalization()(discriminator_data)
        if use_dropout:
            discriminator_data = Dropout(dropout)(discriminator_data)

    discriminator_condition = condition_input
    for layer_size in condition_layers:
        discriminator_condition = Dense(layer_size)(discriminator_condition)
        discriminator_condition = LeakyReLU(leaky_relu)(
            discriminator_condition)
        if use_batch_norm:
            discriminator_condition = BatchNormalization()(
                discriminator_condition)
        if use_dropout:
            discriminator_condition = Dropout(dropout)(discriminator_condition)
    #
    #  Concatenate the condition and data latent states
    #
    discriminator = Concatenate()(
        [discriminator_data, discriminator_condition])
    #
    #  Create final discriminator layers
    #
    for layer_size in combined_layers:
        discriminator = Dense(layer_size)(discriminator)
        discriminator = LeakyReLU(leaky_relu)(discriminator)
        if use_batch_norm: discriminator = BatchNormalization()(discriminator)
        if use_dropout: discriminator = Dropout(dropout)(discriminator)
    #
    #  Compile discriminator model
    #
    discriminator = Dense(num_categories, activation="sigmoid")(discriminator)
    discriminator = Model(name="Discriminator",
                          inputs=[data_input, condition_input],
                          outputs=[discriminator])
    if num_categories == 1:
        discriminator.compile(loss="binary_crossentropy", optimizer=Adam())
    else:
        discriminator.compile(loss="categorical_crossentropy",
                              optimizer=Adam())
    if verbose: discriminator.summary()
    #
    #  return discriminator
    #
    return discriminator
Example #15
0
def create_model(words,
                 chars,
                 max_len,
                 n_words,
                 n_tags,
                 max_len_char,
                 n_pos,
                 n_chars,
                 embedding_mats,
                 use_word=True,
                 use_pos=False,
                 embedding_matrix=None,
                 embed_dim=70,
                 trainable=True,
                 input_dropout=False,
                 stack_lstm=1,
                 epochs=100,
                 early_stopping=True,
                 patience=20,
                 min_delta=0.0001,
                 use_char=False,
                 crf=False,
                 add_random_embedding=True,
                 pretrained_embed_dim=300,
                 stack_cross=False,
                 stack_double=False,
                 rec_dropout=0.1,
                 validation_split=0.1,
                 output_dropout=False,
                 optimizer='rmsprop',
                 pos_dropout=None,
                 char_dropout=False,
                 all_spatial_dropout=True,
                 print_summary=True,
                 verbose=2):
    X_tr, X_te, y_tr, y_te, pos_tr, pos_te = words
    X_char_tr, X_char_te, _, _ = chars
    all_input_embeds = []
    all_inputs = []
    train_data = []
    if use_word and not add_random_embedding and embedding_matrix is None:
        raise ValueError('Cannot use word without embedding')
    if use_word:
        input = Input(shape=(max_len, ))
        if add_random_embedding:
            input_embed = Embedding(input_dim=n_words + 2,
                                    output_dim=embed_dim,
                                    input_length=max_len)(input)
            all_input_embeds.append(input_embed)
        if embedding_matrix is not None:
            input_embed = Embedding(input_dim=n_words + 2,
                                    output_dim=pretrained_embed_dim,
                                    input_length=max_len,
                                    weights=[embedding_mats[embedding_matrix]],
                                    trainable=trainable)(input)
            all_input_embeds.append(input_embed)
        all_inputs.append(input)
        train_data.append(X_tr)
    if use_pos:
        pos_input = Input(shape=(max_len, ))
        pos_embed = Embedding(input_dim=n_pos + 1,
                              output_dim=10,
                              input_length=max_len)(pos_input)
        if pos_dropout is not None:
            pos_embed = Dropout(pos_dropout)(pos_embed)
        all_input_embeds.append(pos_embed)
        all_inputs.append(pos_input)
        train_data.append(pos_tr)
    if use_char:
        # input and embeddings for characters
        char_in = Input(shape=(
            max_len,
            max_len_char,
        ))
        emb_char = TimeDistributed(
            Embedding(input_dim=n_chars + 2,
                      output_dim=20,
                      input_length=max_len_char))(char_in)
        # character LSTM to get word encodings by characters
        char_enc = TimeDistributed(
            Bidirectional(
                LSTM(units=10, return_sequences=False,
                     recurrent_dropout=0.5)))(emb_char)
        if char_dropout:
            char_enc = SpatialDropout1D(0.3)(char_enc)
        all_input_embeds.append(char_enc)
        all_inputs.append(char_in)
        train_data.append(
            np.array(X_char_tr).reshape(
                (len(X_char_tr), max_len, max_len_char)))
    if len(all_inputs) > 1:
        model = Concatenate()(all_input_embeds)
        if (use_char and all_spatial_dropout):
            model = SpatialDropout1D(0.3)(model)
    else:
        model = all_input_embeds[0]
        all_input_embeds = all_input_embeds[0]
        all_inputs = all_inputs[0]
        train_data = train_data[0]

    if input_dropout:
        model = Dropout(0.1)(model)

    if stack_double:
        front = LSTM(units=100,
                     return_sequences=True,
                     recurrent_dropout=rec_dropout)(model)
        front = LSTM(units=100,
                     return_sequences=True,
                     recurrent_dropout=rec_dropout)(front)
        back = LSTM(units=100,
                    return_sequences=True,
                    recurrent_dropout=rec_dropout,
                    go_backwards=True)(model)
        model = LSTM(units=100,
                     return_sequences=True,
                     recurrent_dropout=rec_dropout,
                     go_backwards=True)(back)
    if stack_cross:
        front = LSTM(units=100,
                     return_sequences=True,
                     recurrent_dropout=rec_dropout)(model)
        front = LSTM(units=100,
                     return_sequences=True,
                     recurrent_dropout=rec_dropout)(front)
        back = LSTM(units=100,
                    return_sequences=True,
                    recurrent_dropout=rec_dropout,
                    go_backwards=True)(model)
        back = LSTM(units=100,
                    return_sequences=True,
                    recurrent_dropout=rec_dropout,
                    go_backwards=True)(back)
        model = concatenate([back, front])
    for i in range(stack_lstm):
        model = Bidirectional(
            LSTM(units=100,
                 return_sequences=True,
                 recurrent_dropout=rec_dropout))(model)

    if output_dropout:
        model = Dropout(0.1)(model)

    if crf:
        model = TimeDistributed(Dense(50, activation="relu"))(
            model)  # a dense layer as suggested by neuralNer
        crf = CRF(n_tags + 1)
        loss = crf_loss
        metric = crf_accuracy
        monitor = 'val_crf_accuracy'
        out = crf(model)
    else:
        out = TimeDistributed(Dense(n_tags + 1, activation="softmax"))(
            model)  # softmax output layer
        loss = "categorical_crossentropy"
        metric = 'accuracy'
        monitor = 'val_acc'

    model = Model(all_inputs, out)
    model.compile(optimizer=optimizer, loss=loss, metrics=[metric])
    if early_stopping:
        es = [
            EarlyStopping(monitor=monitor,
                          mode='max',
                          verbose=1,
                          patience=patience,
                          restore_best_weights=True,
                          min_delta=min_delta)
        ]
    else:
        es = None
    if print_summary:
        print(model.summary())
    history = model.fit(train_data,
                        np.array(y_tr),
                        batch_size=32,
                        epochs=epochs,
                        validation_split=validation_split,
                        verbose=verbose,
                        callbacks=es)
    hist = pd.DataFrame(history.history)

    return model, hist
Example #16
0
    def dcl_model():
        n_classes = 6
        kernel_size = 3
        f_act = 'relu'
        pool_size = 2
        dropout_rate = 0.15

        # 三个子模型的输入数据
        main_input1 = Input(shape=(128, 3), name='main_input1')
        main_input2 = Input(shape=(128, 3), name='main_input2')
        main_input3 = Input(shape=(128, 3), name='main_input3')

        def cnn_lstm_cell(main_input):
            """
            基于DeepConvLSTM算法, 创建子模型
            :param main_input: 输入数据
            :return: 子模型
            """
            sub_model = Conv1D(512,
                               kernel_size,
                               input_shape=(128, 3),
                               activation=f_act,
                               padding='same')(main_input)
            sub_model = BatchNormalization()(sub_model)
            sub_model = MaxPooling1D(pool_size=pool_size)(sub_model)
            sub_model = Dropout(dropout_rate)(sub_model)
            sub_model = Conv1D(64,
                               kernel_size,
                               activation=f_act,
                               padding='same')(sub_model)
            sub_model = BatchNormalization()(sub_model)
            sub_model = MaxPooling1D(pool_size=pool_size)(sub_model)
            sub_model = Dropout(dropout_rate)(sub_model)
            sub_model = Conv1D(32,
                               kernel_size,
                               activation=f_act,
                               padding='same')(sub_model)
            sub_model = BatchNormalization()(sub_model)
            sub_model = MaxPooling1D(pool_size=pool_size)(sub_model)
            sub_model = LSTM(128, return_sequences=True)(sub_model)
            sub_model = LSTM(128, return_sequences=True)(sub_model)
            sub_model = LSTM(128)(sub_model)
            main_output = Dropout(dropout_rate)(sub_model)
            return main_output

        first_model = cnn_lstm_cell(main_input1)
        second_model = cnn_lstm_cell(main_input2)
        third_model = cnn_lstm_cell(main_input3)

        model = Concatenate()([first_model, second_model, third_model])  # 合并模型
        model = Dropout(0.4)(model)
        model = Dense(n_classes)(model)
        model = BatchNormalization()(model)
        output = Activation('softmax', name="softmax")(model)

        model = Model([main_input1, main_input2, main_input3], output)
        model.compile(loss='categorical_crossentropy',
                      optimizer='rmsprop',
                      metrics=['accuracy'])

        return model