Ejemplo n.º 1
0
def set_cnn_model_attention(input_dim=4, input_length=2701):
    attention_reg_x = 0.25
    attention_reg_xr = 1
    attentionhidden_x = 16
    attentionhidden_xr = 8
    nbfilter = 16
    input = Input(shape=(input_length, input_dim))
    x = conv.Convolution1D(nbfilter, 10, border_mode="valid")(input)
    x = Dropout(0.5)(x)
    x = Activation('relu')(x)
    x = conv.MaxPooling1D(pool_length=3)(x)
    x_reshape = core.Reshape((x._keras_shape[2], x._keras_shape[1]))(x)

    x = Dropout(0.5)(x)
    x_reshape = Dropout(0.5)(x_reshape)

    decoder_x = Attention(hidden=attentionhidden_x,
                          activation='linear')  # success
    decoded_x = decoder_x(x)
    output_x = myFlatten(x._keras_shape[2])(decoded_x)

    decoder_xr = Attention(hidden=attentionhidden_xr, activation='linear')
    decoded_xr = decoder_xr(x_reshape)
    output_xr = myFlatten(x_reshape._keras_shape[2])(decoded_xr)

    output = merge([output_x, output_xr, Flatten()(x)], mode='concat')
    #output = BatchNormalization()(output)
    output = Dropout(0.5)(output)
    print output.shape
    output = Dense(nbfilter * 10, activation="relu")(output)
    output = Dropout(0.5)(output)
    out = Dense(2, activation='softmax')(output)
    #output = BatchNormalization()(output)
    model = Model(input, out)
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    return model
Ejemplo n.º 2
0
def MultiCNN(trainX,
             trainY,
             valX=None,
             valY=None,
             batch_size=1200,
             nb_epoch=500,
             earlystop=None,
             transferlayer=1,
             weights=None,
             forkinas=False,
             compiletimes=0,
             compilemodels=None,
             predict=False):
    input_row = trainX.shape[2]
    input_col = trainX.shape[3]

    trainX_t = trainX
    valX_t = valX
    if (earlystop is not None):
        early_stopping = EarlyStopping(monitor='val_loss', patience=earlystop)
        nb_epoch = 10000
        #set to a very big value since earlystop used

    trainX_t.shape = (trainX_t.shape[0], input_row, input_col)
    if (valX is not None):
        valX_t.shape = (valX_t.shape[0], input_row, input_col)

    if compiletimes == 0:
        input = Input(shape=(input_row, input_col))
        filtersize1 = 1
        filtersize2 = 9
        filtersize3 = 10
        filter1 = 200
        filter2 = 150
        filter3 = 200
        dropout1 = 0.75
        dropout2 = 0.75
        dropout4 = 0.75
        dropout5 = 0.75
        dropout6 = 0
        L1CNN = 0
        nb_classes = 2
        batch_size = 1200
        actfun = "relu"
        optimization = 'adam'
        attentionhidden_x = 10
        attentionhidden_xr = 8
        attention_reg_x = 0.151948
        attention_reg_xr = 2
        dense_size1 = 149
        dense_size2 = 8
        dropout_dense1 = 0.298224
        dropout_dense2 = 0

        input = Input(shape=(input_row, input_col))
        x = conv.Convolution1D(filter1,
                               filtersize1,
                               init='he_normal',
                               W_regularizer=l1(L1CNN),
                               border_mode="same")(input)
        x = Dropout(dropout1)(x)
        x = Activation(actfun)(x)
        x = conv.Convolution1D(filter2,
                               filtersize2,
                               init='he_normal',
                               W_regularizer=l1(L1CNN),
                               border_mode="same")(x)
        x = Dropout(dropout2)(x)
        x = Activation(actfun)(x)
        x = conv.Convolution1D(filter3,
                               filtersize3,
                               init='he_normal',
                               W_regularizer=l1(L1CNN),
                               border_mode="same")(x)
        x = Activation(actfun)(x)
        x_reshape = core.Reshape((x._keras_shape[2], x._keras_shape[1]))(x)

        x = Dropout(dropout4)(x)
        x_reshape = Dropout(dropout5)(x_reshape)

        decoder_x = Attention(hidden=attentionhidden_x,
                              activation='linear',
                              init='he_normal',
                              W_regularizer=l1(attention_reg_x))  # success
        decoded_x = decoder_x(x)
        output_x = myFlatten(x._keras_shape[2])(decoded_x)

        decoder_xr = Attention(hidden=attentionhidden_xr,
                               activation='linear',
                               init='he_normal',
                               W_regularizer=l1(attention_reg_xr))
        decoded_xr = decoder_xr(x_reshape)
        output_xr = myFlatten(x_reshape._keras_shape[2])(decoded_xr)

        output = merge([output_x, output_xr], mode='concat')
        output = Dropout(dropout6)(output)
        output = Dense(dense_size1, init='he_normal',
                       activation='relu')(output)
        output = Dropout(dropout_dense1)(output)
        output = Dense(dense_size2, activation="relu",
                       init='he_normal')(output)
        output = Dropout(dropout_dense2)(output)
        out = Dense(nb_classes, init='he_normal', activation='softmax')(output)
        cnn = Model(input, out)
        cnn.compile(loss='binary_crossentropy',
                    optimizer=optimization,
                    metrics=['accuracy'])

    else:
        cnn = compilemodels

    if (predict is False):
        if (weights is not None and compiletimes == 0):  #for the first time
            print "load weights:" + weights
            if not forkinas:
                cnn.load_weights(weights)
            else:
                cnn2 = copy.deepcopy(cnn)
                cnn2.load_weights(weights)
                for l in range((len(cnn2.layers) -
                                transferlayer)):  #the last cnn is not included
                    cnn.layers[l].set_weights(cnn2.layers[l].get_weights())
                    #cnn.layers[l].trainable= False  # for frozen layer

        if (valX is not None):
            if (earlystop is None):
                fitHistory = cnn.fit(trainX_t,
                                     trainY,
                                     batch_size=batch_size,
                                     nb_epoch=nb_epoch,
                                     validation_data=(valX_t, valY))
            else:
                fitHistory = cnn.fit(trainX_t,
                                     trainY,
                                     batch_size=batch_size,
                                     nb_epoch=nb_epoch,
                                     validation_data=(valX_t, valY),
                                     callbacks=[early_stopping])
        else:
            fitHistory = cnn.fit(trainX_t,
                                 trainY,
                                 batch_size=batch_size,
                                 nb_epoch=nb_epoch)

    return cnn
