Beispiel #1
0
def wnd_test(X, y):
    auc = []
    pr = []
    sr = []
    # X_val2 = X.tolist()
    # for i in range(0, len(X_val2)):
    #     X_val2[i] = X_val2[i][0:3967]
    # X = np.array(X_val2)
    for i in range(1, 11):
        model = keras.models.load_model(
            '../saved_models/WnD/WnD' + str(i) + '.h5',
            custom_objects={
                'masked_loss_function': masked_loss_function,
                'masked_accuracy': masked_accuracy
            })
        score_for_each_drug = ROC_PR.ROC(model,
                                         X,
                                         y, ("wide-n-deep" + "BO_delete"),
                                         True,
                                         bccdc=True)
        spec_recall, prec_recall = ROC_PR.PR(model, X, y, bccdc=True)

        # print('AUC-ROC:', score_for_each_drug)
        # print("recall at 95 spec: ", spec_recall)
        # print("precision recall: ", prec_recall)
        auc.append(score_for_each_drug)
        pr.append(prec_recall)
        sr.append(spec_recall)
    print(auc)
    print(pr)
    print(sr)
Beispiel #2
0
def model_256_128_64_2_100Ep(FrameSize, X, X_train, X_test, y_train, y_test):
    model = Sequential()
    # model.add(Embedding(2, 50, input_length=None))
    # model.add(LSTM(256, return_sequences=True))

    model.add(
        LSTM(256,
             input_shape=(FrameSize, X[0].shape[1]),
             return_sequences=True,
             recurrent_dropout=0.3))
    model.add(SpatialDropout1D(0.2))
    model.add(LSTM(128, return_sequences=False, recurrent_dropout=0.3))
    model.add(Dropout(0.2))
    model.add(Dense(64))
    model.add(Dropout(0.2))
    model.add(Dense(2, activation='softmax'))

    model.compile(loss='categorical_crossentropy',
                  optimizer='Adam',
                  metrics=['accuracy'])

    history = model.fit(X_train,
                        y_train,
                        epochs=100,
                        batch_size=128,
                        shuffle=True,
                        verbose=2,
                        validation_data=(X_test, y_test))

    plot.plot(history, "One_256_128_64_2_100Ep")

    ROC_PR.ROC(model, X_test, y_test, "One_256_128_64_2_100Ep")
Beispiel #3
0
def run_one_fold(model):
    model.compile(loss=masked_loss_function,
                  optimizer='Adam',
                  metrics=[masked_accuracy])
    history = model.fit(
        X_train,
        y_train,
        epochs=epochs,
        batch_size=128,
        # shuffle=True,
        verbose=2,
        validation_data=(X_val, y_val),
        callbacks=[MyCustomCallback()])

    score = ROC_PR.ROC_Score(model, X_val, y_val)
    score_test = ROC_PR.ROC_Score(model, X_test, y_test)
    score_for_each_drug = ROC_PR.ROC(model, X_test, y_test,
                                     ("wide-n-deep" + "BO_delete"), True)
    spec_recall, prec_recall = ROC_PR.PR(model, X_test, y_test)

    print('area under ROC curve for val:', score)
    print('area under ROC curve for test:', score_test)
    print(score_for_each_drug)
    print("recall at 95 spec: ", spec_recall)
    print("precision recall: ", prec_recall)

    string_random = get_random_string(17)
    print(string_random)
    model.save('wnd_' + string_random + '.h5')

    return score
Beispiel #4
0
def lrcn_test(X, y):
    auc = []
    pr = []
    sr = []
    for i in range(0, 10):
        model = keras.models.load_model(
            '../saved_models/LRCN/lrcn_' + str(i) + '.h5',
            custom_objects={
                'masked_loss_function': masked_loss_function,
                'masked_accuracy': masked_accuracy
            })
        score_for_each_drug = ROC_PR.ROC(model,
                                         X,
                                         y, ("LRCN" + "BO_delete"),
                                         True,
                                         bccdc=True)
        spec_recall, prec_recall = ROC_PR.PR(model, X, y, bccdc=True)

        # print('AUC-ROC:', score_for_each_drug)
        # print("recall at 95 spec: ", spec_recall)
        # print("precision recall: ", prec_recall)
        auc.append(score_for_each_drug)
        pr.append(prec_recall)
        sr.append(spec_recall)
    print(auc)
    print(pr)
    print(sr)
