get_expanded_vars
Add a function that encapsulates the creation of the 'combined model':
pvars = pmodel.get_expanded_vars(w=qx, x=x_train)
The size of the datamodel will be automatically computed from the input data. Internally, the encapsulated code will be something like:
# create combined model
plate_size = pmodel._get_plate_size(sample_dict)
with ed.interception(inf.util.random_variable.set_values(**{**qvars, **sample_dict})):
pvars, _ = pmodel.expand_model(plate_size)
This might simplify the definition of custom loss functions.
def custom_elbo(pmodel, qvars, sample_dict):
# create combined model
pvars = pmodel.get_expanded_vars(**{**qvars, **sample_dict})
# compute energy
energy = tf.reduce_sum([tf.reduce_sum(p.log_prob(p.value)) for p in pvars.values()])
# compute entropy
entropy = - tf.reduce_sum([tf.reduce_sum(q.log_prob(q.value)) for q in qvars.values()])
# compute ELBO
ELBO = energy + entropy
# This function will be minimized. Return minus ELBO
return -ELBO
Right now the defined ELBO function uses the following parameters:
pmodel: The p modelqmodel: The q modelplate_size: The size of the plate (used to expand the models)batch_weight=1: Weights used to weight the energy part of the ELBO depending on the batch size with respect to the full dataset size (it should be a number bettween 0 and 1).
Therefore, this function, let's say get_expanded_vars, placed inside the module ELBO (i.e.: from inferpy.inference.loss_functions import ELBO and ELBO.get_expanded_vars(...)) can be defined as a function with the following parameters:
-pmodel
-qmodel
plate_size
And should return a tuple (pvars, qvars) (where both p and q vars are expanded, and coorrectly intercepted if needed).