def train_perceptron(self, n_epochs):
        '''Trains the parser by running the averaged perceptron algorithm for n_epochs.''' 
        self.weights = np.zeros(self.features.n_feats)
        total = np.zeros(self.features.n_feats)
        for epoch in range(n_epochs):
            print "Epoch {0}".format(epoch+1)
            n_mistakes = 0
            n_tokens = 0
            n_instances = 0
            for instance in self.reader.train_instances:
                feats = self.features.create_features(instance)
                scores = self.features.compute_scores(feats, self.weights)
                if self.projective:
                    heads_pred = self.decoder.parse_proj(scores)
                else:
                    heads_pred = self.decoder.parse_nonproj(scores)

                for m in range(np.size(heads_pred)):
                    if heads_pred[m] != instance.heads[m]: # mistake
                        for f in feats[instance.heads[m]][m]: 
                            if f < 0:
                                continue
                            self.weights[f] += 1.0
                        for f in feats[heads_pred[m]][m]: 
                            if f < 0:
                                continue
                            self.weights[f] -= 1.0
                        n_mistakes += 1
                    n_tokens += 1
                n_instances += 1
            print "Training accuracy: {0}".format(np.double(n_tokens - n_mistakes)/np.double(n_tokens))
            total += self.weights   

        self.weights = total / np.double(n_epochs)
dataset/ETHPy150Open LxMLS/lxmls-toolkit/lxmls/parsing/dependency_parser.py/DependencyParser.train_perceptron/original