Tensor Slicing and Indexing
Problem Statement
Given a 2D tensor, extract specific rows, columns, and sub-matrices using PyTorch indexing.
Background
PyTorch tensors support NumPy-style indexing and slicing, allowing you to extract arbitrary parts of a tensor.
Your Task
Write a function extract_parts(tensor) that takes a 4×4 tensor and returns a dictionary with its first row, last column, and center 2×2 sub-matrix.
Output Format
Return a dictionary with keys: "first_row", "last_column", "center".
Each value must be a PyTorch tensor (first_row and last_column have 4 elements, center is 2×2). The sample below shows them as lists for readability.
Example
torch.arange(16).reshape(4, 4)
{"first_row": [0, 1, 2, 3], "last_column": [3, 7, 11, 15], "center": [[5, 6], [9, 10]]}- Input: a integer tensor. In the sample its entries are to laid out row by row, so the entry in row , column (counting from ) is .
first_row: fix and let run from to : . A 1-D tensor of length 4.last_column: fix and let run from to : gives . Each row down adds 4. Notice it comes back as a flat length-4 vector, not a column.center: drop the outer ring and keep rows crossed with columns : , , , , giving[[5, 6], [9, 10]]. This part stays 2-D.- Output: a dictionary mapping the three keys to tensors (shown as lists for display). An all-ones input gives all-ones parts with the same shapes.
Constraints
- Input tensor is always 4x4
- Values are integers
- Return a dictionary with the three required keys