Beispiel #5
0
def model_CNN_LSTM_limited_1(FrameSize, X, X_train, X_test, y_train, y_test,
                             epoch, earlyStopping, name):
    print(X.shape)
    print(FrameSize)
    model = Sequential()
    model.add(Dropout(0.43369355853937297))
    model.add(
        Conv1D(filters=5, kernel_size=4, activation='relu', padding='same'))
    model.add(MaxPooling1D(pool_size=4, padding='same'))
    model.add(
        Conv1D(filters=7, kernel_size=7, activation='relu', padding='same'))
    model.add(MaxPooling1D(pool_size=7, padding='same'))

    model.add(LSTM(398, return_sequences=True, recurrent_dropout=0.3))
    model.add(SpatialDropout1D(0.43369355853937297))
    model.add(LSTM(106, return_sequences=True, recurrent_dropout=0.3))
    model.add(SpatialDropout1D(0.43369355853937297))
    model.add(LSTM(475, return_sequences=True, recurrent_dropout=0.3))
    model.add(SpatialDropout1D(0.43369355853937297))
    model.add(LSTM(264, return_sequences=False, recurrent_dropout=0.3))
    model.add(Dropout(0.43369355853937297))

    model.add(Dense(352))
    model.add(Dropout(0.43369355853937297))
    model.add(Dense(378))
    model.add(Dropout(0.43369355853937297))

    model.add(Dense(7, activation='sigmoid'))

    model.compile(loss=masked_loss_function,
                  optimizer='Adam',
                  metrics=[masked_accuracy])

    history = model.fit(
        X_train,
        y_train,
        epochs=epoch,
        batch_size=128,
        # shuffle=True,
        verbose=2,
        validation_data=(X_test, y_test),
        callbacks=[
            earlyStopping,
            ModelCheckpoint('result/CNN_LSTM_limited_1.h5',
                            monitor='val_masked_accuracy',
                            mode='max',
                            save_best_only=True)
        ])

    # plot_model(model, to_file='model_plot.png', show_shapes=True)

    plot.plot(history, ("CNN_LSTM_limited_1" + name))

    score = ROC_PR.ROC(model,
                       X_test,
                       y_test, ("CNN_LSTM_limited_1" + name),
                       True,
                       limited=True)
    return score
Beispiel #6
0
def model_CNN_LSTM_random_data(FrameSize, X, X_train, X_test, y_train, y_test,
                               epoch, earlyStopping, name):
    print(X.shape)
    print(FrameSize)
    model = Sequential()

    model.add(Dropout(0.3311428861138142))
    model.add(
        Conv1D(filters=4, kernel_size=6, activation='relu', padding='same'))
    model.add(MaxPooling1D(pool_size=4, padding='same'))
    model.add(Dropout(0.3311428861138142))
    model.add(
        Conv1D(filters=7, kernel_size=4, activation='relu', padding='same'))
    model.add(MaxPooling1D(pool_size=4, padding='same'))
    model.add(Dropout(0.3311428861138142))
    model.add(
        Conv1D(filters=6, kernel_size=6, activation='relu', padding='same'))
    model.add(MaxPooling1D(pool_size=4, padding='same'))
    model.add(Dropout(0.3311428861138142))
    model.add(
        Conv1D(filters=4, kernel_size=4, activation='relu', padding='same'))
    model.add(MaxPooling1D(pool_size=4, padding='same'))

    model.add(LSTM(425, return_sequences=True, recurrent_dropout=0.3))
    model.add(Dropout(0.3311428861138142))
    model.add(LSTM(189, return_sequences=True, recurrent_dropout=0.3))
    model.add(Dropout(0.3311428861138142))
    model.add(LSTM(283, return_sequences=True, recurrent_dropout=0.3))
    model.add(Dropout(0.3311428861138142))
    model.add(LSTM(333, return_sequences=False, recurrent_dropout=0.3))
    model.add(Dropout(0.3311428861138142))

    model.add(Dense(331))
    model.add(Dropout(0.3311428861138142))
    model.add(Dense(12, activation='sigmoid'))

    model.compile(loss=masked_loss_function,
                  optimizer='Adam',
                  metrics=[masked_accuracy])

    history = model.fit(X_train,
                        y_train,
                        epochs=epoch,
                        batch_size=128,
                        verbose=2,
                        validation_data=(X_test, y_test),
                        callbacks=[
                            earlyStopping,
                            ModelCheckpoint('result/CNN256_LSTM128_64_2.h5',
                                            monitor='val_masked_accuracy',
                                            mode='max',
                                            save_best_only=True)
                        ])

    plot.plot(history, ("LRCN" + name))

    score = ROC_PR.ROC(model, X_test, y_test, ("LRCN" + name), True)
    return score, ROC_PR.ROC_Score(model, X_train, y_train, limited=False)
