[Python] Unintelligible values in dataloader data

Discussão em 'Python' iniciado por Stack, Abril 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following code:

    class CustomQM9(InMemoryDataset):
    def __init__(self, root, target_indices):
    super().__init__(root)
    self.original_data = QM9(root)
    self.data, self.slices = self.process_data(target_indices)

    def process_data(self, target_indices):
    data_list = []
    for data in self.original_data:
    task_index = random.choice(target_indices)
    data.task_index = torch.tensor([task_index], dtype=torch.long)
    data_list.append(data)
    return self.collate(data_list)



    def load_dataset(batch_size, train_ratio, val_ratio, test_ratio, target_indices, device, dataset_usage_ratio=1.0,
    shuffling=False):
    dataset_path = "./data"
    dataset = CustomQM9(root=dataset_path, target_indices=target_indices)

    # choosing regression targets
    y_target = pd.DataFrame(dataset.data.y.cpu().numpy())
    dataset.data.y = torch.Tensor(y_target[target_indices].values).to(device)

    # shuffling data
    if shuffling:
    dataset = dataset.shuffle()

    # splitting the data
    num_samples = int(len(dataset) * dataset_usage_ratio)
    indices = list(range(num_samples))

    train_index, temp_index = train_test_split(indices, test_size=(1.0 - train_ratio), random_state=42)
    val_index, test_index = train_test_split(temp_index, test_size=test_ratio / (val_ratio + test_ratio),
    random_state=42)


    # normalizing the data
    data_mean = dataset.data.y[train_index].mean(dim=0, keepdim=True)
    data_std = dataset.data.y[train_index].std(dim=0, keepdim=True)
    dataset.data.y = ((dataset.data.y - data_mean) / data_std).to(device)

    # putting datasets into dataloaders
    train_loader = DataLoader([dataset for i in train_index], batch_size=batch_size, shuffle=shuffling)
    val_loader = DataLoader([dataset for i in val_index], batch_size=batch_size, shuffle=False)
    test_loader = DataLoader([dataset for i in test_index], batch_size=batch_size, shuffle=False)

    return train_loader, val_loader, test_loader


    and I use it like this:

    train_loader, val_loader, test_loader = dataloader.load_dataset(32,0.7,0.1,0.2,[0,1,2,3], device,0.001)


    When I print data like this:

    for i in train_index:
    print(dataset.task_index)


    or like this:

    for x in train_loader:
    for y in range(len(x)):
    print(x[y].task_index)


    gets the corresponding print, in which the task_index is from the range of target_indices ([0,1,2,3]) f.e.:

    tensor([3])
    tensor([0])
    tensor([0])
    tensor([1])
    tensor([1])
    tensor([2])


    but if I print like this:

    for batch in train_loader:
    print(batch.task_index)


    receives such print:

    tensor([ 3, 11, 24, 40, 49, 61, 74, 81, 85, 96, 102, 118, 125, 139,
    151, 167, 173, 185, 195, 209, 217, 228, 233, 245, 255, 265, 272, 281,
    291, 301, 309, 313])
    tensor([ 3, 15, 19, 30, 44, 52, 66, 75, 87, 96, 108, 123, 130, 142,
    153, 164, 173, 187, 202, 215, 229, 238, 246, 256, 267, 279, 288, 294,
    304, 314, 324, 332])
    tensor([ 1, 10, 18, 29, 34, 48, 52, 58, 73, 75, 78, 89, 100, 113,
    122, 136, 147, 162, 171, 187, 203, 214, 226, 234, 243, 259])


    And I have no idea where these values come from. Can someone explain it to me? What are these values?

    Continue reading...

Compartilhe esta Página