In the class, to define self.variance
, how I can adjust your solution?
for i in range(4):
globals()["test_{}".format(i)].append(ToTensor(vectors[i]))
Becuase upper code works with your help.
But under case (in class) it doesn't work.
class MyDataset():
def __init__(self, cropped_img_vectors, targets):
self.data_0 = cropped_img_vectors[0]
self.data_1 = cropped_img_vectors[1]
self.data_2 = cropped_img_vectors[2]
self.data_3 = cropped_img_vectors[3]
self.targets = targets
def __getitem__(self, index):
data_0 = self.data_0[index]
data_1 = self.data_1[index]
data_2 = self.data_2[index]
data_3 = self.data_3[index]
y = self.targets[index]
dataset = []
for i in range(4):
dataset.append(["data_{}".format(i)])
return dataset, y
def __len__(self):
return len(self.data_0)
I changed uppder to under.
class MyDataset():
def __init__(self, cropped_1pixel_dataset, targets):
for i in range(4):
globals()["self.data_{}".format(i)] = cropped_1pixel_dataset[i]
self.targets = targets
def __getitem__(self, index):
for i in range(4):
globals()["data_{}".format(i)] = cropped_1pixel_dataset[i][index]
y = self.targets[index]
return [globals()["data_{}".format(i)] for i in range(4)], y
def __len__(self):
return len(self.data_0)
And after run this cell,
MyDataset(train_cropped_1pixel_dataset, train_dataset.targets)
it occur this error.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-960ee70394c1> in <module>
3 train_loader = torch.utils.data.DataLoader(dataset = train_dataset,
4 batch_size = batch_size,
----> 5 shuffle = True)
~/.local/lib/python3.5/site-packages/torch/utils/data/dataloader.py in __init__(self, dataset, batch_size, shuffle, sampler, batch_sampler, num_workers, collate_fn, pin_memory, drop_last, timeout, worker_init_fn)
800 if sampler is None:
801 if shuffle:
--> 802 sampler = RandomSampler(dataset)
803 else:
804 sampler = SequentialSampler(dataset)
~/.local/lib/python3.5/site-packages/torch/utils/data/sampler.py in __init__(self, data_source, replacement, num_samples)
58
59 if self.num_samples is None:
---> 60 self.num_samples = len(self.data_source)
61
62 if not isinstance(self.num_samples, int) or self.num_samples <= 0:
<ipython-input-10-293dc919d173> in __len__(self)
12
13 def __len__(self):
---> 14 return len(self.data_0)
AttributeError: 'MyDataset' object has no attribute 'data_0'
I really need helps..
Thank you.