Esempio n. 1
0
def mlp(inputs_train, outputs_train, inputs_test, outputs_test, loss):
    """
    Fully connected neural network.
    @param loss: 'categorical_crossentropy' or 'mean_squared_error'
    """
    # Split to train-set and validation-set
    split_at = len(inputs_train) - len(inputs_train) * 2 // 10
    (inputs_train, inputs_validation) = \
        (inputs_train[:split_at], inputs_train[split_at:])
    (outputs_train, outputs_validation) = \
        (outputs_train[:split_at], outputs_train[split_at:])
    # Build MLP model
    model = Sequential()
    model.add(Dense(64, activation='relu', input_dim=inputs_train.shape[1]))
    model.add(Dropout(0.5))
    model.add(Dense(64, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(outputs_train.shape[1], activation='softmax'))
    print(model.summary())
    # compile
    model.compile(loss=loss, optimizer='adam', metrics=['accuracy'])
    # train
    if loss == 'categorical_crossentropy':
        early_stopping = EarlyStopping(min_delta=0.001,
                                       patience=5,
                                       restore_best_weights=True)
    elif loss == 'mean_squared_error':
        early_stopping = EarlyStopping(min_delta=0.0003,
                                       patience=5,
                                       restore_best_weights=True)
    else:
        raise ValueError(
            "loss should be 'categorical_crossentropy' or 'mean_squared_error'."
        )
    model.fit(inputs_train,
              outputs_train,
              epochs=100,
              batch_size=256,
              validation_data=(inputs_validation, outputs_validation),
              callbacks=[early_stopping])
    # evaluate
    outputs_test_pred = np.asarray(model.predict(inputs_test))
    acc_eval = accuracy(outputs_test, outputs_test_pred)
    fscore_eval = fscore(outputs_test, outputs_test_pred)
    coef_eval = coef(outputs_test, outputs_test_pred)
    print("Evaluation: acc - %.4f - fscore: %.4f - coef: %.4f - pvalue: %.4f" %
          (acc_eval, fscore_eval, coef_eval[0], coef_eval[1]))
    # return model
    return model
Esempio n. 2
0
def verify_model(model_file_name, inputs_test, outputs_test):
    # load model
    model_file_path = os.path.join(os.path.pardir, "models",
                                   model_file_name + ".h5")
    model = load_model(model_file_path)
    print(model_file_name)
    # model structure
    print(model.summary())
    # model evaluation
    outputs_test_pred = np.asarray(model.predict(inputs_test))
    acc_eval = accuracy(outputs_test, outputs_test_pred)
    fscore_eval = fscore(outputs_test, outputs_test_pred)
    coef_eval = coef(outputs_test, outputs_test_pred)
    print("Evaluation: acc - %.4f - fscore: %.4f - coef: %.4f - pvalue: %.4f" %
          (acc_eval, fscore_eval, coef_eval[0], coef_eval[1]))
Esempio n. 3
0
def rnn(inputs_train, outputs_train, inputs_test, outputs_test, loss,
        train_embedding):
    """
    Recurrent neural network.
    @param loss: 'classification' or 'regression'
    @param train_embedding: 0 - initialize with word_embedding_matrix, trainable=False
                            1 - initialize with word_embedding_matrix, trainable=True
                            2 - initialize with random matrix, trainable=True
    """
    # Load word-embedding matrix
    word_embedding_matrix_file_path = os.path.join(os.path.pardir, "data",
                                                   "word-embedding_matrix")
    with open(word_embedding_matrix_file_path,
              'rb') as word_embedding_matrix_file:
        word_embedding_matrix = np.load(word_embedding_matrix_file)
    # Split to train-set and validation-set
    split_at = len(inputs_train) - len(inputs_train) * 2 // 10
    (inputs_train, inputs_validation) = \
        (inputs_train[:split_at], inputs_train[split_at:])
    (outputs_train, outputs_validation) = \
        (outputs_train[:split_at], outputs_train[split_at:])
    # Build RNN model
    if train_embedding == 0:
        embedding_layer = Embedding(word_embedding_matrix.shape[0],
                                    word_embedding_matrix.shape[1],
                                    weights=[word_embedding_matrix],
                                    input_length=inputs_train.shape[1],
                                    trainable=False)
    elif train_embedding == 1:
        embedding_layer = Embedding(word_embedding_matrix.shape[0],
                                    word_embedding_matrix.shape[1],
                                    weights=[word_embedding_matrix],
                                    input_length=inputs_train.shape[1],
                                    trainable=True)
    elif train_embedding == 2:
        embedding_layer = Embedding(word_embedding_matrix.shape[0],
                                    word_embedding_matrix.shape[1],
                                    input_length=inputs_train.shape[1],
                                    trainable=True)
    else:
        raise ValueError("train_embedding should be 0 or 1 or 2.")
    model = Sequential()
    model.add(embedding_layer)
    model.add(SimpleRNN(128, unroll=True))
    model.add(Dropout(0.5))
    model.add(Dense(outputs_train.shape[1], activation='softmax'))
    print(model.summary())
    # compile
    model.compile(loss=loss, optimizer='adam', metrics=['accuracy'])
    # train
    if loss == 'categorical_crossentropy':
        early_stopping = EarlyStopping(min_delta=0.005,
                                       patience=3,
                                       restore_best_weights=True)
    elif loss == 'mean_squared_error':
        early_stopping = EarlyStopping(min_delta=0.0005,
                                       patience=3,
                                       restore_best_weights=True)
    else:
        raise ValueError(
            "loss should be 'categorical_crossentropy' or 'mean_squared_error'."
        )
    model.fit(inputs_train,
              outputs_train,
              epochs=100,
              batch_size=128,
              validation_data=(inputs_validation, outputs_validation),
              callbacks=[early_stopping])
    # evaluate
    outputs_test_pred = np.asarray(model.predict(inputs_test))
    acc_eval = accuracy(outputs_test, outputs_test_pred)
    fscore_eval = fscore(outputs_test, outputs_test_pred)
    coef_eval = coef(outputs_test, outputs_test_pred)
    print("Evaluation: acc - %.4f - fscore: %.4f - coef: %.4f - pvalue: %.4f" %
          (acc_eval, fscore_eval, coef_eval[0], coef_eval[1]))
    # return model
    return model
Esempio n. 4
0
def cnn(inputs_train, outputs_train, inputs_test, outputs_test, loss,
        train_embedding):
    """
    Convolutional neural network for sentence classification.
    See "Kim, Y. (2014). Convolutional Neural Networks for Sentence Classification. Proceedings of the 2014 Conference on Empirical Methods in Natural Language Processing (EMNLP 2014), 1746–1751." for details.
    @param loss: 'categorical_crossentropy' or 'mean_squared_error'
    @param train_embedding: 0 - initialize with word_embedding_matrix, trainable=False
                            1 - initialize with word_embedding_matrix, trainable=True
                            2 - initialize with random matrix, trainable=True
    """
    # Load word-embedding matrix
    word_embedding_matrix_file_path = os.path.join(os.path.pardir, "data",
                                                   "word-embedding_matrix")
    with open(word_embedding_matrix_file_path,
              'rb') as word_embedding_matrix_file:
        word_embedding_matrix = np.load(word_embedding_matrix_file)
    # Split to train-set and validation-set
    split_at = len(inputs_train) - len(inputs_train) * 2 // 10
    (inputs_train, inputs_validation) = \
        (inputs_train[:split_at], inputs_train[split_at:])
    (outputs_train, outputs_validation) = \
        (outputs_train[:split_at], outputs_train[split_at:])
    # Build CNN model
    if train_embedding == 0:
        embedding_layer = Embedding(word_embedding_matrix.shape[0],
                                    word_embedding_matrix.shape[1],
                                    weights=[word_embedding_matrix],
                                    input_length=inputs_train.shape[1],
                                    trainable=False)
    elif train_embedding == 1:
        embedding_layer = Embedding(word_embedding_matrix.shape[0],
                                    word_embedding_matrix.shape[1],
                                    weights=[word_embedding_matrix],
                                    input_length=inputs_train.shape[1],
                                    trainable=True)
    elif train_embedding == 2:
        embedding_layer = Embedding(word_embedding_matrix.shape[0],
                                    word_embedding_matrix.shape[1],
                                    input_length=inputs_train.shape[1],
                                    trainable=True)
    else:
        raise ValueError("train_embedding should be 0 or 1 or 2.")
    model_input = Input(shape=(inputs_train.shape[1], ))
    embedding = embedding_layer(model_input)
    conv_blocks = []
    for kernel_size in range(2, 8):
        for _ in range(2):  # 2 filters for each kernel_size
            conv = Conv1D(4, kernel_size, activation="relu")(embedding)
            conv = MaxPooling1D(3)(conv)
            conv = Dropout(0.25)(conv)
            conv = Flatten()(conv)
            conv_blocks.append(conv)
    x = Concatenate()(conv_blocks)
    x = Dense(128, activation="relu")(x)
    x = Dropout(0.5)(x)
    model_output = Dense(outputs_train.shape[1], activation="softmax")(x)
    model = Model(model_input, model_output)
    print(model.summary())
    # compile
    model.compile(loss=loss, optimizer='adam', metrics=['accuracy'])
    # train
    if loss == 'categorical_crossentropy':
        early_stopping = EarlyStopping(min_delta=0.003,
                                       patience=5,
                                       restore_best_weights=True)
    elif loss == 'mean_squared_error':
        early_stopping = EarlyStopping(min_delta=0.0003,
                                       patience=5,
                                       restore_best_weights=True)
    else:
        raise ValueError(
            "loss should be 'categorical_crossentropy' or 'mean_squared_error'."
        )
    model.fit(inputs_train,
              outputs_train,
              epochs=100,
              batch_size=128,
              validation_data=(inputs_validation, outputs_validation),
              callbacks=[early_stopping])
    # evaluate
    outputs_test_pred = np.asarray(model.predict(inputs_test))
    acc_eval = accuracy(outputs_test, outputs_test_pred)
    fscore_eval = fscore(outputs_test, outputs_test_pred)
    coef_eval = coef(outputs_test, outputs_test_pred)
    print("Evaluation: acc - %.4f - fscore: %.4f - coef: %.4f - pvalue: %.4f" %
          (acc_eval, fscore_eval, coef_eval[0], coef_eval[1]))
    # return model
    return model