<p>在类中,要定义<code>self.variance</code>,如何调整您的解决方案?你知道吗</p>
<pre><code>for i in range(4):
globals()["test_{}".format(i)].append(ToTensor(vectors[i]))
</code></pre>
<p>因为上面的代码可以在你的帮助下工作。你知道吗</p>
<p>但万一(在课堂上)不起作用。你知道吗</p>
<pre><code>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)
</code></pre>
<p>我把上面改成下面。你知道吗</p>
<pre><code>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)
</code></pre>
<p>运行完这个单元后</p>
<pre><code>MyDataset(train_cropped_1pixel_dataset, train_dataset.targets)
</code></pre>
<p>可能会发生这种错误。你知道吗</p>
<pre><code> -
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'
</code></pre>
<p>我真的需要帮助。。
非常感谢。你知道吗</p>