-
Notifications
You must be signed in to change notification settings - Fork 0
/
functionExample.py
87 lines (72 loc) · 2.43 KB
/
functionExample.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
'''
This example is meant to demonstrate how you can map complex
functions using a single input and single output with polynomial
synaptic weights
'''
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import high_order_layers.PolynomialLayers as poly
import high_order_layers.FourierLayers as fourier
offset = -0.1
factor = 1.5 * 3.14159
xTest = np.arange(100) / 50 - 1.0
yTest = 0.5 * np.cos(factor * (xTest - offset))
xTrain = tf.random.uniform([1000], minval=-1.0, maxval=1, dtype=tf.float32)
yTrain = 0.5 * tf.math.cos(factor * (xTrain - offset))
'''
modelSetF = [
{'name': 'Fourier 1', 'func': poly.b1F},
{'name': 'Fourier 2', 'func': poly.b2F},
{'name': 'Fourier 3', 'func': poly.b3F},
{'name': 'Fourier 4', 'func': poly.b4F},
{'name': 'Fourier 5', 'func': poly.b5F}
]
'''
modelSetD = [
{'name': 'Discontinuous 1', 'func': poly.b1D},
{'name': 'Discontinuous 2', 'func': poly.b2D},
{'name': 'Discontinuous 3', 'func': poly.b3D},
{'name': 'Discontinuous 4', 'func': poly.b4D},
{'name': 'Discontinuous 5', 'func': poly.b5D}
]
modelSetC = [
{'name': 'Continuous 1', 'func': poly.b1C},
{'name': 'Continuous 2', 'func': poly.b2C},
{'name': 'Continuous 3', 'func': poly.b3C},
{'name': 'Continuous 4', 'func': poly.b4C},
{'name': 'Continuous 5', 'func': poly.b5C}
]
modelSet = [
{'name': '1st order', 'func': poly.b1},
{'name' : '2nd order', 'func' : poly.b2},
{'name': '3rd order', 'func': poly.b3},
{'name' : '4th order', 'func' : poly.b4},
{'name': '5th order', 'func': poly.b5}
]
colorIndex = ['red', 'green', 'blue', 'purple', 'black']
symbol = ['+', 'x', 'o', 'v', '.']
thisModelSet = modelSetC
for i in range(0, len(thisModelSet)):
model = tf.keras.models.Sequential([
poly.Polynomial(1, basis=thisModelSet[i]['func']),
#fourier.Fourier(1, frequencies=20),
])
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['mean_squared_error'])
model.fit(xTrain, yTrain, epochs=10, batch_size=1)
model.evaluate(xTrain, yTrain)
predictions = model.predict(xTest)
plt.scatter(
xTest,
predictions.flatten(),
c=colorIndex[i],
marker=symbol[i],
label=thisModelSet[i]['name'])
plt.plot(xTest, yTest, '-', label='actual', color='black')
plt.title('fourier synapse - no hidden layers')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()