Beispiel #7
0
def model_CNN_LSTM_time(FrameSize, X, X_train, X_test, y_train, y_test, epoch,
                        earlyStopping, name):
    print(X.shape)
    print(X_train.shape)
    print(X_test.shape)
    print(FrameSize)
    print(y_train.shape)
    print(y_test.shape)
    X_train = X_train.reshape(X_train.shape[0], X_train.shape[1],
                              X_train.shape[2], 1)
    X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2],
                            1)
    # y_train = y_train.reshape(7060, 12, 1)
    # y_test = y_test.reshape(785, 12, 1)
    model = Sequential()
    model.add(Dropout(0.1))
    model.add(
        TimeDistributed(
            Conv1D(filters=8, kernel_size=3, activation='relu',
                   padding='same')))
    model.add(TimeDistributed(MaxPooling1D(pool_size=3, padding='same')))
    model.add(
        TimeDistributed(
            Conv1D(filters=8, kernel_size=3, activation='relu',
                   padding='same')))
    model.add(TimeDistributed(MaxPooling1D(pool_size=3, padding='same')))
    model.add(TimeDistributed(Flatten()))

    model.add(LSTM(518, return_sequences=False, recurrent_dropout=0.3))
    model.add(Dropout(0.1))

    model.add(Dense(64))
    model.add(Dropout(0.1))

    model.add(Dense(12, activation='sigmoid'))

    model.compile(loss=masked_loss_function,
                  optimizer='Adam',
                  metrics=[masked_accuracy])

    history = model.fit(X_train,
                        y_train,
                        epochs=epoch,
                        batch_size=128,
                        verbose=2,
                        validation_data=(X_test, y_test),
                        callbacks=[
                            earlyStopping,
                            ModelCheckpoint('result/CNN256_LSTM128_64_2.h5',
                                            monitor='val_masked_accuracy',
                                            mode='max',
                                            save_best_only=True)
                        ])

    plot.plot(history, ("LRCN" + name))

    score = ROC_PR.ROC(model, X_test, y_test, ("LRCN" + name), True)
    return score, ROC_PR.ROC_Score(model, X_train, y_train, limited=False)
Beispiel #8
0
def model_lrcn_simple(FrameSize,
                      X,
                      X_train,
                      X_test,
                      y_train,
                      y_test,
                      epoch,
                      earlyStopping,
                      name,
                      limited=False):
    print(X.shape)
    print(FrameSize)
    model = Sequential()
    model.add(Dropout(0.2))
    model.add(
        Conv1D(filters=5, kernel_size=5, activation='relu', padding='same'))
    model.add(MaxPooling1D(pool_size=3, padding='same'))
    model.add(LSTM(256, return_sequences=True, recurrent_dropout=0.3))
    model.add(SpatialDropout1D(0.2))
    model.add(LSTM(128, return_sequences=False, recurrent_dropout=0.3))
    model.add(Dropout(0.2))
    model.add(Dense(128))
    model.add(Dropout(0.2))
    if limited:
        model.add(Dense(7, activation='sigmoid'))
    else:
        model.add(Dense(12, activation='sigmoid'))

    model.compile(loss=masked_loss_function,
                  optimizer='Adam',
                  metrics=[masked_accuracy])

    history = model.fit(X_train,
                        y_train,
                        epochs=epoch,
                        batch_size=128,
                        verbose=2,
                        validation_data=(X_test, y_test),
                        callbacks=[
                            earlyStopping,
                            ModelCheckpoint('result/CNN256_LSTM128_64_2.h5',
                                            monitor='val_masked_accuracy',
                                            mode='max',
                                            save_best_only=True)
                        ])

    plot.plot(history, ("LRCN" + name))

    score = ROC_PR.ROC(model,
                       X_test,
                       y_test, ("LRCN" + name),
                       True,
                       limited=limited)
    return score
