PIXELBANKv8.2.1
Menu

Tensor Shape and Attributes

Problem Statement

Given a PyTorch tensor, extract and return its key attributes.

Background

Understanding tensor attributes is crucial for debugging and ensuring tensors are compatible for operations. Every tensor has a shape, data type, and device.

Your Task

Write a function get_tensor_info(tensor) that returns a dictionary containing the tensor's shape (as a list), dtype (as a string), and device (as a string).

Output Format

Return a dictionary with keys: "shape", "dtype", "device".

Example:

Input:
torch.rand(2, 3)
Output:
{"shape": [2, 3], "dtype": "torch.float32", "device": "cpu"}
Reasoning:

We access tensor.shape, tensor.dtype, and tensor.device attributes

Constraints:

  • Input will always be a valid PyTorch tensor
  • Tensor can be of any shape and data type
  • Return dtype and device as strings
Editor

Test Results

0/0
Run code to see test results.