mirror of
				https://github.com/explosion/spaCy.git
				synced 2025-10-31 07:57:35 +03:00 
			
		
		
		
	Track which indices are being used
This commit is contained in:
		
							parent
							
								
									755d7d486c
								
							
						
					
					
						commit
						abb209f631
					
				|  | @ -1,9 +1,10 @@ | ||||||
| from thinc.linear.features cimport ConjunctionExtracter | from thinc.linear.features cimport ConjunctionExtracter | ||||||
| from thinc.typedefs cimport atom_t, weight_t | from thinc.typedefs cimport atom_t, weight_t | ||||||
| from thinc.structs cimport FeatureC | from thinc.structs cimport FeatureC | ||||||
| from libc.stdint cimport uint32_t | from libc.stdint cimport uint32_t, uint64_t | ||||||
| cimport numpy as np | cimport numpy as np | ||||||
| from cymem.cymem cimport Pool | from cymem.cymem cimport Pool | ||||||
|  | from libcpp.vector cimport vector | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| cdef class LinearModel: | cdef class LinearModel: | ||||||
|  | @ -14,6 +15,7 @@ cdef class LinearModel: | ||||||
|     cdef Pool mem |     cdef Pool mem | ||||||
|     cdef weight_t* W |     cdef weight_t* W | ||||||
|     cdef weight_t* d_W |     cdef weight_t* d_W | ||||||
|  |     cdef vector[uint64_t]* _indices | ||||||
| 
 | 
 | ||||||
|     cdef void hinge_lossC(self, weight_t* d_scores, |     cdef void hinge_lossC(self, weight_t* d_scores, | ||||||
|             const weight_t* scores, const weight_t* costs) nogil |             const weight_t* scores, const weight_t* costs) nogil | ||||||
|  |  | ||||||
|  | @ -15,6 +15,9 @@ cimport numpy as np | ||||||
| import numpy | import numpy | ||||||
| np.import_array() | np.import_array() | ||||||
| 
 | 
 | ||||||
|  | from thinc.neural.optimizers import Adam | ||||||
|  | from thinc.neural.ops import NumpyOps | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| cdef class LinearModel: | cdef class LinearModel: | ||||||
|     def __init__(self, int nr_class, templates, weight_t learn_rate=0.001, |     def __init__(self, int nr_class, templates, weight_t learn_rate=0.001, | ||||||
|  | @ -28,6 +31,10 @@ cdef class LinearModel: | ||||||
|                                            sizeof(weight_t)) |                                            sizeof(weight_t)) | ||||||
|         self.d_W = <weight_t*>self.mem.alloc(self.nr_weight * self.nr_class, |         self.d_W = <weight_t*>self.mem.alloc(self.nr_weight * self.nr_class, | ||||||
|                                            sizeof(weight_t)) |                                            sizeof(weight_t)) | ||||||
|  |         self._indices = new vector[uint64_t]() | ||||||
|  | 
 | ||||||
|  |     def __dealloc__(self): | ||||||
|  |         del self._indices | ||||||
| 
 | 
 | ||||||
|     cdef void hinge_lossC(self, weight_t* d_scores, |     cdef void hinge_lossC(self, weight_t* d_scores, | ||||||
|             const weight_t* scores, const weight_t* costs) nogil: |             const weight_t* scores, const weight_t* costs) nogil: | ||||||
|  | @ -129,12 +136,19 @@ cdef class LinearModel: | ||||||
|         # Sort them, to improve memory access pattern |         # Sort them, to improve memory access pattern | ||||||
|         libcpp.algorithm.sort(indices.begin(), indices.end()) |         libcpp.algorithm.sort(indices.begin(), indices.end()) | ||||||
|         for idx in indices: |         for idx in indices: | ||||||
|             W = &self.W[idx * nr_class] |             d_W = &self.d_W[idx * nr_class] | ||||||
|             for clas in range(nr_class): |             for clas in range(nr_class): | ||||||
|                 if d_scores[clas] < 0: |                 if d_scores[clas] < 0: | ||||||
|                     W[clas] -= self.learn_rate * max(-10., d_scores[clas]) |                     d_W[clas] += max(-10., d_scores[clas]) | ||||||
|                 else: |                 else: | ||||||
|                     W[clas] -= self.learn_rate * min(10., d_scores[clas]) |                     d_W[clas] += min(10., d_scores[clas]) | ||||||
|  | 
 | ||||||
|  |     def finish_update(self, optimizer): | ||||||
|  |         cdef np.npy_intp[1] shape | ||||||
|  |         shape[0] = self.nr_weight * self.nr_class | ||||||
|  |         W_arr = np.PyArray_SimpleNewFromData(1, shape, np.NPY_FLOAT, self.W) | ||||||
|  |         dW_arr = np.PyArray_SimpleNewFromData(1, shape, np.NPY_FLOAT, self.d_W) | ||||||
|  |         optimizer(W_arr, dW_arr, key=1) | ||||||
| 
 | 
 | ||||||
|     @property |     @property | ||||||
|     def nr_active_feat(self): |     def nr_active_feat(self): | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user