Beispiel #9
0
def model_256_128_64_2BS(FrameSize, X, X_train, X_test, y_train, y_test,
                         epoch):
    model = Sequential()
    # model.add(Embedding(2, 50, input_length=None))
    # model.add(LSTM(256, return_sequences=True))

    model.add(
        LSTM(256,
             input_shape=(FrameSize, X[0].shape[1]),
             return_sequences=True,
             recurrent_dropout=0.3))
    model.add(SpatialDropout1D(0.2))
    model.add(LSTM(128, return_sequences=False, recurrent_dropout=0.3))
    model.add(Dropout(0.2))
    model.add(Dense(64))
    model.add(Dropout(0.2))
    model.add(Dense(2, activation='sigmoid'))

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

    history = model.fit(X_train,
                        y_train,
                        epochs=epoch,
                        batch_size=128,
                        verbose=2,
                        shuffle=True,
                        validation_data=(X_test, y_test),
                        callbacks=[
                            ModelCheckpoint('result/One_256_128_64_2BS.h5',
                                            monitor='val_accuracy',
                                            mode='max',
                                            save_best_only=True)
                        ])

    # model.save_weights("result/One_256_128_64_2BS.h5")

    plot.plot(history, "One_256_128_64_2BS")

    ROC_PR.ROC(model, X_test, y_test, "One_256_128_64_2BS")
Beispiel #10
0
def model_CNN256_LSTM128_64_2(FrameSize, X, X_train, X_test, y_train, y_test,
                              epoch):
    model = Sequential()
    model.add(Dropout(0.2))
    model.add(
        Conv1D(filters=5, kernel_size=3, activation='relu', padding='same'))
    model.add(MaxPooling1D(pool_size=3))
    model.add(LSTM(256, return_sequences=True, recurrent_dropout=0.3))
    model.add(SpatialDropout1D(0.2))
    model.add(LSTM(128, return_sequences=False, recurrent_dropout=0.3))
    model.add(Dropout(0.2))
    model.add(Dense(64))
    model.add(Dropout(0.2))
    model.add(Dense(2, activation='sigmoid'))

    model.compile(loss='categorical_crossentropy',
                  optimizer='Adam',
                  metrics=['accuracy'])

    history = model.fit(X_train,
                        y_train,
                        epochs=epoch,
                        batch_size=128,
                        shuffle=True,
                        verbose=2,
                        validation_data=(X_test, y_test),
                        callbacks=[
                            ModelCheckpoint('result/CNN256_LSTM128_64_2.h5',
                                            monitor='accuracy',
                                            mode='max',
                                            save_best_only=True)
                        ])

    # plot_model(model, to_file='model_plot.png', show_shapes=True)

    plot.plot(history, "One_CNN256_LSTM128_64_2")

    ROC_PR.ROC(model, X_test, y_test, "One_CNN256_LSTM128_64_2", False)
Beispiel #11
0
def model_256_128_64_2(FrameSize, X, X_train, X_test, y_train, y_test, epoch,
                       earlyStopping):
    model = Sequential()
    model.add(
        LSTM(256,
             input_shape=(FrameSize, X[0].shape[1]),
             return_sequences=True,
             recurrent_dropout=0.3))
    model.add(SpatialDropout1D(0.2))
    model.add(LSTM(128, return_sequences=False, recurrent_dropout=0.3))
    model.add(Dropout(0.2))
    model.add(Dense(64))
    model.add(Dropout(0.2))
    model.add(Dense(12, activation='sigmoid'))

    model.compile(loss=masked_loss_function,
                  optimizer='Adam',
                  metrics=[masked_accuracy])

    print(model.summary())

    history = model.fit(X_train,
                        y_train,
                        epochs=epoch,
                        batch_size=128,
                        shuffle=True,
                        verbose=2,
                        validation_data=(X_test, y_test),
                        callbacks=[
                            earlyStopping,
                            ModelCheckpoint('result/256_128_64_2.h5',
                                            monitor='val_masked_accuracy',
                                            mode='max',
                                            save_best_only=True)
                        ])

    plot.plot(history, "256_128_64_2")
    ROC_PR.ROC(model, X_test, y_test, "256_128_64_2", True)
