Download runtime/graphkit/_vectors.py from VISAI-AI/icj-citation-graph: direct link, hf CLI and curl.
- Browser
- Download file 860 Bytes
-
https://huggingface.co/datasets/VISAI-AI/icj-citation-graph/resolve/main/runtime/graphkit/_vectors.py
- Command line
-
hf download hf://datasets/VISAI-AI/icj-citation-graph/runtime/graphkit/_vectors.py
-
curl -L -o _vectors.py https://huggingface.co/datasets/VISAI-AI/icj-citation-graph/resolve/main/runtime/graphkit/_vectors.py
860 Bytes
| import numpy as np | |
| def validate_vectors(values, dimension=None, normalize=False): | |
| try: | |
| array = np.asarray(values, dtype=np.float32) | |
| except (TypeError, ValueError) as exc: | |
| raise ValueError('Embedding response is not a rectangular numeric matrix.') from exc | |
| if array.ndim != 2 or array.shape[0] == 0 or array.shape[1] == 0: | |
| raise ValueError('Embeddings must be a nonempty matrix.') | |
| if dimension and array.shape[1] != dimension: | |
| raise ValueError(f'Embedding dimension {array.shape[1]} does not match {dimension}.') | |
| norms = np.linalg.norm(array, axis=1) | |
| if not np.isfinite(array).all() or not np.isfinite(norms).all() or np.any(norms == 0): | |
| raise ValueError('Embeddings contain non-finite values or zero vectors.') | |
| if normalize: | |
| array = array / norms[:, None] | |
| return array.tolist() | |