根据词表获得预训练的字向量:
import pickle
import tqdm
import numpy as np
from nlutools import tools as nlu
def gene_embedding():
vocab_path = "./word2id.pkl"
with open(vocab_path, 'rb') as f:
word2id = pickle.load(f)
word2id_new = sorted(word2id.items(),key = lambda x:x[1],reverse = False)
vecs = []
for char, i in word2id_new:
v = nlu.w2v(char)
print(np.array(v).shape) # v shape: (1, 300)
vecs.append(v[0])
np.save('./pretrain_embedding', a)
if __name__ == "__main__":
gene_embedding()
vecs = np.load('./pretrain_embedding.npy')
print("pretrain_embedding shape:",vecs.shape)
pretrain_embedding shape: (2960, 300)