| 133 | |
| 134 | |
| 135 | class Continuum: |
| 136 | def __init__(self, data, args): |
| 137 | self.data = data |
| 138 | self.batch_size = args.batch_size |
| 139 | n_tasks = len(data) |
| 140 | task_permutation = range(n_tasks) |
| 141 | |
| 142 | if args.shuffle_tasks == 'yes': |
| 143 | task_permutation = torch.randperm(n_tasks).tolist() |
| 144 | |
| 145 | sample_idxs = [] |
| 146 | |
| 147 | if '|' in args.samples_per_task: |
| 148 | s_args = list(map(int, str(args.samples_per_task).replace('|', '').split(','))) |
| 149 | assert len(s_args) == 3, \ |
| 150 | "Need (1)task number, (2)task length (3)other tasks length, got {}".format(s_args) |
| 151 | samples_per_task = [s_args[2] for _ in range(n_tasks)] |
| 152 | samples_per_task[int(s_args[0]) - 1] = s_args[1] |
| 153 | else: |
| 154 | samples_per_task = list(map(int, str(args.samples_per_task).split(","))) |
| 155 | print("parsed samples_per_task={}".format(samples_per_task)) |
| 156 | |
| 157 | # n = 1000 |
| 158 | for t in range(n_tasks): |
| 159 | N = data[t][1].size(0) |
| 160 | idx = t if len(samples_per_task) > t else 0 |
| 161 | if samples_per_task[idx] <= 0: |
| 162 | n = N |
| 163 | else: |
| 164 | n = min(samples_per_task[idx], N) |
| 165 | print("*********Task", t, "Samples are", n) |
| 166 | p = torch.randperm(data[t][1].size(0))[0:n] |
| 167 | sample_idxs.append(p) |
| 168 | |
| 169 | if args.iid: |
| 170 | n_tasks = 1 # assemble all data in 1 task |
| 171 | task_permutation = [0] |
| 172 | |
| 173 | min_class = np.inf |
| 174 | max_class = -1 |
| 175 | x_tr = [] |
| 176 | y_tr = [] |
| 177 | for task_t, t_data in enumerate( |
| 178 | self.data): # Each task like [(c1, c2), x_tr[i_tr].clone(), y_tr[i_tr].clone()] |
| 179 | min_class = min(min_class, min(t_data[0])) # (c1, c2) |
| 180 | max_class = max(max_class, max(t_data[0])) # (c1, c2) |
| 181 | x_tr.extend(t_data[1][sample_idxs[task_t]]) |
| 182 | y_tr.extend(t_data[2][sample_idxs[task_t]]) |
| 183 | x_tr = torch.stack(x_tr, dim=0) |
| 184 | y_tr = torch.stack(y_tr, dim=0) |
| 185 | self.data = [[(min_class, max_class), x_tr, y_tr]] |
| 186 | sample_idxs = [torch.randperm(y_tr.size(0))] |
| 187 | |
| 188 | self.task_idxs = [] |
| 189 | for t in range(n_tasks): |
| 190 | task_t = task_permutation[t] |
| 191 | |
| 192 | for _ in range(args.n_epochs): |