def save_as_memory_map_dir(directory: str, emb: Embeddings):
"""
Saves the given embeddings as memory map file and corresponding meta data in a directory.
Args:
directory: the directory to store the memory map file in (called `memory_map`) and the meta file (called
`meta.json` that stores the shape of the memory map and the actual vocabulary.
emb: the embeddings to store.
"""
if not os.path.exists(directory):
os.makedirs(directory)
meta_file = os.path.join(directory, "meta.json")
mem_map_file = os.path.join(directory, "memory_map")
with open(meta_file, "w") as f:
json.dump({
"vocab": emb.vocabulary,
"shape": emb.shape
}, f)
mem_map = np.memmap(mem_map_file, dtype='float32', mode='w+', shape=emb.shape)
mem_map[:] = emb.lookup[:]
mem_map.flush()
del mem_map
评论列表
文章目录