Initialize Special Tensors
Problem Statement
Create common special tensors used in deep learning.
Background
PyTorch provides functions to create tensors with specific initial values — tensors of all zeros, all ones, and identity matrices. These are essential for weight initialization, masking, and other operations.
Your Task
Write a function create_special_tensors(n) that returns a dictionary with three n×n tensors: zeros, ones, and an identity matrix.
Output Format
Return a dictionary with keys: "zeros", "ones", "identity" — each as a nested list.
Example:
(2, 2), "ones"
tensor([[1., 1.],
[1., 1.]])We use torch.ones() with the given shape
Constraints:
- shape is a tuple of positive integers
- fill_type is one of: "zeros", "ones", "random"
- Return None for invalid fill_type
1. Background Knowledge
Special tensors (zeros, ones, identity) are fundamental building blocks in deep learning for weight initialization, mask creation, and bias computation. PyTorch provides efficient tensor creation functions that underpin these operations.
Key Concepts:
- Zero tensor: All elements = 0. Used for weight initialization in skip connections (ResNets), bias initialization, or gradient accumulation.
- Ones tensor: All elements = 1. Used for attention masks, normalization factors, or one-hot encodings.
- Identity matrix: 1s on main diagonal, 0s elsewhere. Mathematical property: I⋅X=X. Essential for residual connections and orthogonal transformations.
Mathematical definitions (for n×n matrices):
Zeros: Z_{ij} = 0 ∀ i,j
Ones: O_{ij} = 1 ∀ i,j
Identity: I_{ij} = 1 if i=j else 0
Why these matter in DL: Proper initialization prevents vanishing/exploding gradients. Research shows zero/ones initialization enables stable training of ultra-deep networks.
2. Algorithm Approach
Direct construction algorithm - No search or optimization needed:
Algorithm CreateSpecialTensors(n):
1. Initialize empty dictionary result
2. Create zeros matrix: [*n for _ in range(n)]
3. Create ones matrix: [*n for _ in range(n)]
4. Create identity: [[1 if i==j else 0 for j in range(n)] for i in range(n)]
5. Store in dictionary with keys "zeros", "ones", "identity"
6. Return dictionary
Time complexity: O(n2) - linear in matrix size.
3. Step-by-Step Strategy
def create_special_tensors(n):
# Step 1: Create zeros tensor (n x n)
zeros = [ * n for _ in range(n)]
# Step 2: Create ones tensor (n x n)
ones = [ * n for _ in range(n)]
# Step 3: Create identity matrix
identity = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
# Step 4: Package in dictionary with exact keys
return {
"zeros": zeros,
"ones": ones,
"identity": identity
}
Verification:
result = create_special_tensors(2)
assert result["zeros"] == [[0, 0], [0, 0]]
assert result["identity"] == [[1, 0], [0, 1]]
4. Common Pitfalls
- Wrong data structure: Return PyTorch tensors instead of nested lists
- Incorrect identity diagonal: Using [*n for _ in range(n)] creates all-ones matrix
- Mutable list reference: zeros = [*n] * n - all rows reference same list!
- Off-by-one indexing: Diagonal check i == j (0-based indexing)
- Missing dictionary keys: Exact spelling/case: "zeros", "ones", "identity"
Wrong examples:
# ❌ Mutable reference bug
bad_zeros = [*n] * n # All rows identical!
# ❌ All ones instead of identity
bad_identity = [*n for _ in range(n)]
# ❌ Wrong keys
return {"zero": zeros, "one": ones, "eye": identity}
5. Time & Space Complexity
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Zeros | O(n²) | O(n²) |
| Ones | O(n²) | O(n²) |
| Identity | O(n²) | O(n²) |
| Total | O(n²) | O(n²) |
Optimal: Cannot create n×n matrix faster than O(n2) space/time.
Complete Solution
def create_special_tensors(n):
"""Create n×n special tensors as nested lists."""
zeros = [ * n for _ in range(n)]
ones = [ * n for _ in range(n)]
identity = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
return {
"zeros": zeros,
"ones": ones,
"identity": identity
}
# Test
print(create_special_tensors(2))
# {'zeros': [[0, 0], [0, 0]], 'ones': [[1, 1], [1, 1]], 'identity': [[1, 0], [0, 1]]}
This solution is production-ready, handles all edge cases (n=1), and matches the exact output format required.