I am trying to build a time series model with some initial parameters, and a latent categorical sampling that is dependent on the outcome of the previous sample. I am quickly running into a dimension error when trying to perform inference using MCMC on the latent initial parameters of the model. The error I am getting is:
ValueError: number of dimensions must be within [0, 32]
I was wondering if anyone had a suggestion on a better way to build the model/guide to avoid this dimensionality issue. My model is currently setup as follows:
def model(data):
## Latent model parameters. Interested in inferring rate
## Rate parameter
rate = pyro.param("rate", torch.tensor(.5),
constraint=pyro.distributions.constraints.positive)
r1 = pyro.sample("r1", pyro.distributions.Normal(rate, .01))
## starting points
s1_start = pyro.sample("s1_start", pyro.distributions.Normal(10, .01))
s2_start = pyro.sample("s2_start", pyro.distributions.Normal(10, .01))
s3_start = pyro.sample("s3_start", pyro.distributions.Normal(10, .01))
## Loop over each observed trace
for i in range(data.shape[0]):
## Each step in trace
for t in range(data.shape[1]):
# latent categorical sample
sample = pyro.sample("sample_{0}_{1}".format(str(t), str(i)),
pyro.distributions.Categorical(
torch.tensor([s1_start * r1,
s2_start * .1,
s3_start * .9])))
## Observed trace
update = np.zeros(3)
update[sample] = 1
s1_temp = pyro.sample("s1_{0}_{1}".format(str(t), str(i)),
pyro.distributions.Normal(s1_start + update[0], .01),
obs = data[i, t, 0])
s2_temp = pyro.sample("s2_{0}_{1}".format(str(t), str(i)),
pyro.distributions.Normal(s2_start + update[1], .01),
obs = data[i, t, 1])
s3_temp = pyro.sample("s3_{0}_{1}".format(str(t), str(i)),
pyro.distributions.Normal(s3_start + update[2], .01),
obs = data[i, t, 2])
s1_start = s1_temp
s2_start = s2_temp
s3_start = s3_temp
I have also tried to define a guide and use variational inference, but the rate
parameter does not update when going that route. My idea for the guide was as follows:
def guide(data):
## Latent model parameters. Interested in infering rate
#with pyro.plate("latent_rates"):
## Rate parameter
rate = pyro.param("rate", torch.tensor(.5),
constraint=pyro.distributions.constraints.positive)
r1 = pyro.sample("r1", pyro.distributions.Normal(rate, .01))
## starting points
s1_start = pyro.sample("s1_start", pyro.distributions.Normal(10, .01))
s2_start = pyro.sample("s2_start", pyro.distributions.Normal(10, .01))
s3_start = pyro.sample("s3_start", pyro.distributions.Normal(10, .01))
for i in range(data.shape[0]):
for t in range(data.shape[1]):
pyro.sample("sample_{0}_{1}".format(str(t), str(i)),
pyro.distributions.Categorical(
torch.tensor([s1_start * r1,
s2_start * .1,
s3_start * .9])))
Any ideas on what I am doing incorrectly would be much appreciated!