コード例 #1
0
mds run so long
tSNE run so long

modified_lle will wrong
hessian_lle will wrong
"""
# reduced_algorithm = [
# 'lle', 'pca', 'KernelPCA', 'FactorAnalysis', 'Isomap'
# , 'ltsa_lle', 'sparse_pca', 'tSNE']

reduced_algorithm = ['Isomap']

# Run the experiment from one dimension to five dimension
for algorithm in reduced_algorithm:
    # Read file LNN_Train_data.xlsx'
    org_data, org_label = LoadData.get_lnn_training_data()

    # Normalize the data
    # normalized_data = preprocessing.normalize(org_data)
    min_max_scaler = preprocessing.MinMaxScaler()
    normalized_data = min_max_scaler.fit_transform(org_data)
    # print(normalized_data)

    # 呼叫不同的降維法去降維, 取特徵直
    # Use different reduced algorithm
    reduced_data = np.array([])
    if algorithm == 'lle':
        reduced_data = ra.lle(normalized_data, dim)

    elif algorithm == 'modified_lle':
        reduced_data = ra.modified_lle(normalized_data, dim)
コード例 #2
0
def train_label_nn(fnn_attribute, algorithm):
    # Declare variables
    nn_weight1, nn_weight2, nn_bias = (0.0 for _ in range(3))
    accuracy = 0.0
    matrix = np.array([])
    record_lnn = LabelNN()

    # This variable is used to store the all accuracy
    all_nn_accuracy = np.array([])

    # Load file LNN_Train_data.xlsx
    org_data, org_label = LoadData.get_lnn_training_data()

    # Reduce dimension and generate train/test data
    reduced_data = reduce_dimension(org_data, org_label, algorithm)

    # normalized_data = preprocessing.normalize(reduced_data)

    # min_max_scaler = preprocessing.MinMaxScaler()
    # normalized_data = min_max_scaler.fit_transform(reduced_data)

    # normalized_data = preprocessing.scale(reduced_data)

    normalized_data = Normalize.normalization(reduced_data)

    # reduced_data = normalization(reduced_data)
    X_train, X_test, y_train, y_test = train_test_split(normalized_data,
                                                        org_label,
                                                        test_size=0.3)

    # print('X_train', X_train)
    # print('X_test', X_test)

    # Label Neural Networks Structure
    weight1_size = 36
    weight2_size = 36
    bias_size = 6

    # Save the one that has the best accuracy
    lnn_input_list = np.array([])
    for test_data, test_label in zip(X_test, y_test):
        lnn_input = get_fnn_output(test_data, fnn_attribute)
        lnn_input_list = np.append(lnn_input_list, lnn_input)
        lnn_input_list = lnn_input_list.reshape(-1, 6)
        # print('label_nn_test(Test)', lnn_input)
    # 產生輸出後再正規化一次
    # lnn_test_input_list = preprocessing.scale(lnn_input_list)

    lnn_test_input_list = Normalize.normalization(lnn_input_list)

    # Save the one that has the best accuracy
    lnn_input_list = np.array([])
    for train_data, train_label in zip(X_train, y_train):
        lnn_input = get_fnn_output(train_data, fnn_attribute)
        lnn_input_list = np.append(lnn_input_list, lnn_input)
        lnn_input_list = lnn_input_list.reshape(-1, 6)
        # print('label_nn_test(Test)', lnn_input)

    # 產生輸出後再正規化一次
    # lnn_train_input_list = preprocessing.scale(lnn_input_list)

    lnn_train_input_list = Normalize.normalization(lnn_input_list)

    for e in lnn_train_input_list:
        print(e)

    # Train the Label NN start
    print('<---Train the Label NN Start--->')
    for _ in range(lnn_random_size):

        weight1 = \
            np.array([np.random.uniform(-1, 1) for _ in range(weight1_size)]).reshape(-1, 6)
        weight2 = \
            np.array([np.random.uniform(-1, 1) for _ in range(weight2_size)]).reshape(-1, 6)
        bias = \
            np.array([np.random.uniform(-1, 1) for _ in range(bias_size)])

        lnn = LabelNN(lnn_input_size, lnn_hidden_size, lnn_output_size,
                      weight1, weight2, bias, lnn_lr)

        # for train_data, train_label in zip(X_train, y_train):
        #     # Calculate the input of the LNN
        #     # By getting the output the FNN1 ~ FNN6
        #     lnn_input = get_fnn_output(train_data, fnn_attribute)
        #
        #     # print('lnn_input', lnn_input)
        #
        #     # print('lnn_input(Train)', lnn_input)
        #     try:
        #         lnn.training_model(lnn_epoch, lnn_input, train_label)
        #
        #     except OverflowError:
        #         print("<---Main.py(Something error had happen in train lnn)--->")
        #         break
        #     except ZeroDivisionError:
        #         print("<---Main.py(Something error had happen in train lnn)--->")
        #         break

        # Test the FNN model,
        # Encoding the label NN
        # Make the confusion matrix
        try:
            lnn.training_model(fnn_epoch, lnn_train_input_list, y_train)
            test_output = lnn.testing_model(lnn_test_input_list)

        except OverflowError:
            print("<---Main.py(Something error had happen in test lnn)--->")
            continue
        except ZeroDivisionError:
            print("<---Main.py(Something error had happen in test lnn)--->")
            continue

        label_pred = LabelNN.label_encode(test_output)
        C_matrix = confusion_matrix(y_test, label_pred)
        C_accuracy = np.sum(C_matrix.diagonal()) / np.sum(C_matrix)

        # Record the single accuracy
        all_nn_accuracy = np.append(all_nn_accuracy, C_accuracy)
        # print(C_matrix)
        # print(C_accuracy)

        if C_accuracy > accuracy:
            accuracy = copy.deepcopy(C_accuracy)
            nn_weight1 = copy.deepcopy(lnn.weight1)
            nn_weight2 = copy.deepcopy(lnn.weight2)
            nn_bias = copy.deepcopy(lnn.bias)
            matrix = copy.deepcopy(C_matrix)
            record_lnn = copy.deepcopy(lnn)
        """
        Every error trend graph will output
        Output the Error Plot to observe trend
        """
        # rel_path = './Data/Graph/' + str(i) + '_LNN_error_trend.png'
        # abs_path = os.path.join(os.path.dirname(__file__), rel_path)
        # ErrorPlot.error_trend(
        #     str(i) + '_LNN_error_trend', len(lnn.error_list), lnn.error_list)

    print('<---Train the Label NN Successfully--->')
    print('<----------------------------------------------->')

    # # Create a folder
    # org_path = './Data/Graph/'
    # org_path = makedir(org_path, dimension_reduce_algorithm)

    # print('3_目錄', os.getcwd())

    abs_path = os.getcwd()
    # Choose the best LNN to Plot error trend
    ErrorPlot.mul_error_trend('Best_LNN_error_trend',
                              len(record_lnn.error_list),
                              record_lnn.error_list, abs_path)

    # Choose the best Accuracy to Plot
    # rel_path = org_path + 'Accuracy vs LNN.png'
    # abs_path = os.path.join(os.path.dirname(__file__), rel_path)

    abs_path = os.getcwd() + '\\Accuracy vs LNN.png'
    AccuracyPlot.build_accuracy_plot(
        'Accuracy vs LNN',
        np.array([i for i in range(1,
                                   len(all_nn_accuracy) + 1, 1)]),
        all_nn_accuracy, abs_path)

    return nn_weight1, nn_weight2, nn_bias, accuracy, matrix