v9.1.1
Menu

Access Dataset Sample

easyFREEDatasetsPyTorch DatasetsPytorch

Problem Statement

Given a PyTorch Dataset, extract a specific sample and return information about it.

Background

PyTorch Datasets support indexing — you can access individual samples, check the total number of samples, and iterate through the dataset.

Your Task

Write a function get_sample_info(dataset, index) that returns a dictionary with the total number of samples, the shape of the feature at the given index, and the label value at that index.

Output Format

Return a dictionary with keys: "num_samples" (int), "feature_shape" (list), "label_value" (int).

Example

Input
dataset with features [[1,2,3],[4,5,6]], labels [0,1], index=0
Output
{"num_samples": 2, "feature_shape": [3], "label_value": 0}
Reasoning
  • Inputs: dataset pairs a 2×32 \times 3 feature matrix with a label vector of length 2, so it holds 2 samples. Sample ii is row ii of the features together with entry ii of the labels. index is 0, the first sample.
  • Sample count: there are 2 rows and 2 labels, so num_samples is 2. It counts samples, not the 2×3=62 \times 3 = 6 individual feature values.
  • Feature shape: sample 0's feature is the row [1,2,3][1, 2, 3], a 1-D vector with 3 entries. Its shape is therefore [3]. It is not [2,3][2, 3], because that is the shape of the whole feature matrix, not of one sample.
  • Label: entry 0 of [0,1][0, 1] is 00, so label_value is 0, reported as a plain integer rather than a one-element tensor.
  • Notice: num_samples doesn't depend on index. With index =1= 1 you would get the same count and shape, but the label would be 11.

Constraints

  • Index will be valid (within dataset bounds)
  • Return feature_shape as a list, not torch.Size
  • label_value should be a Python int, not tensor

More from Pytorch

solution.py

Test results

Sign in to run your code against the test cases.