-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_generation.py
144 lines (98 loc) · 4.54 KB
/
dataset_generation.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import torch
import numpy as np
import progressbar
def determine_cls(sam,class_priority,patterns,def_class):
for cls in class_priority:
reasons = []
for pattern in patterns[cls]:
if (sam[list(pattern)] == 1).all():
reasons.append(pattern)
if len(reasons) > 0:
return cls, reasons
return def_class, None
def generate_sample_with_pattern(pattern,cls,n_dim,class_priority,patterns,def_class,pos_rate):
n_trial = 1
sam = np.random.binomial(1, pos_rate, n_dim)
sam[list(pattern)]=1
real_cls,_ = determine_cls(sam,class_priority,patterns,def_class)
while real_cls != cls:
sam = np.random.binomial(1, pos_rate, n_dim)
sam[list(pattern)]=1
real_cls,_ = determine_cls(sam,class_priority,patterns,def_class)
n_trial += 1
return tuple(sam), n_trial
def gen_INBEN(n_train, n_valid, n_test, n_dim, n_min_co, n_max_co, n_min_pattern, n_max_pattern, def_class, class_priority, pos_rate=0.2, seed=0):
torch.manual_seed(seed)
np.random.seed(seed=seed)
n_class = len(class_priority)
assert(set(class_priority)==set(range(n_class)))
assert(n_train%n_class==0)
assert(n_valid%n_class==0)
assert(n_test%n_class==0)
patterns = {}
features = np.array(list(range(n_dim)))
all_patterns = {}
for i in range(n_class):
n_pattern = np.random.randint(n_min_pattern, n_max_pattern+1)
print("class {} has {} patterns".format(i,n_pattern))
patterns[i]=[]
for j in range(n_pattern):
pattern_length = np.random.randint(n_min_co, n_max_co+1)
pattern = tuple(np.random.choice(features,pattern_length,False))
while pattern in all_patterns:
pattern_length = np.random.randint(n_min_co, n_max_co+1)
pattern = tuple(np.random.choice(features,pattern_length,False))
all_patterns[pattern] = i
patterns[i].append(pattern)
n_sam_per_class = int((n_train+n_valid+n_test)/n_class)
all_samples = set()
n_trials = []
n_repeats = []
samples_per_class = {}
for i in range(n_class):
print("generating class:",i)
samples_per_class[i] = []
n_pattern = len(patterns[i])
bar = progressbar.ProgressBar(max_value=n_sam_per_class)
for j in range(n_pattern):
pattern = patterns[i][j]
n_repeat = 0
sam, n_trial = generate_sample_with_pattern(pattern,i,n_dim,class_priority,patterns,def_class,pos_rate)
n_trials.append(n_trial)
while sam in all_samples:
sam, n_trial = generate_sample_with_pattern(pattern,i,n_dim,class_priority,patterns,def_class,pos_rate)
n_trials.append(n_trial)
n_repeat += 1
n_repeats.append(n_repeat)
samples_per_class[i].append(list(sam))
all_samples.add(sam)
bar.update(len(samples_per_class[i]))
while len(samples_per_class[i]) < n_sam_per_class:
bar.update(len(samples_per_class[i]))
pattern = np.random.choice(patterns[i],1)
n_repeat = 0
sam, n_trial = generate_sample_with_pattern(pattern,i,n_dim,class_priority,patterns,def_class,pos_rate)
n_trials.append(n_trial)
while sam in all_samples:
sam, n_trial = generate_sample_with_pattern(pattern,i,n_dim,class_priority,patterns,def_class,pos_rate)
n_trials.append(n_trial)
n_repeat += 1
n_repeats.append(n_repeat)
samples_per_class[i].append(list(sam))
all_samples.add(sam)
train_set = []
valid_set = []
test_set = []
n_train_per_class = int(n_train/n_class)
n_valid_per_class = int(n_valid/n_class)
n_test_per_class = int(n_test/n_class)
for i in range(n_class):
train_set.extend([(torch.tensor(sam,dtype=torch.float32),i) for sam in samples_per_class[i][:n_train_per_class]])
valid_set.extend([(torch.tensor(sam,dtype=torch.float32),i) for sam in samples_per_class[i][n_train_per_class:n_train_per_class+n_valid_per_class]])
test_set.extend([(torch.tensor(sam,dtype=torch.float32),i) for sam in samples_per_class[i][n_train_per_class+n_valid_per_class:]])
np.random.shuffle(train_set)
np.random.shuffle(valid_set)
np.random.shuffle(test_set)
print("avg trial:",np.mean(n_trials))
print("avg repeats:",np.mean(n_repeats))
return train_set, valid_set, test_set, patterns