Ejemplo n.º 3
0
def DeepCleave_CNN(output_model_name,
                   trainX,
                   trainY,
                   valX=None,
                   valY=None,
                   batch_size=1024,
                   epochs=500,
                   n_earlystop=None,
                   n_transfer_layer=1,
                   background_weights=None,
                   for_transfer=False,
                   compiletimes=0,
                   compilemodels=None,
                   predict=False):
    input_row = trainX.shape[1]
    input_col = trainX.shape[2]

    trainX_t = trainX
    valX_t = valX
    checkpoint = ModelCheckpoint(filepath=output_model_name,
                                 monitor='val_acc',
                                 verbose=1,
                                 mode='max',
                                 save_best_only='True')

    if (n_earlystop is not None):
        early_stopping = EarlyStopping(monitor='val_acc', patience=n_earlystop)
        epochs = 10000
        #set to a very big value since earlystop used

        callback_lists = [early_stopping, checkpoint]
    else:
        callback_lists = [checkpoint]

    trainX_t.shape = (trainX_t.shape[0], input_row, input_col)
    if (valX is not None):
        valX_t.shape = (valX_t.shape[0], input_row, input_col)

    if compiletimes == 0:
        filtersize1 = 1
        filtersize2 = 9
        filtersize3 = 10
        filter1 = 200
        filter2 = 150
        filter3 = 200
        dropout1 = 0.75
        dropout2 = 0.75
        dropout4 = 0.75
        dropout5 = 0.75
        dropout6 = 0.25
        L1CNN = 0
        l2value = 0.001
        nb_classes = 2
        actfun = "relu"
        optimization = 'adam'
        attentionhidden_x = 10
        attentionhidden_xr = 8
        attention_reg_x = 0.151948
        attention_reg_xr = 2
        dense_size1 = 149
        dense_size2 = 8
        dropout_dense1 = 0.298224
        dropout_dense2 = 0

        input2 = Input(shape=(input_row, input_col))

        x = conv.Convolution1D(filter1,
                               filtersize1,
                               init='he_normal',
                               W_regularizer=l2(l2value),
                               border_mode="same")(input2)
        x = Dropout(dropout1)(x)
        x1 = Activation(actfun)(x)

        y1 = conv.Convolution1D(filter2,
                                filtersize2,
                                init='he_normal',
                                W_regularizer=l2(l2value),
                                border_mode="same")(x1)
        y1 = Dropout(dropout2)(y1)
        y1 = Activation(actfun)(y1)

        y2 = conv.Convolution1D(filter2,
                                6,
                                init='he_normal',
                                W_regularizer=l2(l2value),
                                border_mode="same")(x1)
        y2 = Dropout(dropout2)(y2)
        y2 = Activation(actfun)(y2)

        y3 = conv.Convolution1D(filter2,
                                3,
                                init='he_normal',
                                W_regularizer=l2(l2value),
                                border_mode="same")(x1)
        y3 = Dropout(dropout2)(y3)
        y3 = Activation(actfun)(y3)

        mergeY = merge([y1, y2, y3], mode='concat', concat_axis=-1)
        mergeY = Dropout(0.75)(mergeY)

        z1 = conv.Convolution1D(filter3,
                                filtersize3,
                                init='he_normal',
                                W_regularizer=l2(l2value),
                                border_mode="same")(mergeY)
        z1 = Activation(actfun)(z1)

        z2 = conv.Convolution1D(filter3,
                                5,
                                init='he_normal',
                                W_regularizer=l2(l2value),
                                border_mode="same")(mergeY)
        z2 = Activation(actfun)(z2)

        z3 = conv.Convolution1D(filter3,
                                15,
                                init='he_normal',
                                W_regularizer=l2(l2value),
                                border_mode="same")(mergeY)
        z3 = Activation(actfun)(z3)

        x_reshape = core.Reshape((z1._keras_shape[2], z1._keras_shape[1]))(z1)

        x = Dropout(dropout4)(z1)
        x_reshape = Dropout(dropout5)(x_reshape)

        decoder_x = Attention(hidden=attentionhidden_x,
                              activation='linear',
                              init='he_normal',
                              W_regularizer=l1(attention_reg_x))  # success
        decoded_x = decoder_x(x)
        output_x = myFlatten(x._keras_shape[2])(decoded_x)

        decoder_xr = Attention(hidden=attentionhidden_xr,
                               activation='linear',
                               init='he_normal',
                               W_regularizer=l1(attention_reg_xr))
        decoded_xr = decoder_xr(x_reshape)

        output_xr = myFlatten(x_reshape._keras_shape[2])(decoded_xr)

        x_reshape = core.Reshape((z2._keras_shape[2], z2._keras_shape[1]))(z2)

        x = Dropout(dropout4)(z2)
        x_reshape = Dropout(dropout5)(x_reshape)

        decoder_x = Attention(hidden=attentionhidden_x,
                              activation='linear',
                              init='he_normal',
                              W_regularizer=l1(attention_reg_x))  # success
        decoded_x = decoder_x(x)
        output_x2 = myFlatten(x._keras_shape[2])(decoded_x)

        decoder_xr = Attention(hidden=attentionhidden_xr,
                               activation='linear',
                               init='he_normal',
                               W_regularizer=l1(attention_reg_xr))
        decoded_xr = decoder_xr(x_reshape)

        output_xr2 = myFlatten(x_reshape._keras_shape[2])(decoded_xr)

        x_reshape = core.Reshape((z3._keras_shape[2], z3._keras_shape[1]))(z3)

        x = Dropout(dropout4)(z3)
        x_reshape = Dropout(dropout5)(x_reshape)

        decoder_x = Attention(hidden=attentionhidden_x,
                              activation='linear',
                              init='he_normal',
                              W_regularizer=l1(attention_reg_x))  # success
        decoded_x = decoder_x(x)
        output_x3 = myFlatten(x._keras_shape[2])(decoded_x)

        decoder_xr = Attention(hidden=attentionhidden_xr,
                               activation='linear',
                               init='he_normal',
                               W_regularizer=l1(attention_reg_xr))
        decoded_xr = decoder_xr(x_reshape)

        output_xr3 = myFlatten(x_reshape._keras_shape[2])(decoded_xr)

        output = merge([
            output_x, output_xr, output_x2, output_xr2, output_x3, output_xr3
        ],
                       mode='concat')

        output = Dropout(dropout6)(output)
        output = Dense(dense_size1, init='he_normal',
                       activation='relu')(output)
        output = Dropout(dropout_dense1)(output)
        output = Dense(dense_size2, activation="relu",
                       init='he_normal')(output)
        output = Dropout(dropout_dense2)(output)
        out = Dense(nb_classes, init='he_normal', activation='softmax')(output)
        cnn = Model(input2, out)
        cnn.compile(loss='binary_crossentropy',
                    optimizer=optimization,
                    metrics=['accuracy'])

    else:
        cnn = compilemodels

    if (predict is False):
        if (background_weights is not None
                and compiletimes == 0):  #for the first time
            if not for_transfer:
                cnn.load_weights(background_weights)
            else:
                cnn2 = Model(input2, out)
                cnn2.compile(loss='binary_crossentropy',
                             optimizer=optimization,
                             metrics=['accuracy'])

                cnn2.load_weights(background_weights)
                for l in range(
                    (len(cnn2.layers) -
                     n_transfer_layer)):  #the last cnn is not included
                    cnn.layers[l].set_weights(cnn2.layers[l].get_weights())
                    cnn.layers[l].trainable = False  # for frozen layer
                cnn.compile(loss='binary_crossentropy',
                            optimizer=optimization,
                            metrics=['accuracy'])

        if (valX is not None):
            if (n_earlystop is None):
                fitHistory = cnn.fit(trainX_t,
                                     trainY,
                                     batch_size=batch_size,
                                     epochs=epochs,
                                     validation_data=(valX_t, valY))

            else:
                fitHistory = cnn.fit(trainX_t,
                                     trainY,
                                     batch_size=batch_size,
                                     epochs=epochs,
                                     validation_data=(valX_t, valY),
                                     callbacks=callback_lists)
        else:
            fitHistory = cnn.fit(trainX_t,
                                 trainY,
                                 batch_size=batch_size,
                                 epochs=epochs)

    return cnn
