📘
Tensor Shape and Attributes
EasyPyTorch Basics
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
Python 3.13.1
Test Results
0/0Run code to see test results.