Beispiel #12
0
def run_k_fold(model):
    global X_train, X_test, y_train, y_test
    global X, y, check
    if check == 0:
        check = 1
        X = np.append(X_train, X_test, axis=0)
        y = np.append(y_train, y_test, axis=0)
        X_train = 0
        X_test = 0
        y_train = 0
        y_test = 0

    cvscores = []
    scores_each_drug = []
    for i in range(0, 10):
        print("fold:" + str(i))
        length = int(len(X) / 10)
        if i == 0:
            X_train_tmp = X[length:]
            X_test_tmp = X[0:length]
            y_train_tmp = y[length:]
            y_test_tmp = y[0:length]
        elif i != 9:
            X_train_tmp = np.append(X[0:length * i],
                                    X[length * (i + 1):],
                                    axis=0)
            X_test_tmp = X[length * i:length * (i + 1)]
            y_train_tmp = np.append(y[0:length * i],
                                    y[length * (i + 1):],
                                    axis=0)
            y_test_tmp = y[length * i:length * (i + 1)]
        else:
            X_train_tmp = X[0:length * i]
            X_test_tmp = X[length * i:]
            y_train_tmp = y[0:length * i]
            y_test_tmp = y[length * i:]

        model.compile(loss=masked_loss_function,
                      optimizer='Adam',
                      metrics=[masked_accuracy])

        # plot_model(model, to_file='model_plot.png', show_shapes=True)

        history = model.fit(
            X_train_tmp,
            y_train_tmp,
            epochs=epochs,
            batch_size=128,
            # shuffle=True,
            verbose=2,
            validation_data=(X_test_tmp, y_test_tmp))

        score = ROC_PR.ROC_Score(model,
                                 X_train_tmp,
                                 y_train_tmp,
                                 limited=limited)
        print('area under ROC curve:', score)
        cvscores.append(score)
        scores_each_drug.append(
            ROC_PR.ROC(model, X_test_tmp, y_test_tmp,
                       ("LRCN" + "BO_delete" + str(i)), True))
    print(np.mean(cvscores))
    if np.mean(cvscores) > 0.97:
        model.save()
    print(scores_each_drug)
    return np.mean(cvscores)
