Hi, I’m a beginner running Bayesian linear regression but keep failing at inference. could you pls help?
#get data
DATA_URL = "https://d2hg8soec8ck9v.cloudfront.net/datasets/rugged_data.csv"
data = pd.read_csv(DATA_URL, encoding="ISO-8859-1")
df = data[["cont_africa", "rugged", "rgdppc_2000"]]
df = df[np.isfinite(df.rgdppc_2000)]
df["rgdppc_2000"] = np.log(df["rgdppc_2000"]) #log transform response
#create tensors
data_in = torch.tensor(df[["cont_africa", "rugged", "rgdppc_2000"]].values,
dtype=torch.float)
#slice tensors
is_cont_africa,ruggedness,lGDP = data_in[:, 0:1], data_in[:, 1:2], data_in[:, 2]
lGDP = data_in[:, 2:3] # maintain the dimensions
#define model
def model_BayesLinReg(is_cont_africa, ruggedness, lGDP):
n_obs = lGDP.shape[0]
a = pyro.sample("a", dist.Normal(8., 1000.))
b_a = pyro.sample("bA", dist.Normal(0., 1.))
b_r = pyro.sample("bR", dist.Normal(0., 1.))
b_ar = pyro.sample("bAR", dist.Normal(0., 1.))
sigma = pyro.sample("sigma", dist.Uniform(0., 10.))
mu = a + b_a * is_cont_africa + b_r * ruggedness + b_ar * is_cont_africa * ruggedness
with pyro.plate("N", n_obs):
y = pyro.sample("y", dist.Normal(mu, sigma), obs=lGDP)
return y
#Define functio to train model
def train(model, guide, lr=0.005, n_steps=501):
pyro.clear_param_store()
adam_params = {"lr": lr}
adam = pyro.optim.Adam(adam_params)
svi = SVI(model, guide, adam, loss=Trace_ELBO())
for step in range(n_steps):
loss = svi.step(is_cont_africa, ruggedness, lGDP)
if step % 50 == 0:
print('[iter {}] loss: {:.4f}'.format(step, loss))
autoguide_BayesLinReg = pyro.infer.autoguide.AutoDelta(model_BayesLinReg)
train(model = model_BayesLinReg, guide=autoguide_BayesLinReg)
error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-69-8e9ce9be844d> in <cell line: 14>()
12
13 autoguide_BayesLinReg = pyro.infer.autoguide.AutoDelta(model_BayesLinReg)
---> 14 train(model = model_BayesLinReg, guide=autoguide_BayesLinReg)
<ipython-input-69-8e9ce9be844d> in train(model, guide, lr, n_steps)
6 svi = SVI(model, guide, adam, loss=Trace_ELBO())
7
----> 8 for step in range(n_steps):
9 loss = svi.step(data)
10 if step % 50 == 0:
TypeError: range() missing 1 required positional arguments: "end"