def from_dict(cls, d, unk): assert unk in d vocab = Vocab(unk) vocab.update(d) vecs = [] for i in range(len(vocab)): word = vocab.index2word(i) vec = d[word] vecs.append(vec) array = np.array(vecs) return cls(array, vocab)
def from_files(cls, array_file, vocab_file): """Load the embedding matrix and the vocab from files. :param (file) array_file: file to read array from :param (file) vocab_file: file to read vocab from :return (Embeddings): an Embeddings object """ logging.info('Loading array...') array = np.load(array_file) logging.info('Loading vocab...') vocab = Vocab.from_file(vocab_file) return cls(array, vocab)
def embeddings(): v = Vocab('unk') v.update('what a show'.split()) array = np.reshape(np.arange(12), (4, 3)) return Embeddings(array, v)