tensorflow使用預訓練詞向量

2021-08-19 21:02:34 字數 2068 閱讀 7726

glove的檔案說明如何使用預訓練詞向量,檔案格式如下:每行為乙個單詞和其對應的詞向量,以空格分隔。 

glove對應的詞向量,非二進位制檔案 

word2vec對應的詞向量,非二進位制檔案 

embedding = np.asarray(embd)vocab:為詞表 

w = tf.variable(tf.constant(0.0, shape=[vocab_size, embedding_dim]),

trainable=false, name="w")

embedding_placeholder = tf.placeholder(tf.float32, [vocab_size, embedding_dim])

embedding_init = w.assign(embedding_placeholder)

sess.run(embedding_init, feed_dict=)
tf.nn

.embedding_lookup(w, input_x)

from tensorflow.contrib import learn

#init vocab processor

vocab_processor = learn.preprocessing

.vocabularyprocessor(max_document_length)

#fit the vocab from glove

pretrain = vocab_processor.fit(vocab)

#transform inputs

input_x = np.array(list(vocab_processor.transform(your_raw_input)))

**

pytorch 使用預訓練層

將其他地方訓練好的網路,用到新的網路裡面 1.原先已經訓練好乙個網路 autoencoder fc 2.首先載入該網路,讀取其儲存的引數 3.設定乙個引數集 cnnpre autoencoder fc cnnpre.load state dict torch.load autoencoder fc....

如何使用預訓練的word embedding

def load glove word index args word index 分詞器生成的word index,第乙個元素為word,第二個元素為索引 returns embedding matrix 按照分詞器給出的索引組成的詞嵌入矩陣 embedding file embedding gl...

BERT 中文預訓練模型使用

只要是從事文字處理相關工作的人應該都知道 google 最新發布的 bert 模型,該模型屬於是詞向量的預訓練模型,一經提出便橫掃各大 nlp 任務,最近 google 公司如約推出了中文詞向量的預訓練模型,不得不說這是一件非常有良心的事情,在此膜拜和感謝 google 公司。那麼如何使用 bert...