Ejemplo n.º 4
0
def train_cnn(learning_rate,weight_decay,dropoutMerge1,dropoutMerge2,dropoutMerge3,dropoutMerge4,
                dropout_dense1,dense1,dense3,conv1_filter,conv2_filter,conv3_filter,earlystop,batch_size,
                input_row_One_Hot,input_col_One_Hot,input_row_ANF_NCP,input_col_ANF_NCP,
                input_row_CKSNAP_NCP,input_col_CKSNAP_NCP,input_row_PSTNPss_NCP,input_col_PSTNPss_NCP,
                beta_1,beta_2,epsilon,attentionhidden_x,attentionhidden_xr,attention_reg_x,attention_reg_xr,
                best_model, weightsModel, predict=False,train=False,transfer=False):

    ############################################# Model ###################################################

    # choose optimazation
    #optimization = Adam(lr=learning_rate, beta_1=beta_1, beta_2=beta_2, epsilon=epsilon)
    optimization = Adamax(lr=learning_rate, beta_1=beta_1, beta_2=beta_2, epsilon=epsilon)

    #####################################One_Hot#####################################

    main_input = Input(shape=(input_row_One_Hot,input_col_One_Hot))

    x1 = conv.Convolution1D(conv1_filter, 1, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(main_input)
    
    x3 = conv.Convolution1D(conv1_filter, 3, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(main_input)

    x5 = conv.Convolution1D(conv1_filter, 5, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(main_input)

    mergeX = merge([x1, x3, x5], mode='concat', concat_axis=-1)
    mergeX = Dropout(dropoutMerge1)(mergeX)
    mergeX = MaxPooling1D(3,1)(mergeX)

    mergeX = conv.Convolution1D(conv2_filter, 1, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeX)

    y3 = conv.Convolution1D(conv2_filter, 3, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeX)

    y5 = conv.Convolution1D(conv2_filter, 5, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeX)

    mergeY = merge([mergeX, y3, y5], mode='concat', concat_axis=-1)
    mergeY = Dropout(dropoutMerge2)(mergeY)
    mergeY = MaxPooling1D(5,1)(mergeY)

    mergeY = conv.Convolution1D(conv3_filter, 1, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeY)

    z3 = conv.Convolution1D(conv3_filter, 3, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeY)

    z5 = conv.Convolution1D(conv3_filter, 5, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeY)

    mergeZ = merge([mergeY,z3,z5], mode='concat', concat_axis=-1)
    mergeZ = Dropout(dropoutMerge3)(mergeZ)
    mergeZ = MaxPooling1D(3,1)(mergeZ)

    mergeZ_reshape=core.Reshape((mergeZ._keras_shape[2],mergeZ._keras_shape[1]))(mergeZ)

    decoder_mergeZ = Attention(hidden=attentionhidden_x,activation='linear',init='he_normal',W_regularizer=l1(attention_reg_x)) # success  
    decoded_mergeZ=decoder_mergeZ(mergeZ)
    output_mergeZ = myFlatten(mergeZ._keras_shape[2])(decoded_mergeZ)

    decoder_mergeZr = Attention(hidden=attentionhidden_xr,activation='linear',init='he_normal',W_regularizer=l1(attention_reg_xr))
    decoded_mergeZr=decoder_mergeZr(mergeZ_reshape)
    output_mergeZr = myFlatten(mergeZ_reshape._keras_shape[2])(decoded_mergeZr)


    q1 = Bidirectional(LSTM(conv3_filter, consume_less='gpu',init='he_normal',W_regularizer=l2(weight_decay) ))(mergeZ)
    #q1 = Dropout(dropout4)(q1)

    q1r = Bidirectional(LSTM(conv3_filter, consume_less='gpu',init='he_normal',W_regularizer=l2(weight_decay) ))(mergeZ_reshape)
    #q1 = Dropout(dropout4)(q1)



    #####################################ANF_NCP#####################################
        
    input_A = Input(shape=(input_row_ANF_NCP,input_col_ANF_NCP))

    x1_A = conv.Convolution1D(conv1_filter, 1, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(input_A)

    x3_A = conv.Convolution1D(conv1_filter, 3, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(input_A)

    x5_A = conv.Convolution1D(conv1_filter, 5, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(input_A)

    mergeX_A = merge([x1_A, x3_A, x5_A], mode='concat', concat_axis=-1)
    mergeX_A = Dropout(dropoutMerge1)(mergeX_A)
    mergeX_A = MaxPooling1D(3,1)(mergeX_A)

    mergeX_A = conv.Convolution1D(conv2_filter, 1, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeX_A)

    y3_A = conv.Convolution1D(conv2_filter, 3, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeX_A)

    y5_A = conv.Convolution1D(conv2_filter, 5, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeX_A)

    mergeY_A = merge([mergeX_A, y3_A, y5_A], mode='concat', concat_axis=-1)
    mergeY_A = Dropout(dropoutMerge2)(mergeY_A)
    mergeY_A = MaxPooling1D(5,1)(mergeY_A)

    mergeY_A = conv.Convolution1D(conv3_filter, 1, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeY_A)

    z3_A = conv.Convolution1D(conv3_filter, 3, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeY_A)

    z5_A = conv.Convolution1D(conv3_filter, 5, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeY_A)

    mergeZ_A = merge([mergeY_A,z3_A,z5_A], mode='concat', concat_axis=-1)
    mergeZ_A = Dropout(dropoutMerge3)(mergeZ_A)
    mergeZ_A = MaxPooling1D(3,1)(mergeZ_A)

    mergeZ_A_reshape=core.Reshape((mergeZ_A._keras_shape[2],mergeZ_A._keras_shape[1]))(mergeZ_A)

    decoder_mergeZ_A = Attention(hidden=attentionhidden_x,activation='linear',init='he_normal',W_regularizer=l1(attention_reg_x)) # success  
    decoded_mergeZ_A=decoder_mergeZ_A(mergeZ_A)
    output_mergeZ_A = myFlatten(mergeZ_A._keras_shape[2])(decoded_mergeZ_A)

    decoder_mergeZ_Ar = Attention(hidden=attentionhidden_xr,activation='linear',init='he_normal',W_regularizer=l1(attention_reg_xr))
    decoded_mergeZ_Ar=decoder_mergeZ_Ar(mergeZ_A_reshape)
    output_mergeZ_Ar = myFlatten(mergeZ_A_reshape._keras_shape[2])(decoded_mergeZ_Ar)


    q1_A = Bidirectional(LSTM(conv3_filter, consume_less='gpu',init='he_normal',W_regularizer=l2(weight_decay) ))(mergeZ_A)
    #q1_A = Dropout(dropout4)(q1_A)

    q1r_A = Bidirectional(LSTM(conv3_filter, consume_less='gpu',init='he_normal',W_regularizer=l2(weight_decay) ))(mergeZ_A_reshape)
    #q1_A = Dropout(dropout4)(q1_A)



    #####################################CKSNAP_NCP#####################################
        
    input_C = Input(shape=(input_row_CKSNAP_NCP,input_col_CKSNAP_NCP))

    x1_C = conv.Convolution1D(conv1_filter, 1, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(input_C)

    x3_C = conv.Convolution1D(conv1_filter, 3, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(input_C)

    x5_C = conv.Convolution1D(conv1_filter, 5, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(input_C)

    mergeX_C = merge([x1_C, x3_C, x5_C], mode='concat', concat_axis=-1)
    mergeX_C = Dropout(dropoutMerge1)(mergeX_C)
    mergeX_C = MaxPooling1D(3,1)(mergeX_C)

    mergeX_C = conv.Convolution1D(conv2_filter, 1, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeX_C)

    y3_C = conv.Convolution1D(conv2_filter, 3, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeX_C)

    y5_C = conv.Convolution1D(conv2_filter, 5, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeX_C)

    mergeY_C = merge([mergeX_C, y3_C, y5_C], mode='concat', concat_axis=-1)
    mergeY_C = Dropout(dropoutMerge2)(mergeY_C)
    mergeY_C = MaxPooling1D(5,1)(mergeY_C)

    mergeY_C = conv.Convolution1D(conv3_filter, 1, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeY_C)

    z3_C = conv.Convolution1D(conv3_filter, 3, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeY_C)

    z5_C = conv.Convolution1D(conv3_filter, 5, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeY_C)

    mergeZ_C = merge([mergeY_C,z3_C,z5_C], mode='concat', concat_axis=-1)
    mergeZ_C = Dropout(dropoutMerge3)(mergeZ_C)
    mergeZ_C = MaxPooling1D(3,1)(mergeZ_C)

    mergeZ_C_reshape=core.Reshape((mergeZ_C._keras_shape[2],mergeZ_C._keras_shape[1]))(mergeZ_C)

    decoder_mergeZ_C = Attention(hidden=attentionhidden_x,activation='linear',init='he_normal',W_regularizer=l1(attention_reg_x)) # success  
    decoded_mergeZ_C=decoder_mergeZ_C(mergeZ_C)
    output_mergeZ_C = myFlatten(mergeZ_C._keras_shape[2])(decoded_mergeZ_C)

    decoder_mergeZ_Cr = Attention(hidden=attentionhidden_xr,activation='linear',init='he_normal',W_regularizer=l1(attention_reg_xr))
    decoded_mergeZ_Cr=decoder_mergeZ_Cr(mergeZ_C_reshape)
    output_mergeZ_Cr = myFlatten(mergeZ_C_reshape._keras_shape[2])(decoded_mergeZ_Cr)


    q1_C = Bidirectional(LSTM(conv3_filter, consume_less='gpu',init='he_normal',W_regularizer=l2(weight_decay) ))(mergeZ_C)
    #q1_C = Dropout(dropout4)(q1_C)

    q1r_C = Bidirectional(LSTM(conv3_filter, consume_less='gpu',init='he_normal',W_regularizer=l2(weight_decay) ))(mergeZ_C_reshape)
    #q1_C = Dropout(dropout4)(q1_C)


    #####################################PSTNPss_NCP#####################################
        
    input_P = Input(shape=(input_row_PSTNPss_NCP,input_col_PSTNPss_NCP))

    x1_P = conv.Convolution1D(conv1_filter, 1, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(input_P)

    x3_P = conv.Convolution1D(conv1_filter, 3, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(input_P)

    x5_P = conv.Convolution1D(conv1_filter, 5, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(input_P)

    mergeX_P = merge([x1_P, x3_P, x5_P], mode='concat', concat_axis=-1)
    mergeX_P = Dropout(dropoutMerge1)(mergeX_P)
    mergeX_P = MaxPooling1D(3,1)(mergeX_P)

    mergeX_P = conv.Convolution1D(conv2_filter, 1, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeX_P)

    y3_P = conv.Convolution1D(conv2_filter, 3, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeX_P)

    y5_P = conv.Convolution1D(conv2_filter, 5, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeX_P)

    mergeY_P = merge([mergeX_P, y3_P, y5_P], mode='concat', concat_axis=-1)
    mergeY_P = Dropout(dropoutMerge2)(mergeY_P)
    mergeY_P = MaxPooling1D(5,1)(mergeY_P)

    mergeY_P = conv.Convolution1D(conv3_filter, 1, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeY_P)

    z3_P = conv.Convolution1D(conv3_filter, 3, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeY_P)

    z5_P = conv.Convolution1D(conv3_filter, 5, activation='relu',border_mode="same",W_regularizer=l2(weight_decay) ,init='he_normal')(mergeY_P)
    
    mergeZ_P = merge([mergeY_P,z3_P,z5_P], mode='concat', concat_axis=-1)
    mergeZ_P = Dropout(dropoutMerge3)(mergeZ_P)
    mergeZ_P = MaxPooling1D(3,1)(mergeZ_P)

    mergeZ_P_reshape=core.Reshape((mergeZ_P._keras_shape[2],mergeZ_P._keras_shape[1]))(mergeZ_P)

    decoder_mergeZ_P = Attention(hidden=attentionhidden_x,activation='linear',init='he_normal',W_regularizer=l1(attention_reg_x)) # success  
    decoded_mergeZ_P=decoder_mergeZ_P(mergeZ_P)
    output_mergeZ_P = myFlatten(mergeZ_P._keras_shape[2])(decoded_mergeZ_P)

    decoder_mergeZ_Pr = Attention(hidden=attentionhidden_xr,activation='linear',init='he_normal',W_regularizer=l1(attention_reg_xr))
    decoded_mergeZ_Pr=decoder_mergeZ_Pr(mergeZ_P_reshape)
    output_mergeZ_Pr = myFlatten(mergeZ_P_reshape._keras_shape[2])(decoded_mergeZ_Pr)


    q1_P = Bidirectional(LSTM(conv3_filter, consume_less='gpu',init='he_normal',W_regularizer=l2(weight_decay) ))(mergeZ_P)
    #q1_P = Dropout(dropout4)(q1_P)

    q1r_P = Bidirectional(LSTM(conv3_filter, consume_less='gpu',init='he_normal',W_regularizer=l2(weight_decay) ))(mergeZ_P_reshape)
    #q1_P = Dropout(dropout4)(q1_P)


    #####################################Merge#####################################
    mergeQ = merge([q1, q1r, output_mergeZ, output_mergeZr,q1_A, q1r_A, output_mergeZ_A, output_mergeZ_Ar,q1_C, q1r_C, output_mergeZ_C, output_mergeZ_Cr,q1_P, q1r_P, output_mergeZ_P, output_mergeZ_Pr], mode='concat', concat_axis=-1)
    mergeQ = Dropout(dropoutMerge4)(mergeQ)

    aa=Dense(dense1,init='he_normal',activation='relu')(mergeQ)
    aa=Dropout(dropout_dense1)(aa)
    
    #w = Dense(dense2,init='he_normal', activation='relu')(aa)
    #w = Dropout(dropout_dense2)(w)

    w2 = Dense(dense3,init='he_normal', activation='relu')(aa)
    #w = Dropout(dropout5)(w)

    out=Dense(2,init='he_normal',activation='softmax')(w2)


    cnn=Model([main_input,input_A,input_C,input_P],out)
    cnn.compile(loss='binary_crossentropy',optimizer=optimization,metrics=['accuracy'])

    print cnn.summary()

    return cnn