You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Technically this is not behavioural, but the code looks wrong. In spacy/pipeline/tagger.pyx:
defpredict(self, docs):
"""Apply the pipeline's model to a batch of docs, without modifying them. docs (Iterable[Doc]): The documents to predict. RETURNS: The models prediction for each document. DOCS: https://spacy.io/api/tagger#predict"""ifnotany(len(doc) for doc in docs):
# Handle cases where there are no tokens in any docs.
n_labels =len(self.labels)
guesses = [self.model.ops.alloc((0, n_labels)) for doc in docs]
assertlen(guesses) ==len(docs)
return guesses
scores =self.model.predict(docs)
assertlen(scores) ==len(docs), (len(scores), len(docs))
guesses =self._scores2guesses(scores)
assertlen(guesses) ==len(docs)
return guesses
def_scores2guesses(self, scores):
guesses = []
for doc_scores in scores:
doc_guesses = doc_scores.argmax(axis=1)
ifnotisinstance(doc_guesses, numpy.ndarray):
doc_guesses = doc_guesses.get()
guesses.append(doc_guesses)
return guesses
In the normal case, _scores2guesses produces the result which looks like a list of 1D arrays. But if all of the docs are empty, it uses [alloc((0, n_labels)) for doc in docs] which produces a list of 2D arrays. This looks like a shape inconsistency in the API contract. In the end, I don't think it affects the behaviour, but it looks wrong.
Hi, I'd like to work on this. In the empty-docs branch of Tagger.predict, I'd return 1D integer arrays (alloc((0,), dtype="i")) so they match what _scores2guesses returns in the normal case. I'd also add a test that checks the shape for both empty and non-empty docs. Does that sound right? Can I take it?
Activity
Amruthareddygurugari20 commentedon Oct 17, 2026
Hi, I'd like to work on this. In the empty-docs branch of
Tagger.predict, I'd return 1D integer arrays (alloc((0,), dtype="i")) so they match what_scores2guessesreturns in the normal case. I'd also add a test that checks the shape for both empty and non-empty docs. Does that sound right? Can I take it?