use scalars everywhere
@@ -6,7 +6,7 @@ import numpy as np
import torch
import pyro
-from pyro.distributions import Uniform, Normal
+from pyro.distributions import Normal, Uniform
from pyro.infer import Importance, Marginal
"""
@@ -31,16 +31,16 @@ time_measurement_sigma = 0.02 # observation noise in seconds (known quantity)
# in steps of size dt, and optionally includes measurement noise
def simulate(mu, length=2.0, phi=np.pi / 6.0, dt=0.005, noise_sigma=None):
- T = torch.zeros(1)
- velocity = torch.zeros(1)
- displacement = torch.zeros(1)
- acceleration = torch.tensor([little_g * np.sin(phi)]) - \
- torch.tensor([little_g * np.cos(phi)]) * mu
+ T = torch.zeros(())
+ velocity = torch.zeros(())
+ displacement = torch.zeros(())
+ acceleration = torch.tensor(little_g * np.sin(phi)) - \
+ torch.tensor(little_g * np.cos(phi)) * mu
- if acceleration.item() <= 0.0: # the box doesn't slide if the friction is too large
- return torch.tensor([1.0e5]) # return a very large time instead of infinity
+ if acceleration.numpy() <= 0.0: # the box doesn't slide if the friction is too large
+ return torch.tensor(1.0e5) # return a very large time instead of infinity
- while displacement.item() < length: # otherwise slide to the end of the inclined plane
+ while displacement.numpy() < length: # otherwise slide to the end of the inclined plane
displacement += velocity * dt
velocity += acceleration * dt
T += dt
@@ -48,7 +48,7 @@ def simulate(mu, length=2.0, phi=np.pi / 6.0, dt=0.005, noise_sigma=None):
if noise_sigma is None:
return T
else:
- return T + noise_sigma * torch.randn(1)
+ return T + noise_sigma * torch.randn(())
# analytic formula that the simulator above is computing via
@@ -64,19 +64,19 @@ def analytic_T(mu, length=2.0, phi=np.pi / 6.0):
print("generating simulated data using the true coefficient of friction %.3f" % mu0)
N_obs = 20
torch.manual_seed(2)
-observed_data = torch.cat([simulate(torch.tensor([mu0]), noise_sigma=time_measurement_sigma)
+observed_data = torch.tensor([simulate(torch.tensor(mu0), noise_sigma=time_measurement_sigma)
for _ in range(N_obs)])
observed_mean = np.mean([T.item() for T in observed_data])
# define model with uniform prior on mu and gaussian noise on the descent time
def model(observed_data):
- mu_prior = Uniform(torch.zeros(1), torch.ones(1))
+ mu_prior = Uniform(0.0, 1.0)
mu = pyro.sample("mu", mu_prior)
def observe_T(T_obs, obs_name):
T_simulated = simulate(mu)
- T_obs_dist = Normal(T_simulated, torch.tensor([time_measurement_sigma]))
+ T_obs_dist = Normal(T_simulated, torch.tensor(time_measurement_sigma))
pyro.sample(obs_name, T_obs_dist, obs=T_obs)
for i, T_obs in enumerate(observed_data):
@@ -95,8 +95,8 @@ def main(args):
posterior_samples = [marginal(observed_data) for i in range(args.num_samples)]
# calculate statistics over posterior samples
- posterior_mean = torch.mean(torch.cat(posterior_samples))
- posterior_std_dev = torch.std(torch.cat(posterior_samples), 0)
+ posterior_mean = torch.mean(torch.tensor(posterior_samples))
+ posterior_std_dev = torch.std(torch.tensor(posterior_samples))
# report results
inferred_mu = posterior_mean.item()
