這篇文章主要講解了“fastText和GloVe怎么使用”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“fastText和GloVe怎么使用”吧!
創(chuàng)新互聯(lián)長期為上千客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為古城企業(yè)提供專業(yè)的網(wǎng)站設(shè)計制作、網(wǎng)站設(shè)計,古城網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
數(shù)據(jù)包括7613條tweet(Text列)和label(Target列),不管他們是否在談?wù)撜嬲臑?zāi)難。有3271行通知實際災(zāi)難,有4342行通知非實際災(zāi)難。
文本中真實災(zāi)難詞的例子:
“ Forest firenear La Ronge Sask. Canada “
使用災(zāi)難詞而不是關(guān)于災(zāi)難的例子:
“These boxes are ready to explode! ExplodingKittens finally arrived! gameofkittens #explodingkittens”
數(shù)據(jù)將被分成訓練(6090行)和測試(1523行)集,然后進行預(yù)處理。我們將只使用文本列和目標列。
from sklearn.model_selection import train_test_split data = pd.read_csv('train.csv', sep=',', header=0) train_df, test_df = train_test_split(data, test_size=0.2, random_state=42, shuffle=True)
此處使用的預(yù)處理步驟:
小寫
清除停用詞
標記化
from sklearn.utils import shuffle raw_docs_train = train_df['text'].tolist() raw_docs_test = test_df['text'].tolist() num_classes = len(label_names) processed_docs_train = [] for doc in tqdm(raw_docs_train): tokens = word_tokenize(doc) filtered = [word for word in tokens if word not in stop_words] processed_docs_train.append(" ".join(filtered)) processed_docs_test = [] for doc in tqdm(raw_docs_test): tokens = word_tokenize(doc) filtered = [word for word in tokens if word not in stop_words] processed_docs_test.append(" ".join(filtered)) tokenizer = Tokenizer(num_words=MAX_NB_WORDS, lower=True, char_level=False) tokenizer.fit_on_texts(processed_docs_train + processed_docs_test) word_seq_train = tokenizer.texts_to_sequences(processed_docs_train) word_seq_test = tokenizer.texts_to_sequences(processed_docs_test) word_index = tokenizer.word_index word_seq_train = sequence.pad_sequences(word_seq_train, maxlen=max_seq_len) word_seq_test = sequence.pad_sequences(word_seq_test, maxlen=max_seq_len)
使用fastText和Glove的第一步是下載每個預(yù)訓練過的模型。我使用google colab來防止我的筆記本電腦使用大內(nèi)存,所以我用request library下載了它,然后直接在notebook上解壓。
我使用了兩個詞嵌入中最大的預(yù)訓練模型。fastText模型給出了200萬個詞向量,而GloVe給出了220萬個單詞向量。
import requests, zipfile, io zip_file_url = “https://dl.fbaipublicfiles.com/fasttext/vectors-english/wiki-news-300d-1M.vec.zip" r = requests.get(zip_file_url) z = zipfile.ZipFile(io.BytesIO(r.content)) z.extractall()
import requests, zipfile, io zip_file_url = “http://nlp.stanford.edu/data/glove.840B.300d.zip" r = requests.get(zip_file_url) z = zipfile.ZipFile(io.BytesIO(r.content)) z.extractall()
FastText提供了加載詞向量的格式,需要使用它來加載這兩個模型。
embeddings_index = {} f = codecs.open(‘crawl-300d-2M.vec’, encoding=’utf-8') # Glove # f = codecs.open(‘glove.840B.300d.txt’, encoding=’utf-8') for line in tqdm(f): values = line.rstrip().rsplit(‘ ‘) word = values[0] coefs = np.asarray(values[1:], dtype=’float32') embeddings_index[word] = coefs f.close()
采用嵌入矩陣來確定訓練數(shù)據(jù)中每個詞的權(quán)重。
但是有一種可能性是,有些詞不在向量中,比如打字錯誤、縮寫或用戶名。這些單詞將存儲在一個列表中,我們可以比較處理來自fastText和GloVe的詞的性能
words_not_found = [] nb_words = min(MAX_NB_WORDS, len(word_index)+1) embedding_matrix = np.zeros((nb_words, embed_dim)) for word, i in word_index.items(): if i >= nb_words: continue embedding_vector = embeddings_index.get(word) if (embedding_vector is not None) and len(embedding_vector) > 0: embedding_matrix[i] = embedding_vector else: words_not_found.append(word) print('number of null word embeddings: %d' % np.sum(np.sum(embedding_matrix, axis=1) == 0))
fastText上的null word嵌入數(shù)為9175,GloVe 上的null word嵌入數(shù)為9186。
你可以對超參數(shù)或架構(gòu)進行微調(diào),但我將使用非常簡單的一個架構(gòu),它包含嵌入層、LSTM層、Dense層和Dropout層。
from keras.layers import BatchNormalization import tensorflow as tf model = tf.keras.Sequential() model.add(Embedding(nb_words, embed_dim, input_length=max_seq_len, weights=[embedding_matrix],trainable=False)) model.add(Bidirectional(LSTM(32, return_sequences= True))) model.add(Dense(32,activation=’relu’)) model.add(Dropout(0.3)) model.add(Dense(1,activation=’sigmoid’)) model.summary()
from keras.optimizers import RMSprop from keras.callbacks import ModelCheckpoint from tensorflow.keras.callbacks import EarlyStopping model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) es_callback = EarlyStopping(monitor='val_loss', patience=3) history = model.fit(word_seq_train, y_train, batch_size=256, epochs=30, validation_split=0.3, callbacks=[es_callback], shuffle=False)
fastText的準確率為83%,而GloVe的準確率為81%。與沒有詞嵌入的模型(68%)的性能比較,可以看出詞嵌入對性能有顯著的影響。
fastText 嵌入的準確度
GloVe 嵌入的準確度
沒有詞嵌入的準確度
感謝各位的閱讀,以上就是“fastText和GloVe怎么使用”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對fastText和GloVe怎么使用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
當前名稱:fastText和GloVe怎么使用
轉(zhuǎn)載來源:http://chinadenli.net/article46/ppjohg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、全網(wǎng)營銷推廣、手機網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、營銷型網(wǎng)站建設(shè)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)