title: PyMC3 how to implement latent dirichlet allocation?
body: <p>I am trying to implement lda using PyMC3. </p>
  
  <p>However, when defining the last part of the model in which words are sampled based on their topics, I keep getting the error: TypeError: list indices must be integers, not TensorVariable</p>
  
  <p>How to tackle the problem?</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>## Data Preparation
  
  K = 2 # number of topics
  N = 4 # number of words
  D = 3 # number of documents
  
  import numpy as np
  
  data = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [0, 0, 0, 0]])
  Wd = [len(doc) for doc in data]  # length of each document
  
  ## Model Specification
  
  from pymc3 import Model, Normal, HalfNormal, Dirichlet, Categorical, constant
  
  lda_model = Model()
  
  with lda_model:
  
      # Priors for unknown model parameters
      alpha = HalfNormal('alpha', sd=1)
      eta = HalfNormal('eta', sd=1)
  
      a1 = eta*np.ones(shape=N)
      a2 = alpha*np.ones(shape=K)
  
      beta = [Dirichlet('beta_%i' % i, a1, shape=N) for i in range(K)]
      theta = [Dirichlet('theta_%s' % i, a2, shape=K) for i in range(D)]
  
      z = [Categorical('z_%i' % d, p = theta[d], shape=Wd[d]) for d in range(D)]
  
      # That's when you get the error. It is caused by: beta[z[d][w]]
      w = [Categorical('w_%i_%i' % (d, w), p = beta[z[d][w]], observed = data[i,j]) for d in range(D) for w in range(Wd[d])]
  </code></pre>
  
  <p>Any help would be much appreciated!</p>