Modeling sequential data


#1

Hi all,

I’m new to edward and I’m having difficulties to implement a sequential model. The main problem is that after inference procedure I’m getting the same value as initialization for latent variables. I’d like to infer alpha1, alpha2 and theta. Here’s my code:

## DATA ##
data = np.load("../data/pickle/data.npz")
x = data["x"].astype(np.int32)
cx = data["cx"].astype(np.float32)
dx = data["dx"].astype(np.float32)
n_users,n_iters = x.shape

## MODEL ##
# beta priors
alpha1 = Beta(tf.ones(n_users), tf.ones(n_users), name="alpha1")
alpha2 = Beta(tf.ones(n_users), tf.ones(n_users), name="alpha2")

# initial variables
a = [1] * n_iters
b = [1] * n_iters
theta = [0] * n_iters
X = [0] * n_iters

# build sequential model
a[0] = tf.ones(n_users)
b[0] = tf.ones(n_users)
for n in range(1,n_iters):
    with tf.name_scope("iter_{}".format(n)):
        a[n] = alpha1*a[n-1] + (1.-alpha1)*cx[:,n-1]
        b[n] = alpha2*b[n-1] + (1.-alpha2)*dx[:,n-1]
        theta[n] = Beta(a[n],b[n], name="theta_{}".format(n))
        X[n] = Bernoulli(theta[n], name="bernoulli_{}".format(n))

# remove first element
theta = theta[1:]
X = X[1:]

## INFERENCE ##
# build latent variables
T = 1000
latent_vars = {}
qalpha1 = Empirical(tf.Variable(tf.([T, n_users])))
qalpha2 = Empirical(tf.Variable(tf.ones([T, n_users])))
qtheta = [0] * len(theta)
for i,t in enumerate(theta):
    qtheta[i] = Empirical(tf.Variable(tf.random_normal([T, n_users])))
    latent_vars.update({t: qtheta[i]})
latent_vars.update({alpha1: qalpha1})
latent_vars.update({alpha2: qalpha2})

# build data
data = {X_t:x_t for X_t,x_t in zip(X,x.T[1:,:])}

# make inference
inference = ed.HMC(latent_vars, data=data)
inference.run(logdir="hmc_train/")

## RESULT ##
qalpha1.eval()
array([[ 1.,  1.,  1., ...,  1.,  1.,  1.],
   [ 1.,  1.,  1., ...,  1.,  1.,  1.],
   [ 1.,  1.,  1., ...,  1.,  1.,  1.],
   ..., 
   [ 1.,  1.,  1., ...,  1.,  1.,  1.],
   [ 1.,  1.,  1., ...,  1.,  1.,  1.],
   [ 1.,  1.,  1., ...,  1.,  1.,  1.]], dtype=float32)

I’m getting unexpected results (basically latent variables are keeping their initial value). I’m sure i’m doing something wrong. Does anybody know how to model this kind of problems?

Thanks in advance!


#2

Have you looked at HMC’s acceptance rate? It sounds like all samples are being rejected. I recommend tuning its hyperparameters (see API at http://edwardlib.org/api/ed/HMC).