Beispiel #13
0
def run_one_fold(model):
    #
    # X_train2 = tf.cast(X_train, tf.float32)
    # X_test2 = tf.cast(X_test, tf.float32)
    # X_val2 = tf.cast(X_val, tf.float32)
    # y_train2 = tf.cast(y_train, tf.float32)
    # y_test2 = tf.cast(y_test, tf.float32)
    # y_val2 = tf.cast(y_val, tf.float32)

    # X_train2 = np.array(X_train)
    # X_train2 = tf.convert_to_tensor(X_train2,dtype=tf.float32)
    # X_train2 = tf.cast(X_train2, tf.float32)

    # y_train2 = np.array(y_train)
    # y_train2 = tf.convert_to_tensor(y_train2, dtype=tf.float32)
    # y_train2 = np.array(y_train)
    # y_train2 = tf.cast(y_train2, tf.float32)

    # print(X_train.)
    # Train the model for a specified number of epochs.

    X_train2 = np.array(X_train).astype(np.float)
    X_test2 = np.array(X_test).astype(np.float)
    X_val2 = np.array(X_val).astype(np.float)

    y_train2 = np.array(y_train).astype(np.float)
    y_test2 = np.array(y_test).astype(np.float)
    y_val2 = np.array(y_val).astype(np.float)

    model.compile(loss=masked_loss_function,
                  optimizer='Adam',
                  metrics=[masked_accuracy])
    # Train the model with the train dataset.
    history = model.fit(
        X_train2,
        y_train2,
        epochs=epochs,
        batch_size=128,
        # shuffle=True,
        verbose=2,
        validation_data=(X_val2, y_val2))

    # Evaluate the model with the eval dataset.
    # score = model.evaluate(X_test, y_test, steps=10, verbose=0)
    # print('Test loss:', score[0])
    # print('Test accuracy:', score[1])

    # Return the accuracy.
    # print(history.history['val_masked_accuracy'])
    score = ROC_PR.ROC_Score(model, X_val2, y_val2)
    score_test = ROC_PR.ROC_Score(model, X_test2, y_test2)
    score_for_each_drug = ROC_PR.ROC(model, X_test2, y_test2,
                                     ("LRCN" + "BO_delete"), True)
    spec_recall, prec_recall = ROC_PR.PR(model, X_test2, y_test2)

    print('area under ROC curve for val:', score)
    print('area under ROC curve for test:', score_test)
    print(score_for_each_drug)
    print("recall at 95 spec: ", spec_recall)
    print("precision recall: ", prec_recall)

    global scores
    global fold_num
    global comp
    if len(scores) == 0:
        # string_random = get_random_string(17)
        # print(string_random)
        print(scores)
        print(score)
        model.save('LRCN' + str(comp) + '_' + str(fold_num) + '.h5')
        scores.append(score)
    else:
        br = 0
        for iter in range(0, len(scores)):
            if scores[iter] > score:
                print(scores)
                print(score)
                br = 1
                break
        if br == 0:
            print(br)
            print(scores)
            print(score)
            model.save('LRCN' + str(comp) + '_' + str(fold_num) + '.h5')
        scores.append(score)

    # from lime import lime_tabular
    # ins = lime_tabular.LimeTabularExplainer
    # explainer = lime_tabular.LimeTabularExplainer.explain_instance(self=ins ,data_row=X_train, predict_fn=model, num_samples=6354)
    # explainer = lime_tabular.LimeTabularExplainer.explain_instance
    # import lime
    # import lime.lime_tabular
    # import pandas as pd

    # explainer = lime.lime_tabular.LimeTabularExplainer(X_train)
    # print(len(X_train))
    # exp = explainer.explain_instance(len(X_train), model.predict, num_features=len(X_train[0]))
    #
    # exp.show_in_notebook(show_table=True)
    #
    # exp.as_list()

    # shap.initjs()
    #
    # explainer = shap.DeepExplainer(model, X_train2[:100])
    # shap_values = explainer.shap_values(X_test2[:10])
    # shap.summary_plot(shap_values, X_test2, plot_type='bar')

    # worked

    # TODO this block worked
    # def score(X_test, y_test):
    #     return ROC_PR.ROC_Score(model, X_test, y_test)
    #
    # from eli5.permutation_importance import get_score_importances
    #
    # feature_score = []
    #
    # for i in range(0, len(X_test2[0])):
    #     lst = []
    #     lst.append(i)
    #     base_score, score_decreases = get_score_importances(score, X_test2, y_test2, n_iter=1, columns_to_shuffle=lst)
    #     feature_importances = np.mean(score_decreases, axis=0)
    #     feature_score.append(feature_importances[0])
    #     print(feature_score)
    #
    # print(feature_score)

    # model.save('model_save.h5')
    #
    # import deeplift
    # from deeplift.conversion import kerasapi_conversion as kc
    #
    # import keras
    # # print(keras.__version__)
    # # deeplift_model = kc.convert_sequential_model(model)
    # deeplift_model = \
    #     kc.convert_model_from_saved_files(
    #         'model_save.h5',
    #         nonlinear_mxts_mode=deeplift.layers.NonlinearMxtsMode.DeepLIFT_GenomicsDefault)
    #
    # find_scores_layer_idx = 0
    #
    # deeplift_contribs_func = deeplift_model.get_target_contribs_func(
    #     find_scores_layer_idx=find_scores_layer_idx,
    #     target_layer_idx=-1)
    #
    # scores = np.array(deeplift_contribs_func(task_idx=0,
    #                                          input_data_list=[X],
    #                                          batch_size=10,
    #                                          progress_update=1000))

    # print(scores)
    return score
