| 210 | |
| 211 | |
| 212 | class CosineBasicBackend(BasicBackend): |
| 213 | def __init__(self, vectors: npt.NDArray, arguments: BasicArgs) -> None: |
| 214 | """Initialize the cosine basic backend.""" |
| 215 | super().__init__(vectors=vectors, arguments=arguments) |
| 216 | self._vectors = normalize_or_copy(self._vectors) |
| 217 | |
| 218 | def _dist(self, x: npt.NDArray) -> npt.NDArray: |
| 219 | """Compute cosine distance.""" |
| 220 | x_norm = normalize(x) |
| 221 | sim = x_norm.dot(self._vectors.T) |
| 222 | return 1 - sim |
| 223 | |
| 224 | def insert(self, vectors: npt.NDArray) -> None: |
| 225 | """Insert vectors into the vector space.""" |
| 226 | # Normalize the new vectors |
| 227 | _norm_vectors = normalize_or_copy(vectors) |
| 228 | self._vectors = np.vstack([self._vectors, _norm_vectors]) |
| 229 | |
| 230 | |
| 231 | class EuclideanBasicBackend(BasicBackend): |
no outgoing calls
no test coverage detected
searching dependent graphs…