用于语言模型的迁移学习

使用 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

# 训练模型 w2v_model = word2vec.Word2Vec(tokenized_corpus, size=embedding_dim, window=neighbor_words_num, iter=100)
# 获取与 "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

# 实例化模型 ft_model = fasttext.FastText(size=embedding_dim, window=neighbor_words_num)
# 构建词表 ft_model.build_vocab(sentences=tokenized_corpus)
# 训练模型 ft_model.train(sentences=tokenized_corpus, total_examples=len(tokenized_corpus), epochs=100)
使用 Keras 构建语言建模的循环神经网络(RNN)

Passons à la pratique !

使用 Keras 构建语言建模的循环神经网络(RNN)

Preparing Video For Download...