Beispiel #14
0
def model_CNN256_LSTM128_64_2(FrameSize,
                              X,
                              X_train,
                              X_test,
                              y_train,
                              y_test,
                              epoch,
                              earlyStopping,
                              name,
                              dropout2_rate,
                              dense_1,
                              filterCNN,
                              kernelCNN,
                              LSTM1,
                              LSTM2,
                              recurrent_dropout,
                              limited=False):
    print(X.shape)
    print(FrameSize)
    model = Sequential()
    # model.add(TimeDistributed(Conv1D(filters=1, kernel_size=3, activation='relu', padding='same', input_shape=(FrameSize, X[0].shape[1], 1))))
    # model.add(TimeDistributed(MaxPooling1D(pool_size=3)))
    # model.add(TimeDistributed(Flatten()))
    model.add(Dropout(dropout2_rate))
    # model.add(Conv1D(filters=5, kernel_size=3, activation='relu', padding='same'))
    model.add(
        Conv1D(filters=filterCNN,
               kernel_size=kernelCNN,
               activation='relu',
               padding='same'))
    model.add(MaxPooling1D(pool_size=3, padding='same'))
    # model.add(TimeDistributed(Flatten()))
    # model.add(LSTM(256, return_sequences=True, recurrent_dropout=0.3))
    model.add(
        LSTM(LSTM1, return_sequences=True,
             recurrent_dropout=recurrent_dropout))
    model.add(SpatialDropout1D(dropout2_rate))
    # model.add(LSTM(128, return_sequences=False, recurrent_dropout=0.3))
    model.add(
        LSTM(LSTM2,
             return_sequences=False,
             recurrent_dropout=recurrent_dropout))
    model.add(Dropout(dropout2_rate))
    # model.add(Dense(64))
    model.add(Dense(dense_1))
    model.add(Dropout(dropout2_rate))
    if limited:
        model.add(Dense(7, activation='sigmoid'))
    else:
        model.add(Dense(12, activation='sigmoid'))

    model.compile(loss=masked_loss_function,
                  optimizer='Adam',
                  metrics=[masked_accuracy])

    history = model.fit(
        X_train,
        y_train,
        epochs=epoch,
        batch_size=128,
        # shuffle=True,
        verbose=2,
        validation_data=(X_test, y_test),
        callbacks=[
            earlyStopping,
            ModelCheckpoint('result/CNN256_LSTM128_64_2.h5',
                            monitor='val_masked_accuracy',
                            mode='max',
                            save_best_only=True)
        ])

    # plot_model(model, to_file='model_plot.png', show_shapes=True)

    plot.plot(history, ("LRCN" + name))

    score = ROC_PR.ROC(model,
                       X_test,
                       y_test, ("LRCN" + name),
                       True,
                       limited=limited)
    return score
