語言模型的遷移學習

使用 Keras 建立語言模型的循環神經網路(RNN)

David Cecchini

Data Scientist

遷移學習的核心想法

遷移學習:

  • 以優於隨機的初始權重開始
  • 使用在超大資料集上訓練的模型
  • 「開源」資料科學模型
使用 Keras 建立語言模型的循環神經網路(RNN)

可用的架構

基礎例子:I really loved this movie

  • Word2Vec
    • 連續詞袋(CBOW)X = [I, really, this, movie], y = loved
    • Skip-gram X = loved, y = [I, really, this, movie]
  • FastText X = [I, rea, eal, all, lly, really, ...], y = loved
    • 使用單字與字元 n-gram
  • ELMo X = [I, really, loved, this], y = movie
    • 依語境產生詞嵌入
    • 使用深度雙向語言模型(biLM)
  • Word2Vec 與 FastText 可在套件 gensim 取得,ELMo 在 tensorflow_hub
使用 Keras 建立語言模型的循環神經網路(RNN)

Word2Vec 範例

from gensim.models import word2vec

# Train the model w2v_model = word2vec.Word2Vec(tokenized_corpus, size=embedding_dim, window=neighbor_words_num, iter=100)
# Get top 3 similar words to "captain" w2v_model.wv.most_similar(["captain"], topn=3)
[('sweatpants', 0.7249663472175598),
('kirk', 0.7083336114883423),
('larry', 0.6495886445045471)]
使用 Keras 建立語言模型的循環神經網路(RNN)

FastText 範例

from gensim.models import fasttext

# Instantiate the model ft_model = fasttext.FastText(size=embedding_dim, window=neighbor_words_num)
# Build vocabulary ft_model.build_vocab(sentences=tokenized_corpus)
# Train the model ft_model.train(sentences=tokenized_corpus, total_examples=len(tokenized_corpus), epochs=100)
使用 Keras 建立語言模型的循環神經網路(RNN)

一起來練習吧!

使用 Keras 建立語言模型的循環神經網路(RNN)

Preparing Video For Download...