FukuML/PocketPLA.py
def train(self):

        '''
        Train Pocket Perceptron Learning Algorithm
        From f(x) = WX
        Find best h(x) = WX similar to f(x)
        Output W
        '''

        if (self.status != 'init'):
            print("Please load train data and init W first.")
            return self.W

        self.status = 'train'

        new_W = self.W

        self.temp_avg_error = self.calculate_avg_error(self.train_X, self.train_Y, new_W)

        for _ in range(self.updates):
            if (self.loop_mode is 'naive_cycle'):
                data_check_order = range(self.data_num)
            elif (self.loop_mode is 'random'):
                data_check_order = range(self.data_num)
                data_check_order = random.sample(data_check_order, self.data_num)
            else:
                data_check_order = range(self.data_num)
                data_check_order = random.sample(data_check_order, self.data_num)
            for i in data_check_order:

                if self.error_function(self.score_function(self.train_X[i], new_W), self.train_Y[i]):
                    self.tune_times += 1
                    new_W = new_W + self.step_alpha * (self.train_Y[i] * self.train_X[i])
                    new_avg_error = self.calculate_avg_error(self.train_X, self.train_Y, new_W)
                    if new_avg_error < self.temp_avg_error:
                        self.put_in_pocket_times += 1
                        self.temp_avg_error = new_avg_error
                        self.W = new_W
                    break

        return self.W
def train(self):

        '''
        Train Pocket Perceptron Learning Algorithm
        From f(x) = WX
        Find best h(x) = WX similar to f(x)
        Output W
        '''

        if (self.status != 'init'):
            print("Please load train data and init W first.")
            return self.W

        self.status = 'train'

        new_W = self.W

        self.temp_avg_error = self.calculate_avg_error(self.train_X, self.train_Y, new_W)

        for _ in range(self.updates):
            if (self.loop_mode is 'naive_cycle'):
                data_check_order = range(self.data_num)
            elif (self.loop_mode is 'random'):
                data_check_order = range(self.data_num)
                data_check_order = random.sample(data_check_order, self.data_num)
            else:
                data_check_order = range(self.data_num)
                data_check_order = random.sample(data_check_order, self.data_num)
            for i in data_check_order:

                if self.error_function(self.score_function(self.train_X[i], new_W), self.train_Y[i]):
                    self.tune_times += 1
                    new_W = new_W + self.step_alpha * (self.train_Y[i] * self.train_X[i])
                    new_avg_error = self.calculate_avg_error(self.train_X, self.train_Y, new_W)
                    if new_avg_error < self.temp_avg_error:
                        self.put_in_pocket_times += 1
                        self.temp_avg_error = new_avg_error
                        self.W = new_W
                    break

        return self.W
def train ( self ) : if ( self . status != 'init' ) : print ( "Please load train data and init W first." ) return self . W self . status = 'train' new_W = self . W self . temp_avg_error = self . calculate_avg_error ( self . train_X , self . train_Y , new_W ) for _ in range ( self . updates ) : if ( self . loop_mode is 'naive_cycle' ) : data_check_order = range ( self . data_num ) elif ( self . loop_mode is 'random' ) : data_check_order = range ( self . data_num ) data_check_order = random . sample ( data_check_order , self . data_num ) else : data_check_order = range ( self . data_num ) data_check_order = random . sample ( data_check_order , self . data_num ) for i in data_check_order : if self . error_function ( self . score_function ( self . train_X [ i ] , new_W ) , self . train_Y [ i ] ) : self . tune_times += 1 new_W = new_W + self . step_alpha * ( self . train_Y [ i ] * self . train_X [ i ] ) new_avg_error = self . calculate_avg_error ( self . train_X , self . train_Y , new_W ) if new_avg_error < self . temp_avg_error : self . put_in_pocket_times += 1 self . temp_avg_error = new_avg_error self . W = new_W break return self . W
Train Pocket Perceptron Learning Algorithm
        From f(x) = WX
        Find best h(x) = WX similar to f(x)
        Output W
Train Pocket Perceptron Learning Algorithm From f ( x ) = WX Find best h ( x ) = WX similar to f ( x ) Output W