From 2c3a67b693da506ca4b523743969ee6757e98b22 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Sun, 23 Oct 2016 14:49:31 +0200 Subject: [PATCH] Fix calculation of vector norm, re Issue #522. Need to consolidate the calculations into a helper function. --- spacy/tokens/doc.pyx | 9 +++++---- spacy/tokens/span.pyx | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/spacy/tokens/doc.pyx b/spacy/tokens/doc.pyx index 0fe5dc4ff..4e292d623 100644 --- a/spacy/tokens/doc.pyx +++ b/spacy/tokens/doc.pyx @@ -1,12 +1,12 @@ cimport cython from libc.string cimport memcpy, memset from libc.stdint cimport uint32_t +from libc.math cimport sqrt import numpy import numpy.linalg import struct cimport numpy as np -import math import six import warnings @@ -251,11 +251,12 @@ cdef class Doc: if 'vector_norm' in self.user_hooks: return self.user_hooks['vector_norm'](self) cdef float value + cdef double norm = 0 if self._vector_norm is None: - self._vector_norm = 1e-20 + norm = 0.0 for value in self.vector: - self._vector_norm += value * value - self._vector_norm = math.sqrt(self._vector_norm) + norm += value * value + self._vector_norm = sqrt(norm) if norm != 0 else 0 return self._vector_norm def __set__(self, value): diff --git a/spacy/tokens/span.pyx b/spacy/tokens/span.pyx index 2cac2d947..033c74c56 100644 --- a/spacy/tokens/span.pyx +++ b/spacy/tokens/span.pyx @@ -3,7 +3,7 @@ from collections import defaultdict import numpy import numpy.linalg cimport numpy as np -import math +from libc.math cimport sqrt import six from ..structs cimport TokenC, LexemeC @@ -136,11 +136,12 @@ cdef class Span: if 'vector_norm' in self.doc.user_span_hooks: return self.doc.user_span_hooks['vector'](self) cdef float value + cdef double norm = 0 if self._vector_norm is None: - self._vector_norm = 1e-20 + norm = 0 for value in self.vector: - self._vector_norm += value * value - self._vector_norm = math.sqrt(self._vector_norm) + norm += value * value + self._vector_norm = sqrt(norm) if norm != 0 else 0 return self._vector_norm property text: