Compute the negative evidence lower bound used to train a variational autoencoder. Use a summed squared reconstruction error per sample and the diagonal-Gaussian KL regularizer.
Lreconstruction=B1i=1∑Bd=1∑D(xid−xid)2 LKL=−2B1i=1∑Bj=1∑L(1+logσij2−μij2−exp(logσij2)) Ltotal=Lreconstruction+LKLHere, B is batch size, D is data width, and L is latent width. Return a Python dictionary with exactly total_loss, reconstruction_loss, and kl_loss, each stored as a Python float.
Examples
Input: x = [[0,0]], reconstruction = [[0,0]], mu = [[0,0]], log_var = [[0,0]]
Output: {"total_loss":0,"reconstruction_loss":0,"kl_loss":0}
Explanation: Exact reconstruction and a standard-normal posterior make both loss terms zero.
Input: x = [[1,1]], reconstruction = [[0,0]], mu = [[0,0]], log_var = [[0,0]]
Output: {"total_loss":2,"reconstruction_loss":2,"kl_loss":0}
Input: x = [[1,0],[0.5,0.5]], reconstruction = [[0,0.5],[0.25,0.75]], mu = [[0.5,-0.5],[0.2,0.1]], log_var = [[-1,0.5],[0.2,-0.3]]
Output: {"total_loss":0.969705,"reconstruction_loss":0.6875,"kl_loss":0.282205}
Hint 1
Reduce squared reconstruction errors over features before the batch mean.
Hint 2
Use np.exp(log_var) in the KL term.
Hint 3
Convert each returned NumPy scalar to float.
Requirements
- Use NumPy.
- Sum squared reconstruction errors over features and average over samples.
- Compute the batch-mean diagonal-Gaussian KL term.
- Return exactly the three documented Python floats.
Constraints
- x and reconstruction have the same shape (B, D) and dtype float64.
- mu and log_var have the same shape (B, L) and dtype float64.
- All inputs contain finite values.
- B, D, and L are positive.
All steps in VAE
Accepts: array
Accepts: array
Accepts: array
Accepts: array
ELBO Loss
Compute the negative evidence lower bound used to train a variational autoencoder. Use a summed squared reconstruction error per sample and the diagonal-Gaussian KL regularizer.
Lreconstruction=B1i=1∑Bd=1∑D(xid−xid)2 LKL=−2B1i=1∑Bj=1∑L(1+logσij2−μij2−exp(logσij2)) Ltotal=Lreconstruction+LKLHere, B is batch size, D is data width, and L is latent width. Return a Python dictionary with exactly total_loss, reconstruction_loss, and kl_loss, each stored as a Python float.
Examples
Input: x = [[0,0]], reconstruction = [[0,0]], mu = [[0,0]], log_var = [[0,0]]
Output: {"total_loss":0,"reconstruction_loss":0,"kl_loss":0}
Explanation: Exact reconstruction and a standard-normal posterior make both loss terms zero.
Input: x = [[1,1]], reconstruction = [[0,0]], mu = [[0,0]], log_var = [[0,0]]
Output: {"total_loss":2,"reconstruction_loss":2,"kl_loss":0}
Input: x = [[1,0],[0.5,0.5]], reconstruction = [[0,0.5],[0.25,0.75]], mu = [[0.5,-0.5],[0.2,0.1]], log_var = [[-1,0.5],[0.2,-0.3]]
Output: {"total_loss":0.969705,"reconstruction_loss":0.6875,"kl_loss":0.282205}
Hint 1
Reduce squared reconstruction errors over features before the batch mean.
Hint 2
Use np.exp(log_var) in the KL term.
Hint 3
Convert each returned NumPy scalar to float.
Requirements
- Use NumPy.
- Sum squared reconstruction errors over features and average over samples.
- Compute the batch-mean diagonal-Gaussian KL term.
- Return exactly the three documented Python floats.
Constraints
- x and reconstruction have the same shape (B, D) and dtype float64.
- mu and log_var have the same shape (B, L) and dtype float64.
- All inputs contain finite values.
- B, D, and L are positive.
All steps in VAE
Accepts: array
Accepts: array
Accepts: array
Accepts: array