This repository was archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_classification.py
More file actions
83 lines (66 loc) · 2.76 KB
/
Copy pathbasic_classification.py
File metadata and controls
83 lines (66 loc) · 2.76 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
import numpy as np
import matplotlib.pyplot as plt
from layers import *
from models import *
from optimizers import *
"""
Basic example of a classification task with toy data.
Adapted from 'https://cs.stanford.edu/people/karpathy/cs231nfiles/minimal_net.html'
"""
def generate_data(num_examples, input_dimensions, output_classes):
X = np.zeros((num_examples*output_classes, input_dimensions)) # data matrix (each row = single example)
y = np.zeros(num_examples*output_classes, dtype='uint8') # class labels
for j in range(output_classes):
ix = range(num_examples*j, num_examples*(j+1))
r = np.linspace(0.0, 1, N) # radius
t = np.linspace(j*4, (j+1)*4, N) + np.random.randn(num_examples)*0.2 # theta
X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]
y[ix] = j
return X, y
if __name__ == '__main__':
random_seed = 7
np.random.seed(random_seed)
N = 100 # number of points per class
D = 2 # dimensionality
K = 3 # number of classes
X, y = generate_data(N, D, K)
# Define the network
input_size, hidden_size, output_size = D, 100, K
model = Model(loss='crossentropy')
layer1 = Dense(input_size=input_size, output_size=hidden_size,
activation='relu', initializer='xavier', regularization=1e-3)
model.add_layer(layer1)
layer2 = Dense(input_size=hidden_size, output_size=output_size,
activation='linear', initializer='xavier', regularization=1e-3)
model.add_layer(layer2)
optim = SGD(lr=1e-0)
model.add_optim(optim)
# Training
for i in range(10000):
# Training the network and observing the loss
loss, acc = model.train(X, y)
# Print loss every 1000 iteration
if i % 1000 == 0:
print(f"Iteration {i}: loss {loss}, accuracy {acc}")
model.set_eval()
# evaluate training set accuracy
prediction = model(X)
predicted_class = np.argmax(prediction, axis=1)
print('Training accuracy: %.2f' % (np.mean(predicted_class == y)))
# Plotting true values and prediction
# Creating the results and the decision boundaries
h = 0.02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
prediction = model(np.c_[xx.ravel(), yy.ravel()])
prediction = np.argmax(prediction, axis=1)
prediction = prediction.reshape(xx.shape)
# Generating the plot
fig = plt.figure()
plt.contourf(xx, yy, prediction, cmap=plt.cm.Spectral, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.show()
#fig.savefig('results/basic_classification_result.png')