Skip to content

Commit 1c50814

Browse files
committed
added graph and additional test
1 parent d85f768 commit 1c50814

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

Summer20/NeuralNetwork/tfsin.py

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import random
66
import matplotlib.pyplot as plt
77

8-
number = 100
9-
108
# randomPoints on the sin graph
119
# number - number of points
1210
# angles - tuple for range of angles
@@ -24,38 +22,63 @@ def randomPoints(number, angles= (-360,360)):
2422
# only specific points on the sin graph
2523
# number - number of points
2624
# angles - target angle
27-
def selectPoints(number, angle=30):
25+
def selectPoints(number, angle=90):
2826
angleList = []
2927
sinList = []
28+
options = [angle+90,angle+180,angle+270,angle-90,angle-180,angle-270]
3029
while(number>0):
30+
angle = random.choice(options)
3131
radangle = angle * math.pi / 180 # convert to radians
3232
angleList.append([radangle])
3333
sinList.append([math.sin(radangle)])
34-
angleList.append([-radangle])
35-
sinList.append([math.sin(-radangle)])
36-
angle = angle + 60
3734
number = number -1
3835
return angleList, sinList
3936

40-
angles = (-360,360)
41-
angleList, sinList = randomPoints(10000, angles)
42-
#angleList, sinList = selectPoints(10000)
4337

38+
39+
# generate training data
40+
angles = (-360,360) # represents full system dynamics
41+
#angles = (0,90) # doesn't represent full system dynamics
42+
43+
angleList, sinList = randomPoints(10000, angles) # good number of points and random points
44+
#angleList, sinList = randomPoints(100, angles) # sparse points and random points
45+
#angleList, sinList = selectPoints(10000) # good number of points but not representative points
46+
47+
48+
49+
# neural network code
4450
model = models.Sequential()
4551
model.add(layers.Dense(10, activation='tanh', input_shape=(1,)))
4652
model.add(layers.Dense(1, activation=None))
4753
model.compile(optimizer='Adam',
4854
loss=losses.MeanSquaredError(),
4955
metrics=['mean_squared_error'])
56+
5057
history = model.fit(np.array(angleList),np.array(sinList), epochs=200)
51-
plt.plot(history.history['mean_squared_error'], label='mean_squared_error')
52-
plt.xlabel('Epoch')
53-
plt.ylabel('MSE')
54-
plt.ylim([0.0, 0.2])
55-
plt.legend(loc='lower right')
5658

59+
# plots out learning curve
60+
# plt.plot(history.history['mean_squared_error'], label='mean_squared_error')
61+
# plt.xlabel('Epoch')
62+
# plt.ylabel('MSE')
63+
# plt.ylim([0.0, 0.2])
64+
# plt.legend(loc='lower right')
65+
# plt.show()
66+
67+
# generate test data
5768
angleTest, sinTest = randomPoints(10)
5869
print(model.predict(np.array(angleTest)))
5970
print(sinTest)
6071

72+
73+
# graph of the test, train points
74+
graph = plt.figure()
75+
ax = graph.add_subplot(111)
76+
77+
x = np.linspace(-math.pi*2,math.pi*2,300)
78+
y = np.sin(x)
79+
80+
plt.plot(x,y, label= 'y = sin(x)', markersize = 2, c='c')
81+
ax.scatter(angleList,sinList, label = 'training', c='b')
82+
ax.scatter(angleTest,model.predict(np.array(angleTest)), label = 'testing', c='r')
83+
plt.legend(loc='lower right')
6184
plt.show()

0 commit comments

Comments
 (0)