Problem
Convert a nested Python list data into a torch.float32 PyTorch tensor and return a 3-tuple (tensor, shape_tuple, num_elements) where shape_tuple is a standard Python tuple of dimension sizes and num_elements is a Python int equal to the total number of scalar entries.
Why It Matters: Every PyTorch model begins by converting raw Python/NumPy data into float32 tensors and verifying their .shape and .numel() before feeding them into neural layers.
Function Signature
import torch
def make_float_tensor(data):
"""Convert nested list to float32 tensor and return (tensor, shape_tuple, num_elements)."""
Sample Input & Output
Example 1: 2D Matrix Input
Input:
data = [[1, 2, 3], [4, 5, 6]]
Expected Output:
(tensor([[1., 2., 3.], [4., 5., 6.]], dtype=torch.float32), (2, 3), 6)
Explanation: A 2×3 nested list converts to a float32 tensor of shape (2, 3) containing 2 * 3 = 6 elements.
Example 2: 1D Vector Input
Input:
data = [10, -5, 0]
Expected Output:
(tensor([10., -5., 0.], dtype=torch.float32), (3,), 3)
Explanation: A flat list produces a 1D tensor with shape (3,) and 3 total elements; integers are cast to float32.
Example 3: Single-Row 2D Batch
Input:
data = [[7.5]]
Expected Output:
(tensor([[7.5]], dtype=torch.float32), (1, 1), 1)
Explanation: Nested brackets preserve the 2D rank (1, 1) rather than collapsing to a scalar.
Constraints & Edge Cases
- Tensor dtype must be
torch.float32. - Ensure the tensor dtype is explicitly
torch.float32, even ifdatacontains Pythonintvalues. - Return
tuple(t.shape)(nottorch.Size) andint(t.numel())for clean type consistency.
Constraints & Evaluation Criteria
- Tensor dtype must be `torch.float32`.
Hints
[[1,2,3],[4,5,6]]
float32, shape=(2,3), numel=6