Converting Float to Double Explicitly

So unless I explicitly convert to Doubles I get errors when running MCMC:

def model(x, y):
    x = torch.tensor(x).to(dtype=torch.double)
    y = torch.tensor(y).to(dtype=torch.double)
    β = pyro.sample("β", dist.Normal(torch.zeros(x.shape[1],y.shape[1]),torch.ones(x.shape[1],y.shape[1]))).to(dtype=torch.double)
    μ = torch.matmul(x,β)
    θ = pyro.sample("θ", dist.HalfCauchy(torch.ones(x.shape[1]))).to(dtype=torch.double)
    L = pyro.sample("L", dist.LKJCorrCholesky(x.shape[1], torch.tensor(1.))).to(dtype=torch.double)
    L_Ω = torch.mm(torch.diag(θ.sqrt()), L).to(dtype=torch.double)
    with pyro.plate("data", x.shape[0]):
        obs = pyro.sample("obs", dist.MultivariateNormal(loc=μ, scale_tril=L_Ω), obs=y).to(dtype=torch.double)

Am I the only one who thinks this is really annoying/ugly? Is there some reason that Pyro can’t do this conversion under the covers?

pyro works with both floats and doubles as long as you’re consistent.

you can make your life easier by using torch api like torch.set_default_dtype(torch.float64)

1 Like

Ah! Thanks for the tip. Definitely makes life easier (and prettier).