Problems
Loading...
Step 1 of 6
VAE Encoder
Easy

Implement the inference network of a variational autoencoder. The encoder maps each input row to the mean and log-variance of a diagonal Gaussian latent distribution.

μ=XWμ+bμ\mu = XW_{\mu} + b_{\mu} log⁡σ2=XWlog⁡σ2+blog⁡σ2\log \sigma^2 = XW_{\log \sigma^2} + b_{\log \sigma^2}

Here, X∈RB×DX \in \mathbb{R}^{B \times D} is a batch of inputs, WμW_{\mu} and Wlog⁡σ2W_{\log \sigma^2} have shape (D,L)(D,L), the two biases have shape (L)(L), and LL is the latent width. Return a Python dictionary with exactly two keys: mu and log_var. Each value must be a float64 NumPy array with shape (B,L)(B,L).

Loading visualization...

Examples

Input: x = [[0.5,-0.3,0.1],[0.2,0.8,-0.5]], W_mu = [[0.1,-0.2],[0.3,0.1],[-0.1,0.4]], b_mu = [0,0.1], W_logvar = [[-0.2,0.1],[0.1,-0.3],[0.2,0]], b_logvar = [0.05,-0.05]

Output: {"mu":[[-0.05,0.01],[0.31,-0.06]],"log_var":[[-0.06,0.09],[-0.01,-0.27]]}

Explanation: The two supplied affine projections produce the Gaussian mean and log-variance independently.

Input: x = [[1,-1],[0.5,0.25]], W_mu = [[0.4],[-0.2]], b_mu = [0.1], W_logvar = [[0.1],[0.3]], b_logvar = [-0.2]

Output: {"mu":[[0.7],[0.25]],"log_var":[[-0.4],[-0.075]]}

Input: x = [[-0.4,0.6,0.2]], W_mu = [[0.2,0.1],[-0.3,0.5],[0.4,-0.2]], b_mu = [-0.1,0.2], W_logvar = [[0.1,-0.4],[0.2,0.3],[-0.5,0.2]], b_logvar = [0,0.1]

Output: {"mu":[[-0.28,0.42]],"log_var":[[-0.02,0.48]]}

Hint 1

Use one matrix multiplication for each latent parameter.

Hint 2

Broadcast each bias across the batch dimension.

Requirements

  • Use NumPy.
  • Apply the supplied mean and log-variance projections independently.
  • Include the supplied bias in each projection.
  • Return exactly the documented dictionary and float64 arrays.

Constraints

  • x has shape (B, D) and dtype float64.
  • W_mu and W_logvar have shape (D, L) and dtype float64.
  • b_mu and b_logvar have shape (L) and dtype float64.
  • B, D, and L are positive.