0
$\begingroup$
T=tensor([101,123,414,463][234,903,313,341]...)
train=TensorDataset(T)
train_dataloader=Dataloader(train)

Now I would like to update tensor T[0] i.e tensor

T[0]=tensor([101,123,567,463])

for this i have tried as follows:

train_dataloader.Dataset[0].index_copy_(0,tensor([2]),tensor([567])

is it possible to modify this way or not? Any kind of reference is helpful

$\endgroup$
5
  • $\begingroup$ If I am not mistaken you cannot access Dataset by index and modify it, same as one cannot access a tensor element by index and modify it $\endgroup$
    – Nikos M.
    Commented May 29, 2021 at 9:57
  • $\begingroup$ Is their any other way to handle this $\endgroup$ Commented May 29, 2021 at 10:03
  • $\begingroup$ train_dataloader.Dataset[0] is giving me first row in tensor i.e tensor([101,123,414,463]) $\endgroup$ Commented May 29, 2021 at 10:04
  • $\begingroup$ Is it a tensor though? $\endgroup$
    – Nikos M.
    Commented May 29, 2021 at 10:11
  • $\begingroup$ yes, it is a tensor and now I would like update certain values in it $\endgroup$ Commented May 29, 2021 at 10:16

1 Answer 1

2
$\begingroup$

Pytorch DataLoader is a generator, it will generate new batches when iterated through. Hence, as per the best of my knowledge, you can only change the data on the fly. For eg, if you want to replace the first element of your dataset with second element of the datase, you can do something like -

T = torch.tensor(([101,123,414,463],[234,903,313,341]))
train = TensorDataset(T)
train_dataloader = DataLoader(train)

for idx,i in enumerate(train_dataloader):
  if idx == 0:
    i = i[0] # Get the first and the only instance of the batch
    i = train[1][0]

Otherwise, you will need to change your dataset (train variable) and then again create a dataloader for this new dataset.

$\endgroup$
1
  • 1
    $\begingroup$ Thank You @ Devashish Prasad $\endgroup$ Commented May 29, 2021 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.