def transform(self, test_data, mode=None): if self.norm == True: t_data = self.normalizer.norm(test_data) else: t_data = test_data if mode == None: mode = self.mode if mode == 'v1': SCCI_enhance.set_alpha(self.alpha) similarity_result = SCCI_enhance.get_MD( t_data, self.node_mean, self.node_sigma) pred_table = np.zeros([len(test_data), self.num_classes]) for i, sim_result in enumerate(similarity_result): pred_cls = sim_result.argmax() pred = self.node_to_class_dict[pred_cls] pred_table[i][pred] = 1 else: boundary = 0.0 SCCI_enhance.set_alpha(self.alpha) node_output = SCCI_enhance.get_MD( t_data, self.node_mean, self.node_sigma) node_output = np.append(node_output, np.ones( (np.size(node_output, 0), 1)), axis=1) target_output = np.matmul(node_output, self.weights) pred_table = self.matrix2Binary(target_output, boundary) return pred_table
def train(self, train_data, train_label): self.data = self.preprocess_data(train_data, train_label) self.tar_matrix = self.target2matrix(train_label, self.num_classes) self.get_hidden_nodes(self.data, train_label) SCCI_enhance.set_alpha(self.alpha) node_output = SCCI_enhance.get_MD( self.data, self.node_mean, self.node_sigma) self.node_output = np.append(node_output, np.ones( (np.size(node_output, 0), 1), dtype=float), axis=1) if self.mode == 'v1': pass elif self.mode == 'v2': self.get_network_weights(self.node_output, self.tar_matrix) class_output = np.matmul(self.node_output, self.weights) self.error.append(self.error_estimate( class_output, self.tar_matrix)) elif self.mode == 'v4': self.get_network_weights(self.node_output, self.tar_matrix) class_output = np.matmul(self.node_output, self.weights) self.error.append(self.error_estimate( class_output, self.tar_matrix)) # Check train result pred_table = self.matrix2Binary(class_output) self.train_target = self.target2matrix( train_label, self.num_classes, neg_note=0) self.error_train.append( 1 - accuracy_score(pred_table, self.train_target))
def update_once(self): class_output = np.matmul(self.node_output, self.weights) self.update(self.data, self.tar_matrix, self.node_output, class_output) node_output = SCCI_enhance.get_MD( self.data, self.node_mean, self.node_sigma) self.node_output = np.append(node_output, np.ones( (np.size(node_output, 0), 1), dtype=float), axis=1) self.get_network_weights(self.node_output, self.tar_matrix) class_output = np.matmul(self.node_output, self.weights) pred_table = self.matrix2Binary(class_output) self.error_train.append( 1 - accuracy_score(pred_table, self.train_target)) self.error.append(self.error_estimate(class_output, self.tar_matrix))
def train(self, train_data, train_label): data = self.preprocess_data(train_data, train_label) tar_matrix = self.target2matrix(train_label, self.num_classes) self.get_hidden_nodes(data, train_label) SCCI_enhance.set_alpha(self.alpha) node_output = SCCI_enhance.get_MD( data, self.node_mean, self.node_sigma) node_output = np.append(node_output, np.ones( (np.size(node_output, 0), 1), dtype=float), axis=1) if self.mode == 'v1': pass elif self.mode == 'v2': self.get_network_weights(node_output, tar_matrix) class_output = np.matmul(node_output, self.weights) self.error.append(self.error_estimate(class_output, tar_matrix)) elif self.mode == 'v4': self.get_network_weights(node_output, tar_matrix) class_output = np.matmul(node_output, self.weights) self.error.append(self.error_estimate(class_output, tar_matrix)) # Check train result boundary = 0.0 pred_table = self.matrix2Binary(class_output, boundary) train_target = self.target2matrix( train_label, self.num_classes, neg_note=0) self.error_train.append(1-accuracy_score(pred_table, train_target)) flag = 0 end_flag = 1 count = 0 while flag < end_flag: self.update(data, tar_matrix, node_output, class_output) node_output = SCCI_enhance.get_MD( data, self.node_mean, self.node_sigma) node_output = np.append(node_output, np.ones( (np.size(node_output, 0), 1), dtype=float), axis=1) self.get_network_weights(node_output, tar_matrix) class_output = np.matmul(node_output, self.weights) pred_table = self.matrix2Binary(class_output, boundary) self.error_train.append( 1-accuracy_score(pred_table, train_target)) self.error.append(self.error_estimate( class_output, tar_matrix)) # if abs(self.error[-2] - self.error[-1])/self.error[-1] < 0.01: # flag = end_flag if self.error_train[-1] == 0.0: flag = end_flag print("Error is equal to ZERO!") elif ((self.error_train[-2]-self.error_train[-1])/self.error_train[-1]) < 0.01: if count > 3: flag = end_flag else: count += 1 elif self.epoch == self.epoch_max: flag = end_flag print("Epochs reach Maximun times.") else: count = 0 self.epoch += 1
def train(self, train_data, train_label): self.num_classes = len(train_label[0]) data = train_data tar_matrix = np.zeros(train_label.shape) for i, row in enumerate(train_label): for j, col in enumerate(row): if col == 1: tar_matrix[i][j] = 1.0 else: tar_matrix[i][j] = -1.0 self.get_hidden_nodes(data, train_label) SCCI_enhance.set_alpha(self.alpha) node_output = SCCI_enhance.get_MD( data, self.node_mean, self.node_sigma) node_output = np.append(node_output, np.ones( (np.size(node_output, 0), 1), dtype=float), axis=1) if self.mode == 'v1': pass elif self.mode == 'v2': self.get_network_weights(node_output, tar_matrix) class_output = np.matmul(node_output, self.weights) self.error.append(self.error_estimate(class_output, tar_matrix)) elif self.mode == 'v4': self.get_network_weights(node_output, tar_matrix) class_output = np.matmul(node_output, self.weights) self.error.append(self.error_estimate(class_output, tar_matrix)) # Check train result boundary = 0.0 pred_table = self.matrix2Binary(class_output, boundary) train_target = train_label self.error_train.append(1-accuracy_score(pred_table, train_target)) flag = 0 end_flag = 1 count = 0 while flag < end_flag: self.update(data, tar_matrix, node_output, class_output) node_output = SCCI_enhance.get_MD( data, self.node_mean, self.node_sigma) node_output = np.append(node_output, np.ones( (np.size(node_output, 0), 1), dtype=float), axis=1) self.get_network_weights(node_output, tar_matrix) class_output = np.matmul(node_output, self.weights) pred_table = self.matrix2Binary(class_output, boundary) self.error_train.append( 1-accuracy_score(pred_table, train_target)) self.error.append(self.error_estimate( class_output, tar_matrix)) # Early stopping if self.error_train[-1] == 0.0: flag = end_flag print("Error is equal to ZERO!") elif ((self.error_train[-2]-self.error_train[-1])/self.error_train[-1]) < 0.005: if count > 2: flag = end_flag else: count += 1 elif self.epoch == self.epoch_max: flag = end_flag print("Epochs reach Maximun times.") self.epoch += 1
sigma_init = 0.2 alpha = math.sqrt(2) beta = 2 learning_rate = 1 lambda_i = 0 tra_label = RBF_ISCC.target2matrix(Y_train, 2, neg_note=0) rbf_iscc = RBF_ISCC_update_once(threshold=threshold, sigma_init=sigma_init, alpha=alpha, beta=beta, lambda_i=lambda_i, learning_rate=learning_rate, mode='v4', norm=False) rbf_iscc.train(X_train, Y_train) w = rbf_iscc.weights SCCI_enhance.set_alpha(alpha) node_output = SCCI_enhance.get_MD( X_train, rbf_iscc.node_mean, rbf_iscc.node_sigma) node_output = np.append(node_output, np.ones( (np.size(node_output, 0), 1), dtype=float), axis=1) cls_output = np.matmul(node_output, rbf_iscc.weights) pred_label_tr = rbf_iscc.transform(X_train) score_tr = performance_measure(pred_label_tr, tra_label) error_tr = [1 - score_tr["AACC"]] flag = 0 end_flag = 1 if error_tr[-1] == 0: flag = end_flag while flag < end_flag: rbf_iscc.update_once() pred_label_tr = rbf_iscc.transform(X_train)