예제 #1
0
Qx_mean = [DeterministicVariable(0., 'x0_mean', learnable=True)]
for t in range(1, T):
    Qx_mean.append(
        DeterministicVariable(0., x_names[t] + "_mean", learnable=True))
    Qx.append(
        NormalVariable(logit_b_post * Qx[t - 1] + Qx_mean[t],
                       1.,
                       x_names[t],
                       learnable=True))
variational_posterior = ProbabilisticModel([Qb] + Qx)
AR_model.set_posterior_model(variational_posterior)

# Inference #
inference.stochastic_variational_inference(
    AR_model,
    number_iterations=100,
    number_samples=300,
    optimizer=chainer.optimizers.Adam(0.05))

loss_list = AR_model.diagnostics["loss curve"]

# Statistics
posterior_samples = AR_model._get_posterior_sample(2000)
b_posterior_samples = posterior_samples[b].data.flatten()
b_mean = np.mean(b_posterior_samples)
b_sd = np.sqrt(np.var(b_posterior_samples))

x_mean = []
lower_bound = []
upper_bound = []
for xt in x:
# Variational Model
Qb1 = NormalVariable(np.zeros((number_hidden_units, 1)),
                     0.2*np.ones((number_hidden_units, 1)), "b1", learnable=True)
Qb2 = NormalVariable(np.zeros((number_output_classes, 1)),
                     0.2*np.ones((number_output_classes, 1)), "b2", learnable=True)
Qweights1 = NormalVariable(np.zeros((number_hidden_units, number_pixels)),
                           0.2*np.ones((number_hidden_units, number_pixels)), "weights1", learnable=True)
Qweights2 = NormalVariable(np.zeros((number_output_classes, number_hidden_units)),
                           0.2*np.ones((number_output_classes, number_hidden_units)), "weights2", learnable=True)
variational_model = ProbabilisticModel([Qb1, Qb2, Qweights1, Qweights2])
model.set_posterior_model(variational_model)

# Inference
inference.stochastic_variational_inference(model,
                                           number_iterations=2000,
                                           number_samples=50,
                                           optimizer=chainer.optimizers.Adam(0.005)) #0.05

# Test accuracy
num_images = 500
test_size = len(test)
test_indices = RandomIndices(dataset_size=test_size, batch_size=1, name="test_indices", is_observed=True)
test_images = EmpiricalVariable(np.array([np.reshape(image[0], newshape=(number_pixels, 1)) for image in test]).astype("float32"),
                           indices=test_indices, name="x_test", is_observed=True)
test_labels = EmpiricalVariable(np.array([image[1]*np.ones((1, 1))
                                          for image in test]).astype("int32"), indices=test_indices, name="labels", is_observed=True)
test_model = ProbabilisticModel([test_images, test_labels])

s = 0
for _ in range(num_images):
    test_sample = test_model._get_sample(1)