回答編集履歴
1
質問追記
test
CHANGED
@@ -32,7 +32,7 @@
|
|
32
32
|
|
33
33
|
次に、以下のようにすることでミニバッチ内のインデックスが取得できます。上記の場合はバッチサイズが4なので、forの中では0,1,2,3のインデックスとそのデータが取得できます。このような回答でよろしかったでしょうか。
|
34
34
|
|
35
|
-
```
|
35
|
+
```python
|
36
36
|
|
37
37
|
for i, data in enumerate(trainloader, 0):
|
38
38
|
|
@@ -43,3 +43,71 @@
|
|
43
43
|
print(index, result) # これでバッチ内のインデックスとそのデータが取得できる。
|
44
44
|
|
45
45
|
```
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<質問に対する回答の追記>
|
50
|
+
|
51
|
+
例えばですが、以下のようにクラスを定義することで、お望みのインデックスありのデータセットが定義できます。
|
52
|
+
|
53
|
+
```python
|
54
|
+
|
55
|
+
class Subset(Dataset):
|
56
|
+
|
57
|
+
"""
|
58
|
+
|
59
|
+
Subset of a dataset at specified indices.
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
Arguments:
|
64
|
+
|
65
|
+
dataset (Dataset): The whole Dataset
|
66
|
+
|
67
|
+
indices (sequence): Indices in the whole set selected for subset
|
68
|
+
|
69
|
+
"""
|
70
|
+
|
71
|
+
def __init__(self, data, label, indices):
|
72
|
+
|
73
|
+
self.data = data
|
74
|
+
|
75
|
+
self.label = label
|
76
|
+
|
77
|
+
self.indices = indices
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
def __getitem__(self, idx):
|
82
|
+
|
83
|
+
#out_data = self.transform(self.data)[idx]
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
return self.data, self.label, self.data[self.indices[idx]]
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
def __len__(self):
|
92
|
+
|
93
|
+
return len(self.indices)
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
train_size = len(trainset) # n_samples is 60000
|
98
|
+
|
99
|
+
indices = list(range(0,train_size)) # [0,1,.....47999]
|
100
|
+
|
101
|
+
train_dataset = Subset(trainset.data, trainset.targets, indices)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
# 二つ目のデータの入力とラベルとインデックスを表示
|
106
|
+
|
107
|
+
print(train_dataset.data[2])
|
108
|
+
|
109
|
+
print(train_dataset.label[2])
|
110
|
+
|
111
|
+
print(train_dataset.indices[2])
|
112
|
+
|
113
|
+
```
|