Module 'pymc' has no attribute 'Lambda'; module 'pymc' has no attribute 'Container'

Using Pymc to build LDA model (codes attached at the end), running into errors: AttributeError: module ‘pymc’ has no attribute ‘Lambda’; AttributeError: module ‘pymc’ has no attribute ‘Container’.

Additional details -

PyMC Version: 4.1.4
Theano Version:1.0.5
Python Version:3.7.4
Operating system:mac
How did you install PyMC: tried both pip and conda 

Codes to implement -

import numpy as np
import pymc as pm

K = 2 # number of topics
V = 4 # number of words
D = 3 # number of documents

data = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [0, 0, 0, 0]])

alpha = np.ones(K)
beta = np.ones(V)

theta = pm.Container([pm.CompletedDirichlet("theta_%s" % i, pm.Dirichlet("ptheta_%s" % i, theta=alpha)) for i in range(D)])
phi = pm.Container([pm.CompletedDirichlet("phi_%s" % k, pm.Dirichlet("pphi_%s" % k, theta=beta)) for k in range(K)])
Wd = [len(doc) for doc in data]

z = pm.Container([pm.Categorical('z_%i' % d, 
                     p = theta[d], 
                     size=Wd[d],
                     value=np.random.randint(K, size=Wd[d]))
                  for d in range(D)])

# cannot use p=phi[z[d][i]] here since phi is an ordinary list while z[d][i] is stochastic
w = pm.Container([pm.Categorical("w_%i_%i" % (d,i),
                    p = pm.Lambda('phi_z_%i_%i' % (d,i), 
                              lambda z=z[d][i], phi=phi: phi[z]),
                    value=data[d][i], 
                    observed=True)
                  for d in range(D) for i in range(Wd[d])])

model = pm.Model([theta, phi, z, w])
mcmc = pm.MCMC(model)

Welcome!

If I not mistaken, containers were a pymc 2.0 feature. Is that right? If you are new to both versions 3 and 4, I would suggest you check out the documentation because parameter arrays are handled quite differently. Maybe start with the notebooks illustrating core features? For a basic comparison of v3 and v4 approaches to parameter arrays specifically, I might suggest this blog post I wrote.

1 Like

Hi Cluhmann, Thank so much. Yes, really new to Pymc 4. This is helpful. Do you happen to know what is the Lambda function in the version 4 that allows me to replace pm.Lambda in the old version:
w = [pm.Categorical(“w_%i_%i” % (d,i),
p = pm.Lambda(‘phi_z_%i_%i’ % (d,i),
lambda z=z[d][i], phi=phi: phi[z]),
value=data[d][i],
observed=True)
for d in range(D) for i in range(Wd[d])]

Not exactly, but it should end up looking much more like standard python/numpy indexing. So something much more like this?

w=pm.Categorical("w",
                 p=phi[z],
                 observed=data
  )

But that’s really just a shot in dark. Following numpy indexing conventions should get you most of the way there.

1 Like