File size: 1,155 Bytes
cead561
 
2f4f993
cead561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f4f993
 
 
cead561
2f4f993
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import numpy as np

def load_deep1b(filename, start_idx=0, chunk_size=None):
    """ Read *.fbin file that contains float32 vectors
    Args:
        :param filename (str): path to *.fbin file
        :param start_idx (int): start reading vectors from this index
        :param chunk_size (int): number of vectors to read.
                                 If None, read all vectors
    Returns:
        Array of float32 vectors (numpy.ndarray)
    """
    with open(filename, "rb") as f:
        nvecs, dim = np.fromfile(f, count=2, dtype=np.int32)
        nvecs = (nvecs - start_idx) if chunk_size is None else chunk_size
        arr = np.fromfile(f, count=nvecs * dim, dtype=np.float32,
                          offset=start_idx * 4 * dim)
    return arr.reshape(nvecs, dim)

def load_glove(filename):
    from gensim.models import KeyedVectors
    return KeyedVectors.load_word2vec_format(filename).vectors


def load_sift1m(fname):
    data = np.fromfile(fname, dtype=np.float32)
    dim = data[0].view(np.int32)
    data = data.reshape(-1, dim + 1)
    data = np.ascontiguousarray(data[:, 1:])
    ndata, dim = data.shape
    return data, ndata, dim