I am getting the below error -
ValueError: Dimensions must be equal, but are 10 and 60000 for '{{node mcmc_sample_chain/dual_averaging_step_size_adaptation___init__/_bootstrap_results/transformed_kernel_bootstrap_results/NoUTurnSampler/.bootstrap_results/process_args/maybe_call_fn_and_grads/value_and_gradients/value_and_gradient/JointDistributionSequential/log_prob/add_4}} = AddV2[T=DT_FLOAT](mcmc_sample_chain/dual_averaging_step_size_adaptation___init__/_bootstrap_results/transformed_kernel_bootstrap_results/NoUTurnSampler/.bootstrap_results/process_args/maybe_call_fn_and_grads/value_and_gradients/value_and_gradient/JointDistributionSequential/log_prob/add_3, mcmc_sample_chain/dual_averaging_step_size_adaptation___init__/_bootstrap_results/transformed_kernel_bootstrap_results/NoUTurnSampler/.bootstrap_results/process_args/maybe_call_fn_and_grads/value_and_gradients/value_and_gradient/JointDistributionSequential/log_prob/Deterministic/log_prob/Log)' with input shapes: [10,10], [10,60000].
for -
%%time
@tf.function
def utilities(x, betas, errors):
"""
`x * betas + errors` with broadcasting.
"""
x = tf.cast(x, dtype=tf.float32)
util = (tf.transpose(x) * betas) + errors
return util
k = 10
sigma_beta = 1.
sigma_error = 1.
def alt_pooled_model(X_train):
return tfd.JointDistributionSequential([
tfd.HalfCauchy(loc=0., scale=sigma_beta, name="sigma_beta"),
tfd.HalfCauchy(loc=0., scale=sigma_error, name="sigma_error"),
tfd.Normal(loc=tf.zeros(k), scale=sigma_beta, name="beta"),
tfd.Gumbel(loc=0., scale=sigma_error, name="error"),
lambda error, beta: tfd.Deterministic(
tf.math.argmax(
tfd.Multinomial(
total_count=1,
logits=utilities(X_train, beta[..., tf.newaxis], error[..., tf.newaxis]),
).sample(), axis=0
)
),
])
def target_log_prob(sigma_beta, sigma_error, beta, error):
return alt_pooled_model(X_train).log_prob(sigma_beta, sigma_error, beta, error,best_choices)
# Use NUTS for inference
hmc = tfp.mcmc.NoUTurnSampler(
target_log_prob_fn=target_log_prob,
step_size=.01)
# Unconstrain the scale parameters, which must be positive
hmc = tfp.mcmc.TransformedTransitionKernel(
inner_kernel=hmc,
bijector=[
tfp.bijectors.Identity(), # sigma_beta
tfp.bijectors.Identity(), # sigma_error
tfp.bijectors.Identity(), # beta
tfp.bijectors.Identity(), # error
])
# Adapt the step size for 100 steps before burnin and main sampling
hmc = tfp.mcmc.DualAveragingStepSizeAdaptation(
inner_kernel=hmc,
num_adaptation_steps=100,
target_accept_prob=.75)
# Initialize 10 chains using samples from the prior
joint_sample = alt_pooled_model(X_train).sample(10)
initial_state = [
joint_sample[0],
joint_sample[1],
joint_sample[2],
joint_sample[3],
# tf.ones((X_train.shape[0],), dtype=tf.int32) * -1 # initialize with invalid choices
]
# Compile with tf.function and XLA for improved runtime performance
@tf.function(autograph=False, experimental_compile=True)
def run():
return tfp.mcmc.sample_chain(
num_results=500,
current_state=initial_state,
kernel=hmc,
num_burnin_steps=200,
trace_fn=lambda _, kr: kr,
# trace_fn=lambda current_state, kernel_results: kernel_results
)
samples, traces = run()
print('R-hat diagnostics: ', tfp.mcmc.potential_scale_reduction(samples))
shape of X_train = (60000, 10)
shape of best_choice = (60000,)
Can somebody plz help me, I am not able to figure this out