Beispiel #15
0
def model_shuffle_index_0(FrameSize, X, X_train, X_test, y_train, y_test,
                          epoch, earlyStopping, name, index):
    print(X.shape)
    print(FrameSize)

    model = Sequential()

    if index == 0:
        model.add(Dropout(0.3975303416300372))
        model.add(
            Conv1D(filters=4, kernel_size=3, activation='relu',
                   padding='same'))
        model.add(MaxPooling1D(pool_size=3, padding='same'))
        model.add(Dropout(0.3975303416300372))
        model.add(
            Conv1D(filters=6, kernel_size=3, activation='relu',
                   padding='same'))
        model.add(MaxPooling1D(pool_size=3, padding='same'))
        model.add(Dropout(0.3975303416300372))
        model.add(
            Conv1D(filters=7, kernel_size=3, activation='relu',
                   padding='same'))
        model.add(MaxPooling1D(pool_size=3, padding='same'))

        model.add(LSTM(107, return_sequences=True, recurrent_dropout=0.3))
        model.add(Dropout(0.3975303416300372))
        model.add(LSTM(161, return_sequences=True, recurrent_dropout=0.3))
        model.add(Dropout(0.3975303416300372))
        model.add(LSTM(386, return_sequences=False, recurrent_dropout=0.3))
        model.add(Dropout(0.3975303416300372))

        model.add(Dense(90))
        model.add(Dropout(0.3975303416300372))
        model.add(Dense(503))
        model.add(Dropout(0.3975303416300372))
        model.add(Dense(319))
        model.add(Dropout(0.3975303416300372))
        model.add(Dense(151))
        model.add(Dropout(0.3975303416300372))

        model.add(Dense(12, activation='sigmoid'))
    elif index == 1:
        model.add(Dropout(0.1135579744586698))
        model.add(
            Conv1D(filters=4, kernel_size=5, activation='relu',
                   padding='same'))
        model.add(MaxPooling1D(pool_size=4, padding='same'))

        model.add(LSTM(420, return_sequences=True, recurrent_dropout=0.3))
        model.add(Dropout(0.1135579744586698))
        model.add(LSTM(67, return_sequences=True, recurrent_dropout=0.3))
        model.add(Dropout(0.1135579744586698))
        model.add(LSTM(231, return_sequences=False, recurrent_dropout=0.3))
        model.add(Dropout(0.1135579744586698))

        model.add(Dense(112))
        model.add(Dropout(0.1135579744586698))
        model.add(Dense(92))
        model.add(Dropout(0.1135579744586698))

        model.add(Dense(12, activation='sigmoid'))
    elif index == 2:
        model.add(Dropout(0.3211287914743064))
        model.add(
            Conv1D(filters=7, kernel_size=3, activation='relu',
                   padding='same'))
        model.add(MaxPooling1D(pool_size=3, padding='same'))
        model.add(Dropout(0.3211287914743064))
        model.add(
            Conv1D(filters=4, kernel_size=5, activation='relu',
                   padding='same'))
        model.add(MaxPooling1D(pool_size=4, padding='same'))
        model.add(Dropout(0.3211287914743064))
        model.add(
            Conv1D(filters=5, kernel_size=4, activation='relu',
                   padding='same'))
        model.add(MaxPooling1D(pool_size=4, padding='same'))

        model.add(LSTM(404, return_sequences=False, recurrent_dropout=0.3))
        model.add(Dropout(0.3211287914743064))

        model.add(Dense(69))
        model.add(Dropout(0.3211287914743064))
        model.add(Dense(70))
        model.add(Dropout(0.3211287914743064))
        model.add(Dense(171))
        model.add(Dropout(0.3211287914743064))
        model.add(Dense(453))
        model.add(Dropout(0.3211287914743064))

        model.add(Dense(12, activation='sigmoid'))
    elif index == 3:

        model.add(Dropout(0.3584182599371434))
        model.add(
            Conv1D(filters=8, kernel_size=5, activation='relu',
                   padding='same'))
        model.add(MaxPooling1D(pool_size=5, padding='same'))
        model.add(Dropout(0.3584182599371434))
        model.add(
            Conv1D(filters=7, kernel_size=5, activation='relu',
                   padding='same'))
        model.add(MaxPooling1D(pool_size=5, padding='same'))

        model.add(LSTM(438, return_sequences=True, recurrent_dropout=0.3))
        model.add(Dropout(0.3584182599371434))
        model.add(LSTM(500, return_sequences=False, recurrent_dropout=0.3))
        model.add(Dropout(0.3584182599371434))

        model.add(Dense(81))
        model.add(Dropout(0.3584182599371434))
        model.add(Dense(267))
        model.add(Dropout(0.3584182599371434))
        model.add(Dense(388))
        model.add(Dropout(0.3584182599371434))

        model.add(Dense(12, activation='sigmoid'))

    model.compile(loss=masked_loss_function,
                  optimizer='Adam',
                  metrics=[masked_accuracy])

    history = model.fit(X_train,
                        y_train,
                        epochs=epoch,
                        batch_size=128,
                        verbose=2,
                        validation_data=(X_test, y_test),
                        callbacks=[
                            earlyStopping,
                            ModelCheckpoint('result/CNN256_LSTM128_64_2.h5',
                                            monitor='val_masked_accuracy',
                                            mode='max',
                                            save_best_only=True)
                        ])

    plot.plot(history, ("LRCN" + name))

    score = ROC_PR.ROC(model, X_test, y_test, ("LRCN" + name), True)
    return score, ROC_PR.ROC_Score(model, X_train, y_train, limited=False)