-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamplers.py
More file actions
101 lines (80 loc) · 3.48 KB
/
Copy pathsamplers.py
File metadata and controls
101 lines (80 loc) · 3.48 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import random
class BalancedSampler:
def __init__(self, full_dataset, class_list, n_way, k_shot):
self.class_list = class_list
self.n_way = n_way
self.k_shot = k_shot
dataset = {}
for label in class_list:
dataset[label] = full_dataset[label]
self.dataset = dataset
def sample(self):
support = {'texts':[],
'labels':[]}
query = {'texts':[],
'labels':[]}
n_classes = random.sample(self.class_list, self.n_way)
for label in n_classes:
k_samples = random.sample(self.dataset[label],2*self.k_shot)
random.shuffle(k_samples)
texts_s = k_samples[:self.k_shot]
texts_q = k_samples[self.k_shot:]
support['texts'].extend(texts_s)
support['labels'].extend([label]*self.k_shot)
query['texts'].extend(texts_q)
query['labels'].extend([label]*self.k_shot)
support_final = {}
query_final = {}
indices = list(range(len(support['texts'])))
random.shuffle(indices)
support_final['texts'] = [support['texts'] for i in indices]
support_final['labels'] = [support['labels'] for i in indices]
indices = list(range(len(query['texts'])))
random.shuffle(indices)
query_final['texts'] = [query['texts'] for i in indices]
query_final['labels'] = [query['labels'] for i in indices]
return support, query
class UnbalancedSampler:
def __init__(self, full_dataset, class_list, n_way, k_shot):
self.class_list = class_list
dataset = {}
for label in class_list:
dataset[label] = full_dataset[label]
self.dataset = dataset
def get_n_way(self):
n_way = random.randint(2, 5) #change this line to implement different sampling for n_way
return n_way
def get_k_shot(self, sampled_classes):
k_shot = {}
for label in sampled_classes:
k_shot[label] = random.randint(1, 5) #change this line to imlpement different sampling for n_way
return k_shot
def sample(self):
n_way = self.get_n_way()
sampled_classes = random.sample(self.class_list, n_way)
support_k_shot = self.get_k_shot(sampled_classes)
query_k_shot = 5
support = {'texts':[],
'labels':[]}
query = {'texts':[],
'labels':[]}
for label in sampled_classes:
k_samples = random.sample(self.dataset[label],query_k_shot+support_k_shot[label])
random.shuffle(k_samples)
texts_s = k_samples[:support_k_shot[label]]
texts_q = k_samples[support_k_shot[label]:]
support['texts'].extend(texts_s)
support['labels'].extend([label]*support_k_shot[label])
query['texts'].extend(texts_q)
query['labels'].extend([label]*query_k_shot)
support_final = {}
query_final = {}
indices = list(range(len(support['texts'])))
random.shuffle(indices)
support_final['texts'] = [support['texts'] for i in indices]
support_final['labels'] = [support['labels'] for i in indices]
indices = list(range(len(query['texts'])))
random.shuffle(indices)
query_final['texts'] = [query['texts'] for i in indices]
query_final['labels'] = [query['labels'] for i in indices]
return support, query