예제 #1
0
    def predict_pil(self, train_X, train_y, test_X, test_y, layer):
        self.train_pil(train_X, train_y)
        train_y_true = tools.to_categorical(train_y)
        test_y_true = tools.to_categorical(test_y)
        for i in range(self.pil_layers):
            train_X = self.activeFunction(train_X.dot(self.pil_weight[i]), self.acFunc)
            test_X = self.activeFunction(test_X.dot(self.pil_weight[i]), self.acFunc)
        train_y_predict = self.get_onehot(train_X.dot(self.pil_weight[-1]))
        test_y_predict = self.get_onehot(test_X.dot(self.pil_weight[-1]))
        self.pil_weight = []

        train_acc = accuracy_score(train_y_true, train_y_predict) * 100
        test_acc = accuracy_score(test_y_true, test_y_predict) * 100
        self.pil_train_acc.append(train_acc)
        self.pil_test_acc.append(test_acc)
        print("[INFO]+==================the {} layer pil===================".format(layer + 1))
        print("PIL classifier layer {}:".format(self.pil_layers))
        print("PIL Train accuracy: {} | Test accuracy: {}".format(train_acc, test_acc))
        test_recall_score = recall_score(train_y_true, train_y_predict, average='micro') * 100
        test_f1_score = f1_score(train_y_true, train_y_predict, average='micro') * 100
        test_classification_report = classification_report(train_y_true, train_y_predict)
        print("PIL test recall: {} and f1_score: {}".format(test_recall_score, test_f1_score))
        # print(self.test_classification_report)

        cnf_matrix = confusion_matrix(np.argmax(test_y_predict, axis=1), np.argmax(test_y_true, axis=1))
예제 #2
0
파일: pilae.py 프로젝트: sibofeng/PILAE
    def predict_pil(self, X_train, y_train, X_test, y_test, layer_th=None):
        train_X = X_train
        train_y = y_train
        test_X = X_test
        test_y = y_test

        # 调用这个predict函数 在predict之前训练
        # 这样的写法有点不符合规范 但是也还说的过去
        self.train_pil(train_X, train_y)
        train_y_true = tools.to_categorical(train_y)
        test_y_true = tools.to_categorical(test_y)
        # 逐层读取保存好的权重 得到分类结果
        for i in range(self.pil_layers):
            train_X = self.activeFunction(train_X.dot(self.pil_weight[i]),
                                          self.acFunc)
            test_X = self.activeFunction(test_X.dot(self.pil_weight[i]),
                                         self.acFunc)
            train_loss = self.compute_loss(train_X)
            test_loss = self.compute_loss(test_X)
            self.train_loss.append(train_loss)
            self.test_loss.append(test_loss)
        train_y_predict = self.get_onehot(train_X.dot(self.pil_weight[-1]))
        test_y_predict = self.get_onehot(test_X.dot(self.pil_weight[-1]))
        self.pil_weight = []

        train_acc = accuracy_score(train_y_true, train_y_predict) * 100
        test_acc = accuracy_score(test_y_true, test_y_predict) * 100
        # 将结果保存到self.pil_train_acc中
        self.pil_train_acc.append(train_acc)
        self.pil_test_acc.append(test_acc)
        print("PIL classifier layer {}:".format(self.pil_layers))
        print("PIL Train accuracy: {}% | Test accuracy: {}%".format(
            train_acc, test_acc))
예제 #3
0
 def random_shuffle(self, X, y):
     m, n = X.shape
     index = [i for i in range(m)]
     random.shuffle(index)
     X = X[index]
     y = y[index]
     y = tools.to_categorical(y)
     return X, y
예제 #4
0
 def train_pil(self, train_X, train_y):
     X = train_X
     y = tools.to_categorical(train_y)
     for i in range(self.pil_layers):
         pinvX = np.linalg.pinv(X)
         self.pil_weight.append(pinvX)
         # pinvX = pinvX[:, 0: int(self.pil_p)]
         tempH = X.dot(pinvX)
         X = self.activeFunction(tempH, self.acFunc)
     invH = np.linalg.inv(X.T.dot(X) + np.eye(X.shape[1]) * self.pil_k)  # recompute W_d
     pred_W = invH.dot(X.T).dot(y)
     self.pil_weight.append(pred_W)
예제 #5
0
파일: pilae.py 프로젝트: sibofeng/PILAE
 def train_pil(self, train_X, train_y):
     X = train_X
     y = tools.to_categorical(train_y)  # 转换成one_hot编码
     # 根据self.pil_layers设置的层数 使用for循环进行逐层训练
     for i in range(self.pil_layers):
         pinvX = np.linalg.pinv(X)
         pinvX = pinvX[:, 0:int(self.pil_p[i])]
         self.pil_weight.append(pinvX)
         tempH = X.dot(pinvX)
         X = self.activeFunction(tempH, self.acFunc)
     invH = np.linalg.inv(X.T.dot(X) + np.eye(X.shape[1]) * self.pil_k)
     pred_W = invH.dot(X.T).dot(y)
     self.pil_weight.append(pred_W)
예제 #6
0
 def PIL_classifier(self, train_X, train_y, test_X, test_y):
     train_y = tools.to_categorical(train_y)
     test_y = tools.to_categorical(test_y)
     self.PIL_fit(train_X, train_y)
     predict_train = self.PIL_feedforward(train_X)
     predict_test = self.PIL_feedforward(test_X)
     train_predict = self.deal_onehot(predict_train)
     test_predict = self.deal_onehot(predict_test)
     self.train_acc = accuracy_score(train_predict, train_y) * 100
     self.test_acc = accuracy_score(test_predict, test_y) * 100
     print("==================pil=================")
     print("PIL classifier layer {}:".format(self.pil_layer))
     print("PIL Train accuracy: {} | Test accuracy: {}".format(
         self.train_acc, self.test_acc))
     self.test_recall_score = recall_score(
         test_y, test_predict, average='micro') * 100
     self.test_f1_score = f1_score(test_y, test_predict,
                                   average='micro') * 100
     self.test_classification_report = classification_report(
         test_y, test_predict)
     print("PIL test recall: {} and f1_score: {}".format(
         self.test_recall_score, self.test_f1_score))
     print(self.test_classification_report)