PIXELBANKv9.1.0
Menu

Orthogonal Initialization for RNNs

Problem Statement

Apply orthogonal initialization to recurrent network weights and verify the orthogonality property.

Background

Orthogonal initialization creates matrices where W^T W = I. This preserves gradient norms during backpropagation, making it ideal for RNNs.

Your Task

The starter code creates a square nn.Linear(4, 4) layer. Apply orthogonal initialization to the layer's weights. The verification code (computing W^T W, checking diagonal, computing singular values) is pre-filled.

Output Format

Returns a dictionary with "weight_shape", "wtw_diagonal", "is_orthogonal", and "singular_values".

Example:

Input:
None
Output:
{'weight_shape': [4, 4], 'wtw_diagonal': [1.0, 1.0, 1.0, 1.0], 'is_orthogonal': True, 'singular_values': [1.0, 1.0, 1.0, 1.0]}
Reasoning:
  • We start by seeding the random number generator with torch.manual_seed(42) to ensure reproducibility of the results.
  • A square matrix of size 4×44 \times 4 is created using nn.Linear(4, 4)$, and then nn.init.orthogonal_` is applied to the weight to make it orthogonal, meaning WTW=IW^T W = I where II is the identity matrix.
  • The matrix product WT@WW^T @ W is computed, and its diagonal is found to be approximately [1.0,1.0,1.0,1.0][1.0, 1.0, 1.0, 1.0] since WTWW^T W is close to the identity matrix, and the off-diagonal elements are less than 0.0010.001.
  • The singular values of the weight matrix are calculated and rounded to 4 decimals, resulting in [1.0,1.0,1.0,1.0][1.0, 1.0, 1.0, 1.0], confirming that the matrix is orthogonal, and the function returns a dictionary with the specified information.

Constraints:

  • Use nn.init.orthogonal_
  • Verify W^T W ≈ I
  • Compute singular values with torch.linalg.svdvals
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Orthogonal Initialization for RNNs - Hard | PixelBank