PIXELBANKv9.1.0
Menu

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:

Input:
(2, 2), "ones"
Output:
tensor([[1., 1.],
        [1., 1.]])
Reasoning:

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
solution.py

Test Results

0/0
Run code to see test results.
Initialize Special Tensors - Easy | PixelBank