def build_rhodes(): print "building rhodes" auth_model_3gram = Sequential() auth_model_3gram.add(convolutional.Convolution1D(100, 3, border_mode='same', input_shape=(longest_sentence_corpus, 300))) auth_model_3gram.add(convolutional.MaxPooling1D(2, stride=1, border_mode='same')) auth_model_4gram = Sequential() auth_model_4gram.add(convolutional.Convolution1D(100, 4, border_mode='same', input_shape=(longest_sentence_corpus, 300))) auth_model_4gram.add(convolutional.MaxPooling1D(2, stride=1, border_mode='same')) auth_model_5gram = Sequential() auth_model_5gram.add(convolutional.Convolution1D(100, 5, border_mode='same', input_shape=(longest_sentence_corpus, 300))) auth_model_5gram.add(convolutional.MaxPooling1D(2, stride=1, border_mode='same')) global merged_model merged_model = Sequential() merged_model.add(Merge([auth_model_3gram, auth_model_4gram, auth_model_5gram], mode='concat', concat_axis=2)) merged_model.add(Flatten()) merged_model.add(Dense(200)) merged_model.add(Dense(200, activation='relu')) merged_model.add(Dropout(0.5)) merged_model.add(Dense(200)) merged_model.add(Dense(2, activation='softmax')) merged_model.summary() ada = Adagrad(lr=0.0001, epsilon=1e-06) merged_model.compile(loss='categorical_crossentropy', optimizer=ada, metrics=['accuracy'])
def create_emb_layer(self): iw = Input(shape=(self.max_ngram_num, ), dtype='int32', name="inputword") emb_in = embeddings.Embedding(output_dim=self.vector_size, input_dim=self.ngram_size, init="uniform", mask_zero=True, name="input_layer") vv_iw = emb_in(iw) zm = ZeroMaskedEntries() zm.build((None, self.max_ngram_num, self.vector_size)) zero_masked_emd = zm(vv_iw) conv_l1 = convolutional.Convolution1D(self.vector_size, self.max_ngram_num, border_mode='valid') conv_l2 = convolutional.Convolution1D(self.vector_size, self.max_ngram_num, border_mode='valid') conv1 = conv_l1(zero_masked_emd) conv2 = conv_l2(zero_masked_emd) sigm_conv = Activation("sigmoid")(conv2) mult_l = Merge(mode='mul') mult = mult_l([conv1, sigm_conv]) return ([iw], emb_in, mult)
def test_convolution_1d(self): nb_samples = 9 nb_steps = 7 input_dim = 10 filter_length = 6 nb_filter = 5 weights_in = [np.ones((nb_filter, input_dim, filter_length, 1)), np.ones(nb_filter)] self.assertRaises(Exception, convolutional.Convolution1D, input_dim, nb_filter, filter_length, border_mode='foo') input = np.ones((nb_samples, nb_steps, input_dim)) for weight in [None, weights_in]: for border_mode in ['valid', 'full', 'same']: for subsample_length in [1, 3]: for W_regularizer in [None, 'l2']: for b_regularizer in [None, 'l2']: for act_regularizer in [None, 'l2']: layer = convolutional.Convolution1D( input_dim, nb_filter, filter_length, weights=weight, border_mode=border_mode, W_regularizer=W_regularizer, b_regularizer=b_regularizer, activity_regularizer=act_regularizer, subsample_length=subsample_length) layer.input = theano.shared(value=input) for train in [True, False]: out = layer.get_output(train).eval() assert input.shape[0] == out.shape[0] if border_mode == 'same' and subsample_length == 1: assert input.shape[1] == out.shape[1] config = layer.get_config()
def create_simple_conv_model(my_model): model = Sequential() conv = convolutional.Convolution1D(VECTOR_SIZE, MAX_NGRAM_NUM, border_mode='valid', input_shape=(MAX_NGRAM_NUM, VECTOR_SIZE)) model.add(conv) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) conv.set_weights( my_model.keras_model.get_layer( my_model.vector_layer_name).get_weights()) return model
def create_emb_layer(self): iw = Input(shape=(self.max_ngram_num, ), dtype='int32', name="inputword") emb_in = embeddings.Embedding(output_dim=self.vector_size, input_dim=self.ngram_size, init="uniform", name="input_layer") vv_iw = emb_in(iw) conv_l = convolutional.Convolution1D(self.vector_size, 30, border_mode='same') conv = conv_l(vv_iw) pool = pooling.AveragePooling1D(self.max_ngram_num, border_mode="same") pool_res = pool(conv) return ([iw], emb_in, pool_res)
def create_emb_layer(self): iw = Input(shape=(self.max_ngram_num, ), dtype='int32', name="inputword") emb_in = embeddings.Embedding(output_dim=self.vector_size, input_dim=self.ngram_size, init="uniform", mask_zero=True, name="input_layer") vv_iw = emb_in(iw) zm = ZeroMaskedEntries() zm.build((None, self.max_ngram_num, self.vector_size)) zero_masked_emd = zm(vv_iw) conv_l = convolutional.Convolution1D(self.vector_size, self.max_ngram_num, border_mode='valid') conv = conv_l(zero_masked_emd) return ([iw], emb_in, conv)
def test_convolution_1d(): nb_samples = 9 nb_steps = 7 input_dim = 10 filter_length = 6 nb_filter = 5 weights_in = [ np.ones((nb_filter, input_dim, filter_length, 1)), np.ones(nb_filter) ] input = np.ones((nb_samples, nb_steps, input_dim)) for weight in [None, weights_in]: for border_mode in ['valid', 'same']: for subsample_length in [1]: if border_mode == 'same' and subsample_length != 1: continue for W_regularizer in [None, 'l2']: for b_regularizer in [None, 'l2']: for act_regularizer in [None, 'l2']: layer = convolutional.Convolution1D( nb_filter, filter_length, weights=weight, border_mode=border_mode, W_regularizer=W_regularizer, b_regularizer=b_regularizer, activity_regularizer=act_regularizer, subsample_length=subsample_length, input_shape=(None, input_dim)) layer.input = K.variable(input) for train in [True, False]: out = K.eval(layer.get_output(train)) assert input.shape[0] == out.shape[0] if border_mode == 'same' and subsample_length == 1: assert input.shape[1] == out.shape[1] layer.get_config()
def create_emb_layer(self): iw = Input(shape=(self.max_ngram_num, ), dtype='int32', name="inputword") emb_in = embeddings.Embedding(output_dim=self.vector_size, input_dim=self.ngram_size, init="uniform", mask_zero=True, name="input_layer") vv_iw = emb_in(iw) zm = ZeroMaskedEntries() zm.build((None, self.max_ngram_num, self.vector_size)) zero_masked_emd = zm(vv_iw) conv_l = convolutional.Convolution1D(self.vector_size, 30, border_mode='same') conv = conv_l(zero_masked_emd) sigm_conv = Activation("sigmoid")(conv) mult_l = Merge(mode='mul') mult = mult_l([conv, sigm_conv]) pool = pooling.AveragePooling1D(self.max_ngram_num, border_mode="same") pool_res = pool(mult) return ([iw], emb_in, pool_res)
def seq_layers(params): layers = [] if params.drop_in: layer = kcore.Dropout(params.drop_in) layers.append(('xd', layer)) nb_layer = len(params.nb_filter) w_reg = kr.WeightRegularizer(l1=params.l1, l2=params.l2) for l in range(nb_layer): layer = kconv.Convolution1D(nb_filter=params.nb_filter[l], filter_length=params.filter_len[l], activation=params.activation, init='glorot_uniform', W_regularizer=w_reg, border_mode='same') layers.append(('c%d' % (l + 1), layer)) layer = kconv.MaxPooling1D(pool_length=params.pool_len[l]) layers.append(('p%d' % (l + 1), layer)) layer = kcore.Flatten() layers.append(('f1', layer)) if params.drop_out: layer = kcore.Dropout(params.drop_out) layers.append(('f1d', layer)) if params.nb_hidden: layer = kcore.Dense(output_dim=params.nb_hidden, activation='linear', init='glorot_uniform') layers.append(('h1', layer)) if params.batch_norm: layer = knorm.BatchNormalization() layers.append(('h1b', layer)) layer = kcore.Activation(params.activation) layers.append(('h1a', layer)) if params.drop_out: layer = kcore.Dropout(params.drop_out) layers.append(('h1d', layer)) return layers
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
def OnehotNetwork(trainX, trainY, valX, valY, Oneofkey_input, folds, train_time=None): if (train_time == 0): # x = conv.Convolution1D(201, 2, init='glorot_normal', W_regularizer=l1(0), border_mode="same")(Oneofkey_input) # x = Dropout(0.4)(x) # x = Activation('softsign')(x) x = conv.Convolution1D(101, 3, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(Oneofkey_input) x = Dropout(0.4)(x) x = Activation('relu')(x) x = conv.Convolution1D(101, 5, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(x) x = Dropout(0.4)(x) x = Activation('relu')(x) x = conv.Convolution1D(101, 7, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(x) x = Dropout(0.4)(x) x = Activation('relu')(x) x = core.Flatten()(x) x = BatchNormalization()(x) # x = Dense(256, init='glorot_normal', activation='relu')(x) # x = Dropout(0.3)(x) x = Dense(128, init='glorot_normal', activation="relu")(x) x = Dropout(0)(x) Oneofkey_output = Dense(2, init='glorot_normal', activation='softmax', W_regularizer=l2(0.001))(x) OnehotNetwork = Model(Oneofkey_input, Oneofkey_output) optimization = 'Nadam' OnehotNetwork.compile(loss='binary_crossentropy', optimizer=optimization, metrics=[keras.metrics.binary_accuracy]) else: OnehotNetwork = load_model('model/' + str(folds) + '/model/' + str(train_time - 1) + 'OnehotNetwork.h5') if (trainY is not None): weight_checkpointer = ModelCheckpoint( filepath='./model/' + str(folds) + '/weight/' + str(train_time) + 'Onehotweight.h5', verbose=1, save_best_only=True, monitor='val_loss', mode='min', save_weights_only=True) early_stopping = EarlyStopping(monitor='val_loss', mode='min', patience=50) loss_checkpointer = LossModelCheckpoint( model_file_path='model/' + str(folds) + '/model/' + str(train_time) + 'OnehotNetwork.h5', monitor_file_path='model/' + str(folds) + '/loss/' + str(train_time) + 'Onehotloss.json', verbose=1, save_best_only=True, monitor='val_loss', mode='min') fitHistory = OnehotNetwork.fit( trainX, trainY, batch_size=512, epochs=5000, verbose=2, validation_data=(valX, valY), shuffle=True, class_weight='auto', callbacks=[early_stopping, loss_checkpointer, weight_checkpointer]) return OnehotNetwork
vector_dim = 300 input_layer = Input(shape=(seqlen, ), dtype='int32') embed_1 = Embedding(output_dim=vector_dim, input_dim=vocabulary_size, input_length=seqlen, name='embed_1', weights=w2v)(input_layer) embed_2 = Embedding(output_dim=vector_dim, input_dim=vocabulary_size, input_length=seqlen, name='embed_2', weights=w2v)(input_layer) cnn_layer_1 = convolutional.Convolution1D(nb_filter=1, filter_length=3, border_mode='valid', activation='relu', name='cnn_layer_1')(embed_1) cnn_layer_2 = convolutional.Convolution1D(nb_filter=1, filter_length=4, border_mode='valid', activation='relu', name='cnn_layer_2')(embed_1) cnn_layer_3 = convolutional.Convolution1D(nb_filter=1, filter_length=5, border_mode='valid', activation='relu', name='cnn_layer_3')(embed_1) cnn_layer_4 = convolutional.Convolution1D(nb_filter=1, filter_length=3, border_mode='valid',
def OnehotNetwork( train_oneofkeyX, trainY, val_oneofkeyX, valY, #pre_train_total_path = 'model/pretrain.h5', train_time=None, compilemodels=None): Oneofkey_input = Input(shape=(train_oneofkeyX.shape[1], train_oneofkeyX.shape[2])) #49*21=1029 early_stopping = EarlyStopping(monitor='val_loss', mode='min', patience=10) if (train_time == 0): x = conv.Convolution1D(201, 2, init='glorot_normal', W_regularizer=l1(0), border_mode="same")(Oneofkey_input) x = Dropout(0.4)(x) x = Activation('relu')(x) x = conv.Convolution1D(151, 3, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(x) x = Dropout(0.4)(x) x = Activation('relu')(x) x = conv.Convolution1D(151, 5, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(x) x = Dropout(0.4)(x) x = Activation('relu')(x) x = conv.Convolution1D(101, 7, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(x) x = Dropout(0.4)(x) x = Activation('relu')(x) x = core.Flatten()(x) x = BatchNormalization()(x) x = Dense(256, init='glorot_normal', activation='relu')(x) x = Dropout(0.298224)(x) x = Dense(128, init='glorot_normal', activation="relu")(x) x = Dropout(0)(x) Oneofkey_output = Dense(2, init='glorot_normal', activation='softmax', W_regularizer=l2(0.001))(x) OnehotNetwork = Model(Oneofkey_input, Oneofkey_output) optimization = 'Nadam' OnehotNetwork.compile(loss='binary_crossentropy', optimizer=optimization, metrics=[keras.metrics.binary_accuracy]) else: OnehotNetwork = load_model("model/" + str(train_time - 1) + 'model/OnehotNetwork.h5') if (trainY is not None): #checkpointer = ModelCheckpoint(filepath="model/"+str(train_time)+'model/OnehotNetwork.h5',verbose=1,save_best_only=True,monitor='val_loss',mode='min') weight_checkpointer = ModelCheckpoint( filepath="model/" + str(train_time) + 'modelweight/OnehotNetworkweight.h5', verbose=1, save_best_only=True, monitor='val_loss', mode='min', save_weights_only=True) loss_checkpointer = LossModelCheckpoint( model_file_path="model/" + str(train_time) + 'model/OnehotNetwork.h5', monitor_file_path="model/loss/" + str(train_time) + "onehotloss.json", verbose=1, save_best_only=True, monitor='val_loss', mode='min') onehotfitHistory = OnehotNetwork.fit( train_oneofkeyX, trainY, batch_size=4096, nb_epoch=50, shuffle=True, callbacks=[early_stopping, loss_checkpointer, weight_checkpointer], class_weight='auto', validation_data=(val_oneofkeyX, valY)) OnehotNetwork.save("model/" + str(train_time) + 'model/1OnehotNetwork.h5') return OnehotNetwork
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
def AlphaturnpropensityNetwork( train_physicalXa, trainY, val_physicalXa, valY, #pre_train_total_path = 'model/pretrain.h5', train_time=None, compilemodels=None): physical_A_input = Input(shape=(train_physicalXa.shape[1], train_physicalXa.shape[2])) #49*118=5782 early_stopping = EarlyStopping(monitor='val_loss', mode='min', patience=10) if (train_time == 0): x = conv.Convolution1D(201, 2, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(physical_A_input) x = Dropout(0.4)(x) x = Activation('relu')(x) x = conv.Convolution1D(151, 3, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(x) x = Dropout(0.3)(x) x = Activation('relu')(x) x = conv.Convolution1D(101, 5, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(x) x = Dropout(0.2)(x) x = Activation('relu')(x) x = conv.Convolution1D(51, 7, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(x) x = Dropout(0.1)(x) x = Activation('relu')(x) x = core.Flatten()(x) x = BatchNormalization()(x) physical_A_output = Dense(2, init='glorot_normal', activation='softmax', W_regularizer=l2(0.001))(x) AlphaturnpropensityNetwork = Model(physical_A_input, physical_A_output) optimization = 'Nadam' AlphaturnpropensityNetwork.compile( loss='binary_crossentropy', optimizer=optimization, metrics=[keras.metrics.binary_accuracy]) else: AlphaturnpropensityNetwork = load_model( "model/" + str(train_time - 1) + 'model/AlphaturnpropensityNetwork.h5') if (trainY is not None): #checkpointer = ModelCheckpoint(filepath="model/"+str(train_time)+'model/AlphaturnpropensityNetwork.h5',verbose=1,save_best_only=True,monitor='val_loss',mode='min') weight_checkpointer = ModelCheckpoint( filepath="model/" + str(train_time) + 'modelweight/AlphaturnpropensityNetworkweight.h5', verbose=1, save_best_only=True, monitor='val_loss', mode='min', save_weights_only=True) loss_checkpointer = LossModelCheckpoint( model_file_path="model/" + str(train_time) + 'model/AlphaturnpropensityNetwork.h5', monitor_file_path="model/loss/" + str(train_time) + "Anetloss.json", verbose=1, save_best_only=True, monitor='val_loss', mode='min') AfitHistory = AlphaturnpropensityNetwork.fit( train_physicalXa, trainY, batch_size=4096, nb_epoch=50, shuffle=True, callbacks=[early_stopping, loss_checkpointer, weight_checkpointer], class_weight='auto', validation_data=(val_physicalXa, valY)) AlphaturnpropensityNetwork.save("model/" + str(train_time) + 'model/1AlphaturnpropensityNetwork.h5') return AlphaturnpropensityNetwork
def MultiCNN(input, Y, batch_size=2048, nb_epoch=1000, pre_train_seq_path=None, pre_train_physical_path=None, pre_train_pssm_path=None, earlystop=None, transferlayer=1, weights=None, forkinas=False, compiletimes=0, class_weights={ 0: 0.5, 1: 1 }, train_time=0, compilemodels=None, predict=False): ########## Set Oneofkey Network Size and Data ########## trainY = kutils.to_categorical(Y) # print("trainY:", trainY) input_row = input.shape[2] input_col = input.shape[3] trainX_t = input ########## Set Early_stopping ########## early_stopping = EarlyStopping(monitor='val_loss', mode='min', patience=5) nb_epoch = 500 # set to a very big value since earlystop used ########## TrainX_t For Shape ########## trainX_t.shape = (trainX_t.shape[0], input_row, input_col) input = Input(shape=(input_row, input_col)) if compiletimes == 0: ########## Total Set Classes ########## nb_classes = 2 ########## Total Set Batch_size ########## batch_size = 2048 ########## Total Set Optimizer ########## # optimizer = SGD(lr=0.0001, momentum=0.9, nesterov= True) optimization = 'Nadam' ########## Begin Oneofkey Network ########## x = conv.Convolution1D(101, 1, init='glorot_normal', W_regularizer=l1(0.02), border_mode="same", name='0')(input) x = Dropout(0.4)(x) x = Activation('softsign')(x) # x = conv.Convolution1D(308, 3, init='glorot_normal', W_regularizer=l2(0), border_mode="same", name='1')(x) # x = Dropout(0.4)(x) # x = Activation('softsign')(x) # # x = conv.Convolution1D(308, 5, init='glorot_normal', W_regularizer=l2(0), border_mode="same", name='2')(x) # x = Dropout(0.4)(x) # x = Activation('softsign')(x) # # x = conv.Convolution1D(268, 7, init='glorot_normal', W_regularizer=l2(0), border_mode="same", name='3')(x) # x = Activation('softsign')(x) # x = Dropout(0.4)(x) output_x = core.Flatten()(x) output = BatchNormalization()(output_x) output = Dropout(0)(output) output = Dense(256, init='glorot_normal', activation='relu', name='4')(output) output = Dropout(0.298224)(output) output = Dense(128, init='glorot_normal', activation="relu", name='5')(output) output = Dropout(0)(output) output = Dense(128, activation="relu", init='glorot_normal', name='6')(output) ########## End Oneofkey Network ########## ########## Total Network After Merge ########## ''' output = Dense(512,init='glorot_normal',activation="relu",W_regularizer= l2(0.001),name = '16')(output) output = BatchNormalization()(output) output = Dropout(0.5)(output) output = Dense(256,init='glorot_normal',activation="relu",W_regularizer= l2(0.001),name = '17')(output) output = BatchNormalization()(output) output = Dropout(0.3)(output) output = Dense(49,init='glorot_normal',activation="relu",W_regularizer= l2(0.001),name = '18')(output) output = BatchNormalization()(output) output = Dropout(0)(output) ''' out = Dense(nb_classes, init='glorot_normal', activation='softmax', W_regularizer=l2(0.001), name='19')(output) ########## Total Network End ########## ########## Set Cnn ########## cnn = Model([input], out) cnn.compile(loss='binary_crossentropy', optimizer=optimization, metrics=[keras.metrics.binary_accuracy]) ########## Load Models ########## if (pre_train_seq_path is not None): seq_model = models.load_model(pre_train_seq_path) for l in range(0, 6): # the last layers is not included cnn.get_layer(name=str(l)).set_weights( seq_model.get_layer(name=str(l)).get_weights()) cnn.get_layer(name=str(l)).trainable = False else: cnn = compilemodels ########## Set Class_weight ########## # oneofkclass_weights={0 : 0.8 , 1 : 1} # physicalclass_weights={0 : 0.3 , 1 : 1} # pssmclass_weights={0 : 0.8 , 1 : 1} # totalclass_weights={0 : 0.4 , 1 : 1} checkpointer = ModelCheckpoint(filepath=str(train_time) + '-' + str(class_weights[0]) + '-' + 'merge.h5', verbose=1, save_best_only=True, monitor='val_loss', mode='min') # weight_checkpointer = ModelCheckpoint( # filepath=str(train_time) + '-' + str(class_weights[0]) + '-' + 'mergeweight.h5', verbose=1, # save_best_only=True, monitor='val_loss', mode='min', save_weights_only=True) fitHistory = cnn.fit(trainX_t, trainY, batch_size=100, nb_epoch=nb_epoch, shuffle=True, validation_split=0.4, callbacks=[early_stopping, checkpointer], class_weight=class_weights) plt.plot(fitHistory.history['loss']) plt.plot(fitHistory.history['val_loss']) plt.title('model loss 20') plt.ylabel('loss') plt.xlabel('epoch') plt.grid(True) plt.legend(['train', 'validation'], loc='upper left') plt.show() return cnn
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
def Combine_model(X1_train, X2_train): VGGmodel = keras.applications.vgg16.VGG16( include_top=False, weights='imagenet' # , input_tensor=inputs , input_shape=X1_train.shape[1:], pooling=None # , classes=1000 ) x1 = Flatten()(VGGmodel.output) x1 = Dropout(0.5)(x1) x1 = Dense(128)(x1) xs = Input(X2_train.shape[1:]) x_image = conv.Convolution1D(16, 5, padding='same', init='he_normal', W_regularizer=l1(0.01))(xs) x_image = BatchNormalization()(x_image) x_image = Activation('relu')(x_image) x_image = conv.Convolution1D(64, 5, padding='same', init='he_normal', W_regularizer=l1(0.01))(x_image) x_image = Activation('relu')(x_image) # x_image = BatchNormalization()(x_image) # x_image = conv.Convolution2D(128, (2, 2), padding='same',init='he_normal')(x_image) # x_image = Activation('relu')(x_image) # x_image = BatchNormalization()(x_image) x_image = Flatten()(x_image) x_image = Dense(128, init='he_normal', activation='relu', kernel_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l1(0.01))(x_image) x_image = keras.layers.Add()([x1, x_image]) x_image = Dropout(0.5)(x_image) preds = Dense(2, init='he_normal', activation='sigmoid')(x_image) ada = Adadelta(lr=1e-3, rho=0.95, epsilon=1e-6) model = Model([VGGmodel.input, xs], preds) # Compile model model.summary() # model.compile(loss='mean_squared_error', optimizer=sgd) model.compile(loss='categorical_crossentropy', optimizer=ada, metrics=['accuracy']) #categorical_crossentropy #binary_crossentropy # history = model.fit(X_train, Y_train, # batch_size = 20, # epochs = 50, # verbose = 1, # validation_data = (X_test, Y_test)) # score = model.evaluate(X_test, Y_test, verbose=1) # print('Test loss:', score[0]) # print('Test accuracy:', score[1]) return model
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
def create_emb_layer(self): iw3 = Input(shape=(self.max_ngram_one_class, ), dtype='int32', name="inputword3") iw4 = Input(shape=(self.max_ngram_one_class, ), dtype='int32', name="inputword4") iw5 = Input(shape=(self.max_ngram_one_class, ), dtype='int32', name="inputword5") iw6 = Input(shape=(self.max_ngram_one_class, ), dtype='int32', name="inputword6") emb_in = embeddings.Embedding(output_dim=self.vector_size, input_dim=self.ngram_size, init="uniform", mask_zero=True, name="input_layer") vv_iw3 = emb_in(iw3) vv_iw4 = emb_in(iw4) vv_iw5 = emb_in(iw5) vv_iw6 = emb_in(iw6) zm = ZeroMaskedEntries() zm.build((None, self.max_ngram_num, self.vector_size)) zero_masked_emd3 = zm(vv_iw3) zero_masked_emd4 = zm(vv_iw4) zero_masked_emd5 = zm(vv_iw5) zero_masked_emd6 = zm(vv_iw6) conv_l3 = convolutional.Convolution1D(self.vector_size, 10, border_mode='same') conv3 = conv_l3(zero_masked_emd3) conv_l4 = convolutional.Convolution1D(self.vector_size, 10, border_mode='same') conv4 = conv_l4(zero_masked_emd4) conv_l5 = convolutional.Convolution1D(self.vector_size, 10, border_mode='same') conv5 = conv_l5(zero_masked_emd5) conv_l6 = convolutional.Convolution1D(self.vector_size, 10, border_mode='same') conv6 = conv_l6(zero_masked_emd6) pool3 = pooling.AveragePooling1D(self.max_ngram_one_class, border_mode="same") pool_res3 = pool3(conv3) pool4 = pooling.AveragePooling1D(self.max_ngram_one_class, border_mode="same") pool_res4 = pool4(conv4) pool5 = pooling.AveragePooling1D(self.max_ngram_one_class, border_mode="same") pool_res5 = pool5(conv5) pool6 = pooling.AveragePooling1D(self.max_ngram_one_class, border_mode="same") pool_res6 = pool6(conv6) merge_conv = Merge(mode='ave', concat_axis=1) merged = merge_conv([pool_res3, pool_res4, pool_res5, pool_res6]) return ([iw3, iw4, iw5, iw6], emb_in, merged)
def CompositionNetwork(trainX, trainY, valX, valY, physical_C_input, folds, train_time=None): if (train_time == 0): x = conv.Convolution1D(101, 2, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(physical_C_input) x = Dropout(0.3)(x) x = Activation('relu')(x) x = conv.Convolution1D(101, 3, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(x) x = Dropout(0.3)(x) x = Activation('relu')(x) x = core.Flatten()(x) x = BatchNormalization()(x) # x = Dense(64, init='glorot_normal', activation='relu')(x) # x = BatchNormalization()(x) # x = Dropout(0.1)(x) physical_C_output = Dense(2, init='glorot_normal', activation='softmax', W_regularizer=l2(0.001))(x) CompositionNetwork = Model(physical_C_input, physical_C_output) optimization = Nadam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004) CompositionNetwork.compile(loss='binary_crossentropy', optimizer=optimization, metrics=[keras.metrics.binary_accuracy]) else: CompositionNetwork = load_model('model/' + str(folds) + '/model/' + str(train_time - 1) + 'CompositionNetwork.h5') if (trainY is not None): weight_checkpointer = ModelCheckpoint( filepath='./model/' + str(folds) + '/weight/' + str(train_time) + 'Compositionweight.h5', verbose=1, save_best_only=True, monitor='val_loss', mode='min', save_weights_only=True) early_stopping = EarlyStopping(monitor='val_loss', mode='min', patience=200) loss_checkpointer = LossModelCheckpoint( model_file_path='model/' + str(folds) + '/model/' + str(train_time) + 'CompositionNetwork.h5', monitor_file_path='model/' + str(folds) + '/loss/' + str(train_time) + 'Compositionloss.json', verbose=1, save_best_only=True, monitor='val_loss', mode='min') fitHistory = CompositionNetwork.fit( trainX, trainY, batch_size=512, epochs=5000, verbose=2, validation_data=(valX, valY), shuffle=True, class_weight='auto', callbacks=[early_stopping, loss_checkpointer, weight_checkpointer]) return CompositionNetwork
def mixallCNNmodel(trainX, trainY, valX, valY, physical_C_input, folds, train_time=None): if (train_time == 0): x = conv.Convolution1D(201, 2, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(physical_C_input) x = Dropout(0.4)(x) x = Activation('relu')(x) x = conv.Convolution1D(151, 3, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(x) x = Dropout(0.4)(x) x = Activation('relu')(x) x = conv.Convolution1D(101, 5, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(x) x = Dropout(0.4)(x) x = Activation('relu')(x) x = conv.Convolution1D(51, 7, init='glorot_normal', W_regularizer=l2(0), border_mode="same")(x) x = Dropout(0.1)(x) x = Activation('relu')(x) x = core.Flatten()(x) x = BatchNormalization()(x) physical_C_output = Dense(2, init='glorot_normal', activation='softmax', W_regularizer=l2(0.001))(x) mixallmodel = Model(physical_C_input, physical_C_output) optimization = 'Nadam' mixallmodel.compile(loss='binary_crossentropy', optimizer=optimization, metrics=[keras.metrics.binary_accuracy]) else: mixallmodel = load_model('model/' + str(folds) + '/model/' + str(train_time - 1) + 'CNNNetwork.h5') if (trainY is not None): weight_checkpointer = ModelCheckpoint( filepath='./model/' + str(folds) + '/weight/' + str(train_time) + 'CNNweight.h5', verbose=1, save_best_only=True, monitor='val_loss', mode='min', save_weights_only=True) loss_checkpointer = LossModelCheckpoint( model_file_path='model/' + str(folds) + '/model/' + str(train_time) + 'CNNNetwork.h5', monitor_file_path='model/' + str(folds) + '/loss/' + str(train_time) + 'CNNloss.json', verbose=1, save_best_only=True, monitor='val_loss', mode='min') fitHistory = mixallmodel.fit( trainX, trainY, batch_size=4096, nb_epoch=50, shuffle=True, callbacks=[early_stopping, loss_checkpointer, weight_checkpointer], class_weight='auto', validation_data=(valX, valY)) return mixallmodel
def MultiCNN(trainX, trainY, trainPhysicalX, train_pssmX, pre_train_seq_path=None, pre_train_physical_path=None, pre_train_pssm_path=None, nb_epoch=1000, earlystop=None, transferlayer=1, weights=None, forkinas=False, compiletimes=0, compilemodels=None, batch_size=2048, class_weights={ 0: 0.5, 1: 1 }, predict=False): ########## Set Oneofkey Network Size and Data ########## input_row = trainX.shape[2] input_col = trainX.shape[3] trainX_t = trainX ########## Set Physical Network Size and Data ########## physical_row = trainPhysicalX.shape[2] physical_col = trainPhysicalX.shape[3] train_physical_X_t = trainPhysicalX ########## Set Pssm Network Size and Data ########## pssm_row = train_pssmX.shape[2] pssm_col = train_pssmX.shape[3] train_pssm_X_t = train_pssmX ########## Set Early_stopping ########## if (earlystop is not None): early_stopping = EarlyStopping(monitor='val_loss', mode='min', patience=20) nb_epoch = 500 #set to a very big value since earlystop used ########## TrainX_t For Shape ########## trainX_t.shape = (trainX_t.shape[0], input_row, input_col) input = Input(shape=(input_row, input_col)) ########## Train_physical_X_t For Shape ########## train_physical_X_t.shape = (train_physical_X_t.shape[0], physical_row, physical_col) physicalInput = Input(shape=(physical_row, physical_col)) ########## Train_pssm_X_t For Shape ########## train_pssm_X_t.shape = (train_pssm_X_t.shape[0], pssm_row, pssm_col) pssmInput = Input(shape=(pssm_row, pssm_col)) if compiletimes == 0: ########## Total Set Classes ########## nb_classes = 2 ########## Total Set Batch_size ########## batch_size = 2048 ########## Total Set Optimizer ########## #optimizer = SGD(lr=0.0001, momentum=0.9, nesterov= True) optimization = 'Nadam' ########## Begin Oneofkey Network ########## x = conv.Convolution1D(201, 2, init='glorot_normal', W_regularizer=l1(0), border_mode="same", name='0')(input) x = Dropout(0.4)(x) x = Activation('softsign')(x) x = conv.Convolution1D(151, 3, init='glorot_normal', W_regularizer=l2(0), border_mode="same", name='1')(x) x = Dropout(0.4)(x) x = Activation('softsign')(x) x = conv.Convolution1D(151, 5, init='glorot_normal', W_regularizer=l2(0), border_mode="same", name='2')(x) x = Dropout(0.4)(x) x = Activation('softsign')(x) x = conv.Convolution1D(101, 7, init='glorot_normal', W_regularizer=l2(0), border_mode="same", name='3')(x) x = Activation('softsign')(x) x_reshape = core.Reshape((x._keras_shape[2], x._keras_shape[1]))(x) x = Dropout(0.4)(x) output_x = core.Flatten()(x) output = BatchNormalization()(output_x) output = Dropout(0)(output) output = Dense(256, init='glorot_normal', activation='relu', name='4')(output) output = Dropout(0.298224)(output) output = Dense(128, init='glorot_normal', activation="relu", name='5')(output) output = Dropout(0)(output) output = Dense(128, activation="relu", init='glorot_normal', name='6')(output) ########## End Oneofkey Network ########## ########## Begin Physical Network ########## physical_code_x = core.Flatten()(physicalInput) physical_code_x = BatchNormalization()(physical_code_x) physical_code_x = Dense(1024, init='glorot_normal', activation='softplus', name='7')(physical_code_x) physical_code_x = BatchNormalization()(physical_code_x) physical_code_x = Dropout(0.2)(physical_code_x) physical_code_x = Dense(512, init='glorot_normal', activation='softplus', name='8')(physical_code_x) physical_code_x = BatchNormalization()(physical_code_x) physical_code_x = Dropout(0.4)(physical_code_x) physical_code_x = Dense(256, init='glorot_normal', activation='softplus', name='9')(physical_code_x) physical_code_x = BatchNormalization()(physical_code_x) physical_code_x = Dropout(0.5)(physical_code_x) output_physical_x = Dense(128, init='glorot_normal', activation='relu', name='10')(physical_code_x) ########## End Physical Network ########## ########## Begin Pssm Network ########## pssm_x = conv.Convolution1D(200, 1, init='glorot_normal', W_regularizer=l1(0), border_mode="same", name='11')(pssmInput) pssm_x = Activation('relu')(pssm_x) pssm_x = Dropout(0.5)(pssm_x) pssm_x = conv.Convolution1D(150, 8, init='glorot_normal', W_regularizer=l1(0), border_mode="same", name='12')(pssm_x) pssm_x = Activation('relu')(pssm_x) pssm_x = Dropout(0.5)(pssm_x) pssm_x = conv.Convolution1D(200, 9, init='glorot_normal', W_regularizer=l1(0), border_mode="same", name='13')(pssm_x) pssm_x = Activation('relu')(pssm_x) pssm_x = Dropout(0.5)(pssm_x) pssm_x_reshape1 = core.Reshape((pssm_col, pssm_row))(pssmInput) pssm_x_reshape2 = conv.Convolution1D(200, 1, init='glorot_normal', W_regularizer=l1(0), border_mode="same", name='14')(pssm_x_reshape1) pssm_x_reshape2 = Activation('relu')(pssm_x_reshape2) pssm_x_reshape2 = Dropout(0.5)(pssm_x_reshape2) pssm_x_reshape2 = conv.Convolution1D(150, 3, init='glorot_normal', W_regularizer=l1(0), border_mode="same", name='15')(pssm_x_reshape2) pssm_x_reshape2 = Activation('relu')(pssm_x_reshape2) pssm_x_reshape2 = Dropout(0.5)(pssm_x_reshape2) pssm_x_reshape2 = conv.Convolution1D(200, 7, init='glorot_normal', W_regularizer=l1(0), border_mode="same", name='16')(pssm_x_reshape2) pssm_x_reshape2 = Activation('relu')(pssm_x_reshape2) pssm_x_reshape2 = Dropout(0.5)(pssm_x_reshape2) pssm_x = core.Flatten()(pssm_x) pssm_x_reshape2 = core.Flatten()(pssm_x_reshape2) pssm_output = merge([pssm_x, pssm_x_reshape2], mode='concat') pssm_output = Dropout(0)(pssm_output) pssm_output = BatchNormalization()(pssm_output) pssm_output = Dense(128, init='glorot_normal', activation='relu', name='17')(pssm_output) pssm_output = Dropout(0.298224)(pssm_output) pssm_output = Dense(128, init='glorot_normal', activation='relu', name='18')(pssm_output) pssm_output = Dropout(0)(pssm_output) ########## End Pssm Network ########## ########## Set Output For Merge ########## output = merge([output, output_physical_x, pssm_output], mode='concat') ########## Total Network After Merge ########## ''' output = Dense(512,init='glorot_normal',activation="relu",W_regularizer= l2(0.001),name = '16')(output) output = BatchNormalization()(output) output = Dropout(0.5)(output) output = Dense(256,init='glorot_normal',activation="relu",W_regularizer= l2(0.001),name = '17')(output) output = BatchNormalization()(output) output = Dropout(0.3)(output) output = Dense(49,init='glorot_normal',activation="relu",W_regularizer= l2(0.001),name = '18')(output) output = BatchNormalization()(output) output = Dropout(0)(output) ''' out = Dense(nb_classes, init='glorot_normal', activation='softmax', W_regularizer=l2(0.001), name='19')(output) ########## Total Network End ########## ########## Set Cnn ########## cnn = Model([input, physicalInput, pssmInput], out) cnn.compile(loss='binary_crossentropy', optimizer=optimization, metrics=[keras.metrics.binary_accuracy]) ########## Load Models ########## if (pre_train_seq_path is not None): seq_model = models.load_model(pre_train_seq_path) for l in range(0, 6): #the last layers is not included cnn.get_layer(name=str(l)).set_weights( seq_model.get_layer(name=str(l)).get_weights()) cnn.get_layer(name=str(l)).trainable = False #cnn.get_layer() if (pre_train_physical_path is not None): physical_model = models.load_model(pre_train_physical_path) for l in range(7, 10): #len(seq_model.layers), (len(seq_model.layers)+len(physical_model.layers)-1)): #the last layer is not included cnn.get_layer(name=str(l)).set_weights( physical_model.get_layer(name=str(l)).get_weights()) cnn.get_layer(name=str(l)).trainable = False if (pre_train_pssm_path is not None): pssm_model = models.load_model(pre_train_pssm_path) for l in range(11, 18): #len(seq_model.layers), (len(seq_model.layers)+len(pssm_model.layers)-1)): #the last layer is not included cnn.get_layer(name=str(l)).set_weights( pssm_model.get_layer(name=str(l)).get_weights()) cnn.get_layer(name=str(l)).trainable = False else: cnn = compilemodels ########## Set Class_weight ########## #oneofkclass_weights={0 : 0.78 , 1 : 1} #physicalclass_weights={0 : 0.3 , 1 : 1} #pssmclass_weights={0 : 0.95 , 1 : 1} #totalclass_weights={0 : 0.1 , 1 : 1} if (predict is False): if (trainY 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)) #fitHistory = cnn.fit(train_physical_X_t, trainY, batch_size=batch_size, nb_epoch=nb_epoch, validation_data=(val_physical_x_t, valY)) #fitHistory = cnn.fit(train_pssm_X_t, trainY, batch_size=batch_size, nb_epoch=nb_epoch, validation_data=(val_pssm_x_t, valY)) fitHistory = cnn.fit( [trainX_t, train_physical_X_t, train_pssm_X_t], trainY, batch_size=batch_size, nb_epoch=nb_epoch, validation_data=([valX_t, val_physical_x_t, val_pssm_x_t], valY)) else: #checkpointer = ModelCheckpoint(filepath='oneofk.h5',verbose=1,save_best_only=True) #weight_checkpointer = ModelCheckpoint(filepath='oneofkweight.h5',verbose=1,save_best_only=True,monitor='val_acc',mode='max',save_weights_only=True) #fitHistory = cnn.fit(trainX_t, trainY, batch_size=batch_size, nb_epoch=nb_epoch, shuffle= True, validation_split=0.2, callbacks=[early_stopping,checkpointer,weight_checkpointer], class_weight = oneofkclass_weights) #checkpointer = ModelCheckpoint(filepath='physical.h5',verbose=1,save_best_only=True,monitor='val_acc',mode='max') #weight_checkpointer = ModelCheckpoint(filepath='physicalweight.h5',verbose=1,save_best_only=True,monitor='val_acc',mode='max',save_weights_only=True) #fitHistory = cnn.fit(train_physical_X_t, trainY, batch_size=batch_size, nb_epoch=nb_epoch, shuffle= True, validation_split=0.2, callbacks=[early_stopping,checkpointer,weight_checkpointer], class_weight = physicalclass_weights) #checkpointer = ModelCheckpoint(filepath='pssm.h5',verbose=1,save_best_only=True,monitor='val_acc',mode='max') #weight_checkpointer = ModelCheckpoint(filepath='pssmweight.h5',verbose=1,save_best_only=True,monitor='val_acc',mode='max',save_weights_only=True) #fitHistory = cnn.fit(train_pssm_X_t, trainY, batch_size=batch_size, nb_epoch=nb_epoch, shuffle= True, validation_split=0.2, callbacks=[early_stopping,checkpointer,weight_checkpointer], class_weight = pssmclass_weights) checkpointer = ModelCheckpoint(filepath=str(compiletimes) + '-' + str(class_weights[0]) + '-' + 'merge.h5', verbose=1, save_best_only=True, monitor='val_loss', mode='min') weight_checkpointer = ModelCheckpoint( filepath=str(compiletimes) + '-' + str(class_weights[0]) + '-' + 'mergeweight.h5', verbose=1, save_best_only=True, monitor='val_loss', mode='min', save_weights_only=True) fitHistory = cnn.fit( [trainX_t, train_physical_X_t, train_pssm_X_t], trainY, batch_size=batch_size, nb_epoch=nb_epoch, shuffle=True, validation_split=0.4, callbacks=[ early_stopping, checkpointer, weight_checkpointer ], class_weight=class_weights) #with open('siqingaowa.txt','a') as f: #f.write(str(fitHistory.history)) #f.close(); else: #fitHistory = cnn.fit(trainX_t, trainY, batch_size=batch_size, nb_epoch=nb_epoch) #fitHistory = cnn.fit(train_physical_X_t, trainY, batch_size=batch_size, nb_epoch=nb_epoch) #fitHistory = cnn.fit(train_pssm_X_t, trainY, batch_size=batch_size, nb_epoch=nb_epoch) fitHistory = cnn.fit( [trainX_t, train_physical_X_t, train_pssm_X_t], trainY, batch_size=batch_size, nb_epoch=nb_epoch) return cnn