mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-26 18:06:29 +03:00
Restore optimized hidden_depth=0 for parser
This commit is contained in:
parent
c10f63bf10
commit
682346dd66
|
@ -14,4 +14,8 @@ cdef class Parser:
|
||||||
cdef readonly TransitionSystem moves
|
cdef readonly TransitionSystem moves
|
||||||
cdef readonly object cfg
|
cdef readonly object cfg
|
||||||
|
|
||||||
|
cdef void _parse_step(self, StateC* state,
|
||||||
|
const float* feat_weights,
|
||||||
|
int nr_class, int nr_feat, int nr_piece) nogil
|
||||||
|
|
||||||
#cdef int parseC(self, TokenC* tokens, int length, int nr_feat) nogil
|
#cdef int parseC(self, TokenC* tokens, int length, int nr_feat) nogil
|
||||||
|
|
|
@ -257,10 +257,15 @@ cdef class Parser:
|
||||||
nI=token_vector_width)
|
nI=token_vector_width)
|
||||||
|
|
||||||
with Model.use_device('cpu'):
|
with Model.use_device('cpu'):
|
||||||
upper = chain(
|
if depth == 0:
|
||||||
clone(Maxout(hidden_width), (depth-1)),
|
upper = chain()
|
||||||
zero_init(Affine(nr_class, drop_factor=0.0))
|
upper.is_noop = True
|
||||||
)
|
else:
|
||||||
|
upper = chain(
|
||||||
|
clone(Maxout(hidden_width), (depth-1)),
|
||||||
|
zero_init(Affine(nr_class, drop_factor=0.0))
|
||||||
|
)
|
||||||
|
upper.is_noop = False
|
||||||
# TODO: This is an unfortunate hack atm!
|
# TODO: This is an unfortunate hack atm!
|
||||||
# Used to set input dimensions in network.
|
# Used to set input dimensions in network.
|
||||||
lower.begin_training(lower.ops.allocate((500, token_vector_width)))
|
lower.begin_training(lower.ops.allocate((500, token_vector_width)))
|
||||||
|
@ -412,20 +417,27 @@ cdef class Parser:
|
||||||
cdef np.ndarray scores
|
cdef np.ndarray scores
|
||||||
c_token_ids = <int*>token_ids.data
|
c_token_ids = <int*>token_ids.data
|
||||||
c_is_valid = <int*>is_valid.data
|
c_is_valid = <int*>is_valid.data
|
||||||
|
cdef int has_hidden = not getattr(vec2scores, 'is_noop', False)
|
||||||
while not next_step.empty():
|
while not next_step.empty():
|
||||||
for i in range(next_step.size()):
|
if not has_hidden:
|
||||||
st = next_step[i]
|
for i in cython.parallel.prange(
|
||||||
st.set_context_tokens(&c_token_ids[i*nr_feat], nr_feat)
|
next_step.size(), num_threads=6, nogil=True):
|
||||||
self.moves.set_valid(&c_is_valid[i*nr_class], st)
|
self._parse_step(next_step[i],
|
||||||
vectors = state2vec(token_ids[:next_step.size()])
|
feat_weights, nr_class, nr_feat, nr_piece)
|
||||||
scores = vec2scores(vectors)
|
else:
|
||||||
c_scores = <float*>scores.data
|
for i in range(next_step.size()):
|
||||||
for i in range(next_step.size()):
|
st = next_step[i]
|
||||||
st = next_step[i]
|
st.set_context_tokens(&c_token_ids[i*nr_feat], nr_feat)
|
||||||
guess = arg_max_if_valid(
|
self.moves.set_valid(&c_is_valid[i*nr_class], st)
|
||||||
&c_scores[i*nr_class], &c_is_valid[i*nr_class], nr_class)
|
vectors = state2vec(token_ids[:next_step.size()])
|
||||||
action = self.moves.c[guess]
|
scores = vec2scores(vectors)
|
||||||
action.do(st, action.label)
|
c_scores = <float*>scores.data
|
||||||
|
for i in range(next_step.size()):
|
||||||
|
st = next_step[i]
|
||||||
|
guess = arg_max_if_valid(
|
||||||
|
&c_scores[i*nr_class], &c_is_valid[i*nr_class], nr_class)
|
||||||
|
action = self.moves.c[guess]
|
||||||
|
action.do(st, action.label)
|
||||||
this_step, next_step = next_step, this_step
|
this_step, next_step = next_step, this_step
|
||||||
next_step.clear()
|
next_step.clear()
|
||||||
for st in this_step:
|
for st in this_step:
|
||||||
|
@ -482,6 +494,28 @@ cdef class Parser:
|
||||||
beams.append(beam)
|
beams.append(beam)
|
||||||
return beams
|
return beams
|
||||||
|
|
||||||
|
cdef void _parse_step(self, StateC* state,
|
||||||
|
const float* feat_weights,
|
||||||
|
int nr_class, int nr_feat, int nr_piece) nogil:
|
||||||
|
'''This only works with no hidden layers -- fast but inaccurate'''
|
||||||
|
#for i in cython.parallel.prange(next_step.size(), num_threads=4, nogil=True):
|
||||||
|
# self._parse_step(next_step[i], feat_weights, nr_class, nr_feat)
|
||||||
|
token_ids = <int*>calloc(nr_feat, sizeof(int))
|
||||||
|
scores = <float*>calloc(nr_class * nr_piece, sizeof(float))
|
||||||
|
is_valid = <int*>calloc(nr_class, sizeof(int))
|
||||||
|
|
||||||
|
state.set_context_tokens(token_ids, nr_feat)
|
||||||
|
sum_state_features(scores,
|
||||||
|
feat_weights, token_ids, 1, nr_feat, nr_class * nr_piece)
|
||||||
|
self.moves.set_valid(is_valid, state)
|
||||||
|
guess = arg_maxout_if_valid(scores, is_valid, nr_class, nr_piece)
|
||||||
|
action = self.moves.c[guess]
|
||||||
|
action.do(state, action.label)
|
||||||
|
|
||||||
|
free(is_valid)
|
||||||
|
free(scores)
|
||||||
|
free(token_ids)
|
||||||
|
|
||||||
def update(self, docs_tokvecs, golds, drop=0., sgd=None, losses=None):
|
def update(self, docs_tokvecs, golds, drop=0., sgd=None, losses=None):
|
||||||
if not any(self.moves.has_gold(gold) for gold in golds):
|
if not any(self.moves.has_gold(gold) for gold in golds):
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in New Issue
Block a user