Pystanで自然言語処理
へ向けて

2013/12/22 BUGS,Stan勉強会 #2
@xiangze750

1
Agenda
Pythonの魅力
● Pystanでできること
●
NLP(自然言語処理)
– topic model
– ライブラリ
– 混合モデル
– LDA
– Dirichlet process, Chinese restaurant process
– 階層Dirichlet process
●
生態学における中立理論
●

2
Pythonの魅力

●

●

豊富なライブラリ

●

Computer Vision (PIL,OpenCV)

●

数式処理(sympy)

●

音声処理(wave,Audiolab)音楽解析(music21)

●

可視化(matplotlib,networkx)

●

NLP(自然言語処理)

3
Pystanでできること

●

●

data(変数)はarrayで代入

4

http://pystan.readthedocs.org/en/latest/getting_started.html
NLPライブラリとの連携

●

●

NLTK(Natural language toolkit)
さまざまなCorpus(文書、単語の集合)が使える
–

●

N-gram化、頻度分布など

Gensim
–

Topic modelの実装(後述)

5
NLP(自然言語処理)

●

●

Bag of words
–

単語の位置関係の情報は捨て去る

–

6

http://journalofdigitalhumanities.org/wp-content/uploads/2013/02/blei_lda_illustration.png
NLP(自然言語処理)

●

●

Topic model
– 文書をtopicに分類
–

Topicを確率変数とする

7

http://journalofdigitalhumanities.org/wp-content/uploads/2013/02/blei_lda_illustration.png
NLPライブラリとの連携

●

●

shuyoさんによるトピックモデルのPython実装
–

https://github.com/shuyo/iir/blob/master/lda

–

NLTKのコーパス読み出し

–

documentをbag of wordsの形式にできる

–

階層Dirichlet modelも実装されている(後述)

8
混合モデル

●

●

多項混合モデル
–

多項分布(Categorical分布)でトピックごとの単語選択
のモデル化
多項分布=“歪んだ
サイコロ”

9

トピックモデル概論
http://sugiyama-www.cs.titech.ac.jp/~sugi/2007/Canon-MachineLearning27-jp.pdf
混合モデル

●

●

ポリヤ混合モデル
–

トピックの事前分布としてDirichlet分布を用いる

–

Dirichlet分布はCategorical分布,多項分布の共役事前分
布

トピックモデル概論
http://sugiyama-www.cs.titech.ac.jp/~sugi/2007/Canon-MachineLearning27-jp.pdf

10
Dirichlet分布

●

●

Categorical分布,多項分布の共役事前分布

●

simplex上の値を返す

●

Stanでは
vector<lower=0>[V] alpha;
simplex[V] x;
x~dirichlet(alpha);

11

歪んだサイコロを生成する
ガチャガチャ
LDA(latent Dirichlet
allocation)
●

●

Word w_m,nごとにトピックz_m,nがある。

●

トピックz_m,nごとに混合分布がある。
トピックの分布(documentごと)
単語の分布(トピックごと)
トピック
単語

12
LDA(latent dirichlet
allocation)
●

●

Stan code(manual 128 page)
parameters {
simplex[K] theta[M]; // topic dist for
doc m
simplex[V] phi[K]; // word dist for topic
k
}
model {
for (m in 1:M)
theta[m] ~ dirichlet(alpha); // prior
for (k in 1:K)
phi[k] ~ dirichlet(beta); // prior
for (n in 1:N) {
real gamma[K];
for (k in 1:K)
gamma[k]<-log(theta[doc[n],k])
+log(phi[k,w[n]]);
increment_log_prob(log_sum_exp(gamma));
}

潜在変数zのCategorical 分布は直接使えない
(http://xiangze.hatenablog.com/entry/2013/12/19/013557)

13
Dirichlet process

●

●

Topicの数を可変(non-parametric)としたい
–

無限変数のDirichlet分布

–

確率分布(Dirichlet分布)上の確率分布

–

変数を交換しても分布は変わらない(c.f. De Finetti's theorem)

任意の分割Aに対して

となればGはHをbase distributionとし
たDirichlet process

G
Θ
面積:G(A0),G(A1),......G(An)

Dirichlet Processes(Teh 2010)
http://www.gatsby.ucl.ac.uk/~ywteh/research/npbayes/Teh2010a.pdf

14
Chinese restaurant process

●

●

無限の変数を有限の過程で表現したい
–

観測変数は有限
確率変数を反復的に取り出す(変数の交換に対して不
変)

–

人(word)の多いテーブルに行きやすい

–

客:word
料理:topic
Table:対応関係

Dirichlet Processes(Teh 2010)
http://www.gatsby.ucl.ac.uk/~ywteh/research/npbayes/Teh2010a.pdf

15
Chinese restaurant process

●

n+1人目の客

新しいテーブ
ルに着く確率

既存のテーブルに着く確率
着席者が多いテーブルにつきやすい

Hierarchical Dirichlet Processes(Teh 2006)
http://www.gatsby.ucl.ac.uk/~ywteh/research/npbayes/jasa2006.pdf

16
階層Dirichlet process

●

●

Dirichlet process上のDirichlet process

Hierarchical Dirichlet Processes(Teh 2006)
http://www.gatsby.ucl.ac.uk/~ywteh/research/npbayes/jasa2006.pdf

17
CRPと階層Dirichlet process

●

●

Chinese resutaurant franchise
各店舗 出てくる料理は同じ
客:word
料理:topic
Table:対応関係

Hierarchical Dirichlet Processes(Teh 2006)
http://www.gatsby.ucl.ac.uk/~ywteh/research/npbayes/jasa2006.pdf

18
CRPと階層Dirichlet process

●

●

Chinese resutaurant franchise
–

単語分布の変数

–

トピック分布の変数

Hierarchical Dirichlet Processes(Teh 2006)
http://www.gatsby.ucl.ac.uk/~ywteh/research/npbayes/jasa2006.pdf

19
Chinese restaurant
franchise
●

●

実装の難点(Stan)
–

model内に変数への代入が書けない

20

“Infinite LDA” –Implementing the HDP with minimum code complexity
http://www.arbylon.net/publications/ilda.pdf
Chinese restaurant
franchise
●

●

実装(JAGS)
–

model {
x[1] ~ dnorm(0.0, 1.0E-4)
k <- 1
for (j in 1:M) {
for (i in 2:N) {
q ~ dunif(0,1)
totm <-sum(m)
if(q>gamma/(totm+gamma)){
ind ~ dmulti(m/sum(m),1)
th[i] <- th[ind]
m[ind] <- m[ind]+1
}else{
th[i] ~ dunif(0,1)
k <- k+1
m[k] <- 1
}
}

途中...

}
for (j in 1:M) {
for (i in 2:N) {
q ~ dunif(0,1)
if(q>alpha/(i-1+alpha)){
ind <- dmulti(n/sum(n),1)
phi[i] ~ th[j][ind]
nj[i] <- nj[i]+1
}else{
phi[i] ~ th[docid[i]*N+]
kn <- kn + 1
n[kn] <- 1
}
}
}

21
そもそもの問題意識

●

●

●

“ノンパラベイズに, 汎用の「パッケージ」はな
い”(Nonparametric Bayes for Non-Bayesians)
様々なデータ構造上の確率過程
–

Infinite Stochastic Tree

–

Mondrian Process

Mondrian Process

実装したかったもの

“Nonparametric Bayes for Non-Bayesians”
http://www.ism.ac.jp/~daichi/paper/ibis2008-npbayes-tutorial.pdf

22
Stick breaking process

●

●

(階層)Dirichlet processの別表現
π0

π1

π2
π3

23

Truncated stick breaking process...?
余談: 生態学における中立
理論
●

●

中立性
同一の生態学的ニッチに属する種の個体数分布は一定
の関数に従う
Ewens distribution
–

●

–
–

限られたニッチの中での各種の個体数の分布
Chinese restaurant processの特殊な場合

24

Stephen P. Hubbell先生
余談: 生態学における中立
理論
●

●

Ewens distribution

A unified theory of biogeography and relative species abundance and
its application to tropical rain forests and coral reefs
http://www3.botany.ubc.ca/vellend/COM_ECOL/Hubbell_CoralReefs97.pdf

25
余談: 生態学における中立
理論
●

●

Rのuntb package
–

http://cran.r-project.org/web/packages/untb
#example
demo(untb)
#Saunder datasetの個体数-種
の順位分布と推定されたθ

26
まとめ

●

●

●

●

Pystanを用いてば比較的簡単にStanのLDAを
使える。
Stan2.0では制約上ノンパラメトリックLDAの
実装はできない。JAGSでは出来るかもしれな
い。
生態学はすごい

27
Reference
●

shuyoさんによるLDA,HDP-LDAのpython実装(nltkを使用)
–

●

ノンパラベイズの入門の入門
–

●

http://www.gatsby.ucl.ac.uk/~ywteh/research/npbayes/Teh2010a.pdf

“Infinite LDA” –Implementing the HDP with minimum code complexity
–

●

http://www.gatsby.ucl.ac.uk/~ywteh/research/npbayes/jasa2006.pdf

Dirichlet Process(Teh 2010)
–

●

http://breakbee.hatenablog.jp/entry/2013/11/30/222553

Hierarchical Dirichlet Processes(Teh 2006)
–

●

http://www.kecl.ntt.co.jp/as/members/yamada/dpm_ueda_yamada2007.pdf

ディリクレ過程混合モデルへの変分推論適用について
–

●

http://sugiyama-www.cs.titech.ac.jp/~sugi/2007/Canon-MachineLearning27-jp.pdf

Introduction to Nonparametric Bayesian Models(上田、山田2007)
–

●

http://d.hatena.ne.jp/n_shuyo/20110608/hdplda

トピックモデル概論
–

●

http://www.slideshare.net/shuyo/ss-15098006

Mi manca qualche giovedi`?  階層ディリクレ過程を実装してみる (1) HDP-LDA と LDA のモデルを比較
–

●

https://github.com/shuyo/iir/blob/master/lda

http://www.arbylon.net/publications/ilda.pdf

A unified theory of biogeography and relative species abundance and its application to tropical rain forests and coral reefs
–

http://www3.botany.ubc.ca/vellend/COM_ECOL/Hubbell_CoralReefs97.pdf

28

More Related Content

PDF
Kosakunakano
PDF
2016word embbed supp
PDF
マーケティングサイエンス徹底入門と実践Part2
PDF
ベイジアンモデリングによるマーケティングサイエンス〜状態空間モデルを用いたモデリング
PDF
セグメンテーションの考え方・使い方 - TokyoR #44
PPTX
状態空間モデル等による多変量時系列データ解析
PDF
PyMC mcmc
PDF
時系列解析の使い方 - TokyoWebMining #17
Kosakunakano
2016word embbed supp
マーケティングサイエンス徹底入門と実践Part2
ベイジアンモデリングによるマーケティングサイエンス〜状態空間モデルを用いたモデリング
セグメンテーションの考え方・使い方 - TokyoR #44
状態空間モデル等による多変量時系列データ解析
PyMC mcmc
時系列解析の使い方 - TokyoWebMining #17

Similar to Pystan for nlp (20)

PDF
NLP若手の回 ACL2012参加報告
PDF
R による文書分類入門
PDF
MapReduceによる大規模データを利用した機械学習
PDF
Unified Expectation Maximization
PDF
機会学習ハッカソン:ランダムフォレスト
PDF
機会学習ハッカソン:ランダムフォレスト
PDF
科学技術計算関連Pythonパッケージの概要
PDF
CluBERT: A Cluster-Based Approach for Learning Sense Distributions in Multipl...
PDF
SIGIR2012勉強会 23 Learning to Rank
PPTX
0610 TECH & BRIDGE MEETING
PDF
質問応答システム入門
PDF
PFI Christmas seminar 2009
PDF
状態空間モデルの実行方法と実行環境の比較
PPTX
Mtg121024
PDF
Jubatusのリアルタイム分散レコメンデーション@TokyoWebmining#17
PDF
Random Forestsとその応用
PPTX
OpenNLP - MEM and Perceptron
PDF
Random Forests
PDF
Ism npblm-20120315
PDF
20150329 tokyo r47
NLP若手の回 ACL2012参加報告
R による文書分類入門
MapReduceによる大規模データを利用した機械学習
Unified Expectation Maximization
機会学習ハッカソン:ランダムフォレスト
機会学習ハッカソン:ランダムフォレスト
科学技術計算関連Pythonパッケージの概要
CluBERT: A Cluster-Based Approach for Learning Sense Distributions in Multipl...
SIGIR2012勉強会 23 Learning to Rank
0610 TECH & BRIDGE MEETING
質問応答システム入門
PFI Christmas seminar 2009
状態空間モデルの実行方法と実行環境の比較
Mtg121024
Jubatusのリアルタイム分散レコメンデーション@TokyoWebmining#17
Random Forestsとその応用
OpenNLP - MEM and Perceptron
Random Forests
Ism npblm-20120315
20150329 tokyo r47
Ad

More from Xiangze (8)

PDF
論文紹介 Amortized bayesian meta learning
PDF
Stochastic complexities of reduced rank regression証明概略
PDF
Gamma分布と仲間たち
PDF
Stable multi scale kernel for topological machine learning
PDF
RcppEigen and SVD
PDF
Data assim r
PDF
Patentmap r xinagze
PDF
Dynamic system in_r_xiangze
論文紹介 Amortized bayesian meta learning
Stochastic complexities of reduced rank regression証明概略
Gamma分布と仲間たち
Stable multi scale kernel for topological machine learning
RcppEigen and SVD
Data assim r
Patentmap r xinagze
Dynamic system in_r_xiangze
Ad

Pystan for nlp