/
spirals_data_new.py
464 lines (340 loc) · 13.3 KB
/
spirals_data_new.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
from torch.utils import data
from random import shuffle
import torch
import numpy as np
import matplotlib.pyplot as plt
import os
np.random.seed(0)
N = 400 # number of points per class
D = 2 # dimensionality
K = 2 # number of classes
number = N
r0 = 0.2
circles = 2
def spiral_xy(i,spiral_num, number, circles = 1, r0 = 0 ):
'''
'''
phi = torch.tensor(0.) # start value of phi
delta_phi = torch.tensor(2 * np.pi * circles / number) # increment which is added to phi for each new datapoint
phi= phi+i*delta_phi
r = r0 # initial radius
delta_r = circles / number
r = r +i* delta_r
x= r*spiral_num*torch.cos(phi)
y = r*spiral_num*torch.sin(phi)
return [x,y]
def spiral(spiral_num):
return [spiral_xy(i, spiral_num,number=N, circles=2, r0=0.1) for i in range(N)]
X = np.zeros((N*K, D))
y = np.zeros(N*K, dtype='uint8')
y[0:N] = np.zeros(N) # represents -1
y[N:] = np.ones(N)
X[0:N] = spiral(-1)
X[N:] = spiral(1)
data_X = X
data_y = y
# GPU or CPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if torch.cuda.device_count() > 0:
print("Running on : ", torch.cuda.device_count(), " GPUs!"
if torch.cuda.device_count() > 1 else " GPU!")
else:
print("Running on CPU!")
# create the data directory
print('Current working directory: ', os.getcwd())
if os.path.exists('data'):
import shutil
shutil.rmtree('data')
print('Removed previously created data directory & contents')
os.mkdir('data')
print('New data directory created')
print(os.listdir())
# Join class labels vector onto features matrix ready for storage
Xy = np.block([[X, np.reshape(y, (len(y), 1))]])
# Method to store each sample under an enumerated file name
def store_as_torch_tensor(slc, num):
pt_array = torch.tensor(slc, dtype=torch.float)
torch.save(pt_array, 'data/id-{}.pt'.format(num))
# print(slc.size, 'data/id-{}.pt'.format(num), pt_array)
return num + 1
# Commence file name enumeration with....
ID = 1
# Save to disk
for row in Xy:
ID = store_as_torch_tensor(row, ID)
# Working with file NAMES of each sample
samples = []
# Walk the 'data' directory
for (dirpath, dirnames, filenames) in os.walk('data'):
samples = filenames
# Shuffle before splitting
shuffle(samples)
# Split dataset by percentage
train = samples[0: int(len(samples) * .75)]
valid = samples[int(len(samples) * .75):]
print('Training set: ', len(train))
print('Validation set: ', len(valid))
# Assign sample NAMES to partition dictionary
partition = {'train': train, 'validation': valid}
# print(partition)
# Assign label VALUES to labels dictionary
labels = {}
for sample in samples:
sample_tensor = torch.load('data/' + sample)
labels[sample] = int(sample_tensor[-1].item())
# print(labels)
class Dataset(data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, list_IDs, labels):
'Initialization'
self.labels = labels
self.list_IDs = list_IDs
def __len__(self):
'Denotes the total number of samples'
return len(self.list_IDs)
def __getitem__(self, index):
'Generates one sample of data'
# Select sample
ID = self.list_IDs[index]
# From file load the data for this sample
X = torch.load('data/' + ID)
# Slice off the labels column so that only features are assigned to X
X = X[:-1]
# Labels dictionary was loaded into memory earlier; here the
# file name is used as the key to retrieve the class label value
y = torch.tensor(self.labels[ID])
# Apply one-hot encoding to the class label
#y_ohe = torch.tensor([y])
y_ohe = y
#y_ohe = torch.nn.functional.one_hot(y, num_classes=1).float()
return X, y_ohe
# generate training and test data
# Parameters
params = {'batch_size': 60, # specify minibatchsize!
'shuffle': True,
'num_workers': 0}
params2 = {'batch_size': 20,
'shuffle': True,
'num_workers': 0}
params_full_training = {'batch_size': 600,
'shuffle': False,
'num_workers': 0}
params_full_validation = {'batch_size': 200,
'shuffle': False,
'num_workers': 0}
# Generators
training_set = Dataset(partition['train'], labels)
training_generator = data.DataLoader(
training_set, **params) # dataloader for training
training_generator_full = data.DataLoader(training_set, **params_full_training)
validation_set = Dataset(partition['validation'], labels)
validation_generator = data.DataLoader(
validation_set, **params2) # dataloader for test/validation
validation_generator_full = data.DataLoader(
validation_set, **params_full_validation)
def gen_spiral_dataset(batchsize,N, r0, circles):
'''
generates train and testdataloader for the spiral dataset. additionally it outputs also all input and output data needed to plot the decisionboundary of a model with this data on top.
Spiral points are 2d point which belong to one of the two possible spiral classes.
Args:
batchsize: (int) batchsize of the train (and test) dataloader
N: (int) number of data per class
r0: (\in [0,1]) indicates how close the spirals ar at the center. smaller values lead to more complex classification tasks.
circles (value>0): number of circles which each spiral makes (can also be non-interger)
Returns:
train_dataloader: pytorch dataloader for training (75% of data)
test_dataloader: pytorch dataloader for testing (25% of data)
data_X
data_y
'''
D = 2
K = 2
number = N
def spiral_xy(i,spiral_num, number, circles = circles, r0 = r0 ):
'''
'''
phi = torch.tensor(0.) # start value of phi
delta_phi = torch.tensor(2 * np.pi * circles / number) # increment which is added to phi for each new datapoint
phi= phi+i*delta_phi
r = r0 # initial radius
delta_r = circles / number
r = r +i* delta_r
x= r*spiral_num*torch.cos(phi)
y = r*spiral_num*torch.sin(phi)
return [x,y]
def spiral(spiral_num):
return [spiral_xy(i, spiral_num,number=N, circles=circles, r0=r0) for i in range(N)]
X = np.zeros((N*K, D))
y = np.zeros(N*K, dtype='uint8')
y[0:N] = np.zeros(N) # represents -1
y[N:] = np.ones(N)
X[0:N] = spiral(-1)
X[N:] = spiral(1)
data_X = X
data_y = y
# GPU or CPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if torch.cuda.device_count() > 0:
print("Running on : ", torch.cuda.device_count(), " GPUs!"
if torch.cuda.device_count() > 1 else " GPU!")
else:
print("Running on CPU!")
# create the data directory
print('Current working directory: ', os.getcwd())
if os.path.exists('data'):
import shutil
shutil.rmtree('data')
print('Removed previously created data directory & contents')
os.mkdir('data')
print('New data directory created')
print(os.listdir())
# Join class labels vector onto features matrix ready for storage
Xy = np.block([[X, np.reshape(y, (len(y), 1))]])
# Method to store each sample under an enumerated file name
def store_as_torch_tensor(slc, num):
pt_array = torch.tensor(slc, dtype=torch.float)
torch.save(pt_array, 'data/id-{}.pt'.format(num))
# print(slc.size, 'data/id-{}.pt'.format(num), pt_array)
return num + 1
# Commence file name enumeration with....
ID = 1
# Save to disk
for row in Xy:
ID = store_as_torch_tensor(row, ID)
# Working with file NAMES of each sample
samples = []
# Walk the 'data' directory
for (dirpath, dirnames, filenames) in os.walk('data'):
samples = filenames
# Shuffle before splitting
shuffle(samples)
# Split dataset by percentage
train = samples[0: int(len(samples) * .75)]
valid = samples[int(len(samples) * .75):]
print('Training set: ', len(train))
print('Validation set: ', len(valid))
# Assign sample NAMES to partition dictionary
partition = {'train': train, 'validation': valid}
# print(partition)
# Assign label VALUES to labels dictionary
labels = {}
for sample in samples:
sample_tensor = torch.load('data/' + sample)
labels[sample] = int(sample_tensor[-1].item())
# print(labels)
class Dataset(data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, list_IDs, labels):
'Initialization'
self.labels = labels
self.list_IDs = list_IDs
def __len__(self):
'Denotes the total number of samples'
return len(self.list_IDs)
def __getitem__(self, index):
'Generates one sample of data'
# Select sample
ID = self.list_IDs[index]
# From file load the data for this sample
X = torch.load('data/' + ID)
# Slice off the labels column so that only features are assigned to X
X = X[:-1]
# Labels dictionary was loaded into memory earlier; here the
# file name is used as the key to retrieve the class label value
y = torch.tensor(self.labels[ID])
# Apply one-hot encoding to the class label
#y_ohe = torch.tensor([y])
y_ohe = y
#y_ohe = torch.nn.functional.one_hot(y, num_classes=1).float()
return X, y_ohe
# generate training and test data
# Parameters
params = {'batch_size': batchsize, # specify minibatchsize!
'shuffle': True,
'num_workers': 0}
params2 = {'batch_size': int(batchsize/3),
'shuffle': True,
'num_workers': 0}
# Generators
training_set = Dataset(partition['train'], labels)
training_generator = data.DataLoader( training_set, **params)
validation_set = Dataset(partition['validation'], labels)
validation_generator = data.DataLoader(
validation_set, **params2) # dataloader for test/validation
return training_generator, validation_generator, data_X, data_y
def plot_decision_boundary(model, features, labels, save_plot=None):
'''
plots decisionboundary of the given model. This works only for models with 2-dimensional input!
Args:
model: pytorch model
features: torch tensor of model input data
labels: torch tensor of corresponding model output data
Example: features=torch.tensor([[1.,0.],[0.,1.],[-1.,0.],[0.,-1.]])
labels= torch.tensor([[0],[1],[1],[0]])
Out:
no return value. generates a plot.
'''
# Plot the decision boundary
# Determine grid range in x and y directions
x_min, x_max = features[:, 0].min()-1, features[:, 0].max()+1
y_min, y_max = features[:, 1].min()-1, features[:, 1].max()+1
# Set grid spacing parameter
spacing = min(x_max - x_min, y_max - y_min) / 500 # 250
# Create grid
XX, YY = np.meshgrid(np.arange(x_min, x_max, spacing),
np.arange(y_min, y_max, spacing))
# Concatenate data to match input
data = np.hstack((XX.ravel().reshape(-1, 1),
YY.ravel().reshape(-1, 1)))
# Pass data to predict method
data_t = torch.tensor(data, dtype=torch.float).to(device)
# Set model to evaluation mode
model.eval()
Z = model(data_t)
# print(Z.shape)
# Convert PyTorch tensor to NumPy for plotting.
# if both values are equal, the the first entry is the argmax, i.e. here 0
Z_cat = np.argmax(Z.detach().cpu().numpy(), axis=1)
Z_max_val = np.max(Z.detach().cpu().numpy(), axis=1)
# Z = Z.detach().cpu().numpy()[:,1] # displays values of output in y axis
Z_cat = Z_cat.reshape(XX.shape)
Z_max_val = Z_max_val.reshape(XX.shape)
# print(Z.shape)
# print(Z[0,:])
# print(Z[:,0])
# fig = plt.figure()
plt.contourf(XX, YY, Z_cat, cmap='gray', alpha=0.8) # plt.cm.Spectral
# sns.countplot(Z_max_val, hue = Z_cat)
plt.scatter(features[:, 0], features[:, 1],
c=labels, s=40, cmap=plt.cm.Spectral)
plt.xlim(XX.min(), XX.max())
plt.ylim(YY.min(), YY.max())
if save_plot is not None:
# save_plot provides the path
# plt.close()
plt.savefig(save_plot, bbox_inches='tight')
plt.close()
# os.unlink(save_plot)
else:
plt.show()
# fig.savefig('spiral_linear.png')
X_small = torch.tensor([[1., 0.], [0., 1.], [-1., 0.], [0., -1.]])
y_small = torch.tensor([[0], [1], [1], [0]])
def check_accuracy(model, x_data, y_data):
X_check = torch.tensor(x_data, dtype=torch.float32).to(device)
y_check = torch.tensor(y_data, dtype=torch.float32).to(device)
# Set model to evaluation mode
model.eval()
# Make predictions
predictions = model(X_check)
''' max_vals is a tensor of probability values.
arg_maxs is a tensor of the index locations at which
the maximum probability occured in the tensor.'''
(max_vals, arg_maxs) = torch.max(predictions.data, dim=1)
(y_max_vals, y_arg_maxs) = torch.max(y_check.data, dim=1)
# print('True Labels: {}'.format(y_arg_maxs))
# print('Predictions: {}'.format(arg_maxs))
# arg_maxs is tensor of indices [0, 1, 0, 2, 1, 1 . . ]
num_correct = torch.sum(y_arg_maxs == arg_maxs)
acc = (num_correct * 100.0 / len(y_data))
return acc.item()