Пример #1
0
# for weight in map_weights:
#     weight[-1] = backtransform_constrained_scale(0.014, transform_unconstrained_scale_factor).numpy()
ensemble.set_weights(map_weights)
[network.noise_sigma for network in ensemble.networks]
initial_unconstrained_scales = [map_weights[i][-1] for i in range(len(map_weights))]
predictions = [network.predict(x_train) for network in ensemble.networks]
print(
    "Initial samples' mean train negative log likelihood",
    [
        -tf.reduce_mean(prediction.log_prob(y_train)).numpy()
        for prediction in predictions
    ],
)

ensemble.fit(
    x_train=x_train, y_train=y_train, batch_size=batch_size, epochs=10, verbose=0
)

predictions = [network.predict(x_train) for network in ensemble.networks]
print(
    "After gradients descent training mean train negative log likelihood",
    [
        -tf.reduce_mean(prediction.log_prob(y_train)).numpy()
        for prediction in predictions
    ],
)
[network.noise_sigma for network in ensemble.networks]
map_weights = ensemble.get_weights()
# for weight, scale in zip(map_weights, initial_unconstrained_scales):
#     weight[-1] = scale # backtransform_constrained_scale(0.014, transform_unconstrained_scale_factor).numpy() #backtransform_constrained_scale(np.array([0.1]), transform_unconstrained_scale_factor).numpy().astype(np.float32)
# ensemble.set_weights(map_weights)
Пример #2
0
initial_learning_rate = 0.01
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
    initial_learning_rate, decay_steps=n_train, decay_rate=0.9, staircase=True)

ensemble = MapDensityEnsemble(
    n_networks=n_networks,
    input_shape=[1],
    layer_units=layer_units,
    layer_activations=layer_activations,
    learning_rate=lr_schedule,
    seed=seed,
)

ensemble.fit(x_train=x_train,
             y_train=y_train,
             batch_size=batch_size,
             epochs=epochs,
             verbose=0)

# %%
prediction = ensemble.predict(x_plot)  # Mixture Of Gaussian prediction
fig, ax = plt.subplots(figsize=figsize)
plot_moment_matched_predictive_normal_distribution(
    x_plot=_x_plot,
    predictive_distribution=prediction,
    x_train=_x_train,
    y_train=y_train,
    y_ground_truth=y_ground_truth,
    fig=fig,
    ax=ax,
    y_lim=y_lim,
    initial_learning_rate, decay_steps=20, decay_rate=0.9, staircase=True
)


ensemble = MapDensityEnsemble(
    n_networks=2,
    input_shape=[1],
    layer_units=layer_units,
    layer_activations=layer_activations,
    learning_rate=lr_schedule,
    seed=0,
)


ensemble.fit(
    x_train=x_train, y_train=y_train, batch_size=batchsize_train, epochs=100, verbose=0
)


# %%
mog_prediction = ensemble.predict(x_plot)  # Mixture Of Gaussian prediction
plot_moment_matched_predictive_normal_distribution(
    x_plot=_x_plot,
    predictive_distribution=mog_prediction,
    x_train=_x_train,
    y_train=y_train,
    y_ground_truth=y_ground_truth,
    y_lim=y_lim,
)
# %%
ensemble.fit_additional_memebers(