mirror of
				https://github.com/explosion/spaCy.git
				synced 2025-11-04 09:57:26 +03:00 
			
		
		
		
	Clean up FTRL parsing stuff.
This commit is contained in:
		
							parent
							
								
									2611ac2a89
								
							
						
					
					
						commit
						a46933a8fe
					
				| 
						 | 
					@ -52,7 +52,7 @@ from ._parse_features cimport fill_context
 | 
				
			||||||
from .stateclass cimport StateClass
 | 
					from .stateclass cimport StateClass
 | 
				
			||||||
from ._state cimport StateC
 | 
					from ._state cimport StateC
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					USE_FTRL = False
 | 
				
			||||||
DEBUG = False
 | 
					DEBUG = False
 | 
				
			||||||
def set_debug(val):
 | 
					def set_debug(val):
 | 
				
			||||||
    global DEBUG
 | 
					    global DEBUG
 | 
				
			||||||
| 
						 | 
					@ -86,14 +86,14 @@ cdef class ParserModel(AveragedPerceptron):
 | 
				
			||||||
        guess = eg.guess
 | 
					        guess = eg.guess
 | 
				
			||||||
        if guess == best or best == -1:
 | 
					        if guess == best or best == -1:
 | 
				
			||||||
            return 0.0
 | 
					            return 0.0
 | 
				
			||||||
        for feat in eg.c.features[:eg.c.nr_feat]:
 | 
					        if USE_FTRL:
 | 
				
			||||||
            self.update_weight_ftrl(feat.key, guess, feat.value * eg.c.costs[guess])
 | 
					            for feat in eg.c.features[:eg.c.nr_feat]:
 | 
				
			||||||
            self.update_weight_ftrl(feat.key, best, -feat.value * eg.c.costs[guess])
 | 
					                self.update_weight_ftrl(feat.key, guess, feat.value * eg.c.costs[guess])
 | 
				
			||||||
        #for clas in [guess, best]:
 | 
					                self.update_weight_ftrl(feat.key, best, -feat.value * eg.c.costs[guess])
 | 
				
			||||||
        #    loss += (-eg.c.costs[clas] - eg.c.scores[clas]) ** 2
 | 
					        else:
 | 
				
			||||||
        #    d_loss = eg.c.scores[clas] - -eg.c.costs[clas]
 | 
					            for feat in eg.c.features[:eg.c.nr_feat]:
 | 
				
			||||||
        #    for feat in eg.c.features[:eg.c.nr_feat]:
 | 
					                self.update_weight(feat.key, guess, feat.value * eg.c.costs[guess])
 | 
				
			||||||
        #        self.update_weight_ftrl(feat.key, clas, feat.value * d_loss)
 | 
					                self.update_weight(feat.key, best, -feat.value * eg.c.costs[guess])
 | 
				
			||||||
        return eg.c.costs[guess]
 | 
					        return eg.c.costs[guess]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def update_from_histories(self, TransitionSystem moves, Doc doc, histories, weight_t min_grad=0.0):
 | 
					    def update_from_histories(self, TransitionSystem moves, Doc doc, histories, weight_t min_grad=0.0):
 | 
				
			||||||
| 
						 | 
					@ -324,6 +324,8 @@ cdef class Parser:
 | 
				
			||||||
            eg.fill_scores(0, eg.c.nr_class)
 | 
					            eg.fill_scores(0, eg.c.nr_class)
 | 
				
			||||||
            eg.fill_costs(0, eg.c.nr_class)
 | 
					            eg.fill_costs(0, eg.c.nr_class)
 | 
				
			||||||
            eg.fill_is_valid(1, eg.c.nr_class)
 | 
					            eg.fill_is_valid(1, eg.c.nr_class)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.moves.finalize_state(stcls.c)
 | 
				
			||||||
        return loss
 | 
					        return loss
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def step_through(self, Doc doc):
 | 
					    def step_through(self, Doc doc):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -76,8 +76,8 @@ cdef class TaggerModel(AveragedPerceptron):
 | 
				
			||||||
        best = VecVec.arg_max_if_zero(eg.c.scores, eg.c.costs, eg.c.nr_class)
 | 
					        best = VecVec.arg_max_if_zero(eg.c.scores, eg.c.costs, eg.c.nr_class)
 | 
				
			||||||
        if guess != best:
 | 
					        if guess != best:
 | 
				
			||||||
            for feat in eg.c.features[:eg.c.nr_feat]:
 | 
					            for feat in eg.c.features[:eg.c.nr_feat]:
 | 
				
			||||||
                self.update_weight_ftrl(feat.key, best, -feat.value)
 | 
					                self.update_weight(feat.key, best, -feat.value)
 | 
				
			||||||
                self.update_weight_ftrl(feat.key, guess, feat.value)
 | 
					                self.update_weight(feat.key, guess, feat.value)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cdef void set_featuresC(self, ExampleC* eg, const TokenC* tokens, int i) except *:
 | 
					    cdef void set_featuresC(self, ExampleC* eg, const TokenC* tokens, int i) except *:
 | 
				
			||||||
        _fill_from_token(&eg.atoms[P2_orth], &tokens[i-2])
 | 
					        _fill_from_token(&eg.atoms[P2_orth], &tokens[i-2])
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user