-
Notifications
You must be signed in to change notification settings - Fork 5
/
circles.py
65 lines (51 loc) · 1.98 KB
/
circles.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
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import Merge
from keras.optimizers import SGD
from vis_tools import progress_plot
import numpy as np
def generate_circle_data(radii, numPoints, maxPerterbutation):
''' Create training cases for a circle configuration
Points are drawn randomly from circles of different radii with each being
assigned to a different class
The points will be perturbed randomly from the circle from which they are
drawn
'''
# begin with an empty array for inputs and classes
X = []
Y = []
for i in range(numPoints):
# Select a class at random
outClass = np.random.randint(0, len(radii))
# Select an angle at random
angle = np.random.rand() * 2 * np.pi
pertubations = maxPerterbutation * (np.random.rand(2) - 0.5)
X.append(radii[outClass] * np.array([np.cos(angle) + pertubations[0],
np.sin(angle) + pertubations[1]]))
y = np.zeros(len(radii))
y[outClass] = 1
Y.append(y)
return (np.array(X), np.array(Y))
radii = [1, 2, 3]
numPoints = 200
maxPerterbutation = 0.5
# Create the neural network model
model = Sequential([
Dense(10, input_dim=2),
Activation('sigmoid'),
Dense(10),
Activation('sigmoid'),
Dense(len(radii)),
Activation('softmax')])
# Compile the model with an optimiser
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='binary_crossentropy')
X, Y = generate_circle_data(radii, numPoints, maxPerterbutation)
# Create a map of points to plot the function stored in the neural network
bottomLeft = [-(max(radii) + 1), -(max(radii) + 1)]
topRight = [max(radii) + 1, max(radii) + 1]
# Create a plot to visualise the progress of training
epochList = [10, 20, 30, 40, 50, 70, 100, 200, 500]
batchSize = 10
progress_plot(model, X, Y, bottomLeft, topRight, epochList, batchSize,
verbose=True)