Changing attributes of dataloader subset modifies entire dataset

i have a dataset object called total_loader
when initialized, it has an attribute called flip transform which is a torch.nn.module object.

i then create train and validation dataloaders by:

train_loader = Subset(total_loader,train_idx)
val_loader = Subset(total_loader,val_idx)
training_generator = DataLoader(train_loader, **params)
val_generator = DataLoader(val_loader, **params)

in the dataset object the get_item func checks whether self.flip_transform is not none and if so applies the transform, like this:
in get item…

if self.flip_transform is not None:
    y = self.flip_transform(y)
    return y

i want this behavior to happen only during training. i tried to manually set the flip_transform attribute back to none for the validation dataset only, but i see the change in both datasets.

i try something like this:

val_generator.dataset.dataset.flip_transform = None

this changes both train and validation generators back to None!

what should i do?

I guess you are changing the attribute of a reference, so you could create different objects for the training and validation datasets instead.
Your code currently doesn’t show the complete workflow, but since total_loader is reused, the same internal dataset would be used.

I was wondering if there’s a way, since I’m implementing my own flip transform class, maybe I can set it such that it won’t run whenever model.eval is called?