Problems
Loading...
Step 5 of 6
ELBO Loss
Easy

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=1B∑i=1B∑d=1D(xid−x^id)2L_{\mathrm{reconstruction}} = \frac{1}{B}\sum_{i=1}^{B}\sum_{d=1}^{D}(x_{id}-\widehat{x}_{id})^2 LKL=−12B∑i=1B∑j=1L(1+log⁡σij2−μij2−exp⁡(log⁡σij2))L_{\mathrm{KL}} = -\frac{1}{2B}\sum_{i=1}^{B}\sum_{j=1}^{L} \left(1 + \log \sigma_{ij}^2 - \mu_{ij}^2 - \exp(\log \sigma_{ij}^2)\right) Ltotal=Lreconstruction+LKLL_{\mathrm{total}} = L_{\mathrm{reconstruction}} + L_{\mathrm{KL}}

Here, BB is batch size, DD is data width, and LL is latent width. Return a Python dictionary with exactly total_loss, reconstruction_loss, and kl_loss, each stored as a Python float.

Loading visualization...

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.