Repository navigation
Expand file tree
/
Copy pathContinuum.py
More file actions
64 lines (50 loc) · 1.69 KB
/
Copy pathContinuum.py
File metadata and controls
64 lines (50 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import torch
import random
"""
This is from Facebook...
"""
class Continuum:
def __init__(self, data, args):
self.data = data
self.batch_size = args.batch_size
n_tasks = len(data)
task_permutation = range(n_tasks)
if args.shuffle_tasks == 'yes':
task_permutation = torch.randperm(n_tasks).tolist()
sample_permutations = []
for t in range(n_tasks):
N = data[t][1].size(0)
if args.samples_per_task <= 0:
n = N
else:
n = min(args.samples_per_task, N)
p = torch.randperm(N)[0:n]
sample_permutations.append(p)
self.permutation = []
for t in range(n_tasks):
task_t = task_permutation[t]
for _ in range(args.epochs):
task_p = [[task_t, i] for i in sample_permutations[task_t]]
random.shuffle(task_p)
self.permutation += task_p
self.length = len(self.permutation)
self.current = 0
def __iter__(self):
return self
def next(self):
return self.__next__()
def __next__(self):
if self.current >= self.length:
raise StopIteration
else:
ti = self.permutation[self.current][0]
j = []
i = 0
while (((self.current + i) < self.length) and
(self.permutation[self.current + i][0] == ti) and
(i < self.batch_size)):
j.append(self.permutation[self.current + i][1])
i += 1
self.current += i
j = torch.LongTensor(j)
return self.data[ti][1][j], ti, self.data[ti][2][j]