<s> import chainer <EOL> import chainer . links as L <EOL> import chainer . functions as F <EOL> from lda2vec import utils , dirichlet_likelihood <EOL> import numpy as np <EOL> class LDA ( chainer . Chain ) : <EOL> def __init__ ( self , n_docs , n_topics , n_dim , n_vocab ) : <EOL> factors = np . random . random ( ( n_topics , n_dim ) ) . astype ( '<STR_LIT>' ) <EOL> super ( LDA , self ) . __init__ ( proportions = L . EmbedID ( n_docs , n_topics ) , <EOL> factors = L . Parameter ( factors ) , <EOL> embedding = L . Linear ( n_dim , n_vocab ) ) <EOL> self . n_docs = n_docs <EOL> self . n_topics = n_topics <EOL> self . n_vocab = n_vocab <EOL> self . n_dim = n_dim <EOL> def forward ( self , ids , bow ) : <EOL> bow , ids = utils . move ( self . xp , bow , ids ) <EOL> proportions = self . proportions ( ids ) <EOL> ld = dirichlet_likelihood ( proportions ) <EOL> doc = F . matmul ( F . softmax ( proportions ) , self . factors ( ) ) <EOL> logp = F . dropout ( self . embedding ( doc ) ) <EOL> sources , targets , counts = [ ] , [ ] , [ ] <EOL> lpi = F . sum ( bow * F . log_softmax ( logp ) , axis = <NUM_LIT:1> ) <EOL> loss = - F . sum ( lpi ) <EOL> return loss , ld </s>
