-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdataset.py
More file actions
259 lines (176 loc) · 6.64 KB
/
dataset.py
File metadata and controls
259 lines (176 loc) · 6.64 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import numpy as np
import torch
def parallelogram_dataset(p, dim, num, seed=0, device='cpu'):
torch.manual_seed(seed)
np.random.seed(seed)
N_sample = 5 * num
x = np.random.choice(p, N_sample*dim*3).reshape(N_sample, 3, dim)
target = -x[:,0,:] + x[:,1,:] + x[:,2,:]
id_ = np.where(np.prod((target >= 0) * (target < p), axis=1)==1)[0][:num]
target = target[id_]
x = x[id_]
data_id = 0
for i in range(dim):
data_id += x[:,:,i] * p ** (dim-i-1)
labels = 0
for i in range(dim):
labels += target[:,i] * p ** (dim-i-1)
data_id = torch.from_numpy(data_id).to(device)
labels = torch.from_numpy(labels).to(device)
vocab_size = p**dim
dataset = {}
dataset['data_id'] = data_id
dataset['label'] = labels
dataset['vocab_size'] = vocab_size
return dataset
def modular_addition_dataset(p, seed=0, device='cpu'):
torch.manual_seed(seed)
np.random.seed(seed)
x = np.arange(p)
y = np.arange(p)
XX, YY = np.meshgrid(x, y)
data_id = np.transpose([XX.reshape(-1,), YY.reshape(-1,)])
labels = (data_id[:,0] + data_id[:,1]) % p
labels = torch.tensor(labels, dtype=torch.long)
vocab_size = p
dataset = {}
dataset['data_id'] = data_id
dataset['label'] = labels
dataset['vocab_size'] = vocab_size
return dataset
def split_dataset(dataset, train_ratio, seed=0):
torch.manual_seed(seed)
np.random.seed(seed)
dataset2 = {}
num = dataset['data_id'].shape[0]
train_num = int(num*train_ratio)
test_num = num - train_num
train_id = np.random.choice(num,train_num,replace=False)
test_id = np.array(list(set(np.arange(num)) - set(train_id)))
dataset2['train_data_id'] = dataset['data_id'][train_id]
dataset2['test_data_id'] = dataset['data_id'][test_id]
dataset2['train_label'] = dataset['label'][train_id]
dataset2['test_label'] = dataset['label'][test_id]
dataset2['vocab_size'] = dataset['vocab_size']
return dataset2
def repeat_dataset(dataset):
dataset2 = {}
dataset2['train_data_id'] = dataset['data_id']
dataset2['test_data_id'] = dataset['data_id']
dataset2['train_label'] = dataset['label']
dataset2['test_label'] = dataset['label']
dataset2['vocab_size'] = dataset['vocab_size']
return dataset2
def combine_dataset(train_dataset, test_dataset):
dataset_c = {}
dataset_c['train_data_id'] = train_dataset['data_id']
dataset_c['test_data_id'] = test_dataset['data_id']
dataset_c['train_label'] = train_dataset['label']
dataset_c['test_label'] = test_dataset['label']
assert train_dataset['vocab_size'] == test_dataset['vocab_size']
dataset_c['vocab_size'] = train_dataset['vocab_size']
return dataset_c
# Dataset and DataLoader
class ToyDataset(torch.utils.data.Dataset):
def __init__(self, inputs, targets):
self.inputs = inputs
self.targets = targets
def __len__(self):
return len(self.inputs)
def __getitem__(self, idx):
return self.inputs[idx], self.targets[idx]
def descendant_dataset(p, num, seed=0, device='cpu'):
torch.manual_seed(seed)
np.random.seed(seed)
N_sample = num
x = np.random.choice(range(2,p), N_sample*2).reshape(N_sample, 2)
# Check if b is a descendant of a
# In a complete binary tree where two children of x is 2x and 2x+1
def is_desc(a, b):
while b > 1:
if b == a:
return True
b //= 2 # Move up to the parent node
return b == a
target = np.array([1 if is_desc(x[i,0]-1, x[i,1]-1) else 0 for i in range(N_sample)])
data_id = torch.from_numpy(x).to(device)
labels = torch.from_numpy(target).to(device)
vocab_size = p+2
dataset = {}
dataset['data_id'] = data_id
dataset['label'] = labels
dataset['vocab_size'] = vocab_size
return dataset
def descendant_dataset_2(p, num, seed=0, device='cpu'):
torch.manual_seed(seed)
np.random.seed(seed)
N_sample = num*4
x = np.random.choice(range(1,(p-1)//2), num*2).reshape(num, 2)
data = np.zeros((N_sample, 4), dtype=np.int32)
data[:num,0] = x[:,0]
data[:num,1] = 2*x[:,0]
data[:num,2] = x[:,1]
data[:num,3] = 2*x[:,1]
data[num:(2*num),0] = x[:,0]
data[num:(2*num),1] = 2*x[:,0] + 1
data[num:(2*num),2] = x[:,1]
data[num:(2*num),3] = 2*x[:,1] + 1
data[2*num:(3*num),0] = 2*x[:,0] + 1
data[2*num:(3*num),1] = x[:,0]
data[2*num:(3*num),2] = 2*x[:,1] + 1
data[2*num:(3*num),3] = x[:,1]
data[3*num:(4*num),0] = 2*x[:,0] + 1
data[3*num:(4*num),1] = x[:,0]
data[3*num:(4*num),2] = 2*x[:,1] + 1
data[3*num:(4*num),3] = x[:,1]
np.random.shuffle(data)
data_id = torch.from_numpy(data[:, :3]).to(device)
labels = torch.from_numpy(data[:, 3]).to(device)
vocab_size = p+1
dataset = {}
dataset['data_id'] = data_id
dataset['label'] = labels
dataset['vocab_size'] = vocab_size
return dataset
def greater_than_dataset(p, num, seed=0, device='cpu'):
torch.manual_seed(seed)
np.random.seed(seed)
N_sample = num
x = np.random.choice(range(p), N_sample*2).reshape(N_sample, 2)
target = np.array([p+1 if x[i,0] > x[i,1] else p for i in range(N_sample)])
data_id = torch.from_numpy(x).to(device)
labels = torch.from_numpy(target).to(device)
vocab_size = p+2
dataset = {}
dataset['data_id'] = data_id
dataset['label'] = labels
dataset['vocab_size'] = vocab_size
return dataset
def xor_dataset(p, num, seed=0, device='cpu'):
torch.manual_seed(seed)
np.random.seed(seed)
N_sample = num
x = np.random.choice(range(p), N_sample*2).reshape(N_sample, 2)
target = np.array([x[i,0]^x[i,1] for i in range(N_sample)])
data_id = torch.from_numpy(x).to(device)
labels = torch.from_numpy(target).to(device)
vocab_size = p+2
dataset = {}
dataset['data_id'] = data_id
dataset['label'] = labels
dataset['vocab_size'] = vocab_size
return dataset
def multi_step_dataset(p, num, seed=0, device='cpu'):
torch.manual_seed(seed)
np.random.seed(seed)
N_sample = num
x = np.random.choice(range(p), N_sample*3).reshape(N_sample, 3)
target = np.array([(x[i,0]*x[i,1]+x[i,2])%p for i in range(N_sample)])
data_id = torch.from_numpy(x).to(device)
labels = torch.from_numpy(target).to(device)
vocab_size = p
dataset = {}
dataset['data_id'] = data_id
dataset['label'] = labels
dataset['vocab_size'] = vocab_size
return dataset