示例#1
0
def train_local_fnn(nn, algorithm):

    # Declare variables
    nn_mean, nn_stddev, nn_weight = (0.0 for _ in range(3))
    accuracy = 0.0
    matrix = np.array([])
    record_fnn = FNN()
    loss_list = np.array([])

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

    # Load file FNN_Train_data_' + str(num) + '.xlsx
    org_data, org_label = LoadData.get_method1_fnn_train(nn)
    org_label = np.array([1 if element == nn else 0 for element in org_label])

    # Reduce dimension and generate train/test data
    reduced_data = reduce_dimension(org_data, org_label, algorithm)
    # normalized_data = preprocessing.normalize(reduced_data)
    # reduced_data = normalization(reduced_data)

    # 正規化 1
    # min_max_scaler = preprocessing.MinMaxScaler()
    # normalized_data = min_max_scaler.fit_transform(reduced_data)

    # 正規化 2
    # normalized_data = preprocessing.scale(reduced_data)

    # 正規化 3
    normalized_data = Normalize.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.shape)
    # print(y_train, y_train.shape)

    # Train the FNN
    print('<---Train the FNN' + str(nn) + ' Start--->')
    for i in range(fnn_random_size):

        # Random Generate the mean, standard deviation
        mean = np.array(
            [np.random.uniform(-1, 1) for _ in range(fnn_membership_size)])
        stddev = np.array(
            [np.random.uniform(0, 1) for _ in range(fnn_membership_size)])
        weight = np.array(
            [np.random.uniform(-1, 1) for _ in range(fnn_rule_size)])
        """
        # Generate FNN object to train
        # para1 -> fnn input layer size
        # para2 -> fnn membership layer size
        # para3 -> fnn rule layer size
        # para4 -> fnn output layer size
        # para5 -> random mean values
        # para6 -> random stddev values
        # para7 -> random weight values
        # para8 -> nn label type
        """
        fnn = FNN(fnn_input_size, fnn_membership_size, fnn_rule_size,
                  fnn_output_size, mean, stddev, weight, fnn_lr, 1)
        fnn.training_model(fnn_epoch, X_train, y_train)

        # Test the FNN model, save the one that has the best accuracy
        test_output = fnn.testing_model(X_test)

        label_pred = label_encode(nn, test_output)
        # print(y_test.shape)
        # print(label_pred.shape)
        # print(y_test)
        # print(label_pred)

        C_matrix = confusion_matrix(y_test, label_pred)
        C_accuracy = np.sum(C_matrix.diagonal()) / np.sum(C_matrix)
        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_mean = copy.deepcopy(fnn.mean)
            nn_stddev = copy.deepcopy(fnn.stddev)
            nn_weight = copy.deepcopy(fnn.weight)
            matrix = copy.deepcopy(C_matrix)
            record_fnn = copy.deepcopy(fnn)
            loss_list = copy.deepcopy(fnn.loss_list)
        """
        Every error trend graph will output
        Output the Error Plot to observe trend
        """
        # rel_path = './Data/Graph/' + str(i) + '_FNN_' + str(nn) + '_error_trend.png'
        # abs_path = os.path.join(os.path.dirname(__file__), rel_path)
        # ErrorPlot.error_trend(
        #     str(i) + '_FNN_' + str(nn) + '_error_trend', len(fnn.error_list), fnn.error_list, abs_path)

    print('<---Train the FNN' + str(nn) + ' Successfully--->')
    print('<----------------------------------------------->')

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

    # First Time, you need to create a folder
    if nn == 1:
        org_path = './Data/Graph/'
        makedir(org_path, algorithm)
    # else:
    #     os.chdir('./Data/Graph/' + dimension_reduce_algorithm)
    # print('2_目錄:', os.getcwd())

    # Choose the best FNN to Plot error trend
    # rel_path = org_path + 'Best_FNN_' + str(nn) + '_error_trend.png'
    # abs_path = os.path.join(os.path.dirname(__file__), rel_path)
    abs_path = os.getcwd() + '\\Best_FNN_' + str(nn) + '_error_trend.png'
    # print('ErrorPlot', abs_path)
    ErrorPlot.error_trend('Best_FNN_' + str(nn) + '_error_trend',
                          len(record_fnn.error_list), record_fnn.error_list,
                          abs_path)

    abs_path = os.getcwd() + '\\Best_FNN_' + str(nn) + '_loss_trend.png'
    # Choose the best FNN to Plot loss on every epoch
    # ErrorPlot.loss_trend(
    #     'Best_FNN_' + str(nn) + '_loss_trend', len(loss_list), loss_list, abs_path)

    # Choose the best Accuracy to Plot
    # rel_path = org_path + 'Accuracy vs FNN' + str(nn) + '.png'
    # abs_path = os.path.join(os.path.dirname(__file__), rel_path)
    abs_path = os.getcwd() + '\\Accuracy vs FNN' + str(nn) + '.png'
    # print('AccuracyPlot', abs_path)
    AccuracyPlot.build_accuracy_plot(
        'Accuracy vs FNN' + str(nn),
        np.array([i for i in range(1,
                                   len(all_nn_accuracy) + 1, 1)]),
        all_nn_accuracy, abs_path)

    return nn_mean, nn_stddev, nn_weight, accuracy, matrix
示例#2
0
# Show Method List
print('<---1. tSNE--->')
print('<---2. PCA--->')
print('<---3. Isomap--->')
reduced_algorithm = input('<---Please Choose the dimension reduced algorithm--->: ')

# Best -> tSNE
# reduced_algorithm = ['tSNE']
dimension = 3

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

        reduced_data = np.array([])
        # Dimension Reduce
        if algorithm == "NCA":
            reduced_data = ra.nca(org_data, org_label, dimension)
        elif algorithm == "tSNE" or "1":
            reduced_data = ra.tsne(org_data, dimension)
        elif algorithm == "LLE":
            reduced_data = ra.lle(org_data, dimension)
        elif algorithm == "sparse_pca":
            reduced_data = ra.sparse_pca(org_data, dimension)
        elif algorithm == "LFDA":
            reduced_data = ra.lfda(org_data, org_label, dimension)
        elif algorithm == "PCA" or "2":
            reduced_data = ra.pca(org_data, dimension)