-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNN.py
170 lines (142 loc) · 6.05 KB
/
NN.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
import os
import numpy
import sklearn.model_selection as sk
from tensorflow.keras.layers import Conv2D, Dense, Flatten
from tensorflow.keras.models import Sequential, load_model
from tensorflow.python.keras import losses
from BaseROM import BaseROM
from Node import Node
class NN:
rom: BaseROM = None
model = None
x = []
y = []
x_test = []
x_train = []
y_test = []
y_train = []
def __init__(self, rom: BaseROM):
self.rom = rom
def build_model(self):
self.model = Sequential([
Conv2D(84, (8, 8), strides=4, input_shape=(84, 84, 4)),
Conv2D(32, (4, 4), strides=2),
Flatten(),
Dense(256)
])
self.add_output_layer()
self.model.compile(optimizer='adam',
loss=losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
def add_output_layer(self):
self.model.add(Dense(len(self.rom.actions()), activation='linear'))
def build_data_from_root(self, rootNode: Node):
return
def predict(self, frames):
frames = numpy.asarray(frames)
frames = frames.reshape((1, 84, 84, 4))
return self.model.predict(frames)
def split_data(self):
self.x = numpy.asarray(self.x)
self.y = numpy.asarray(self.y)
self.x = self.x.reshape((self.x.shape[0], 84, 84, 4))
self.x_train, self.x_test, self.y_train, self.y_test = sk.train_test_split(self.x, self.y, test_size=.1)
def train(self):
self.model.fit(self.x_train, self.y_train, epochs=10)
def test(self):
return self.model.evaluate(self.x_test, self.y_test)
def save_model(self):
self.model.save(f"models/{self.name()}.{self.rom.name}.h5", save_format="tf")
def load_model(self):
if os.path.exists(f"models/{self.name()}.{self.rom.name}.h5"):
self.model = load_model(f"models/{self.name()}.{self.rom.name}.h5")
return True
return False
def name(self):
return "NN"
class ClassificationNN(NN):
def add_output_layer(self):
self.model.add(Dense(len(self.rom.actions()), activation='softmax'))
def name(self):
return "ClassificationNN"
def build_data_from_root(self, rootNode: Node):
frames = numpy.asarray(rootNode.last_four_frames())
self.x.append(frames.reshape((84, 84, 4)))
bestChild = rootNode.selectBestChild()
if bestChild is None:
bestChild = rootNode.selectRandomAction()
self.y.append(bestChild.action)
for child in [child for child in rootNode.children.values() if not child.isLeaf()]:
self.build_data_from_root(child)
class RegressionNN(NN):
def add_output_layer(self):
self.model.add(Dense(len(self.rom.actions()), activation='linear'))
def name(self):
return "RegressionNN"
def split_data(self):
self.x = numpy.asarray(self.x)
self.y = numpy.asarray(self.y)
self.x = self.x.reshape(self.x.shape[0], 84, 84, 4)
self.x_train, self.x_test, self.y_train, self.y_test = sk.train_test_split(self.x, self.y, test_size=.1)
def build_data_from_root(self, rootNode: Node):
frames = numpy.asarray(rootNode.last_four_frames())
self.x.append(frames.reshape((84, 84, 4)))
bestChild = rootNode.selectBestChild()
if bestChild is None:
bestChild = rootNode.selectRandomAction()
self.y.append(bestChild.selectWeight)
for child in [child for child in rootNode.children.values() if not child.isLeaf()]:
self.build_data_from_root(child)
class HybridNN(ClassificationNN):
x_trains = []
x_tests = []
y_trains = []
y_tests = []
def name(self):
return "HybridNN"
def split_data(self):
self.x = numpy.asarray(self.x)
self.y = numpy.asarray(self.y)
self.x = self.x.reshape((self.x.shape[0], 84, 84, 4))
xLen = int(self.x.shape[0]/4)
yLen = int(self.y.shape[0]/4)
x_train, x_test, y_train, y_test = sk.train_test_split(self.x[0:xLen,...], self.y[0:yLen,...], test_size=.1)
self.x_trains.append(x_train)
self.x_tests.append(x_test)
self.y_trains.append(y_train)
self.y_tests.append(y_test)
def train(self):
self.model.fit(self.x_trains[0], self.y_trains[0], epochs=10)
xLen = int(self.x.shape[0]/4)
yLen = int(self.y.shape[0]/4)
for i in range(0, int(xLen)):
pred = self.predict(self.x[i].tolist()).argmax()
self.y[i] = pred
x_train, x_test, y_train, y_test = sk.train_test_split(self.x[xLen:2*xLen,...], self.y[yLen:2*yLen,...], test_size=.1)
self.x_trains.append(x_train)
self.x_tests.append(x_test)
self.y_trains.append(y_train)
self.y_tests.append(y_test)
self.model.fit(self.x_trains[1], self.y_trains[1], epochs=10)
for i in range(int(xLen), int(2*xLen)):
pred = self.predict(self.x[i].tolist()).argmax()
self.y[i] = pred
x_train, x_test, y_train, y_test = sk.train_test_split(self.x[xLen*2:3*xLen,...], self.y[yLen*2:3*yLen,...], test_size=.1)
self.x_trains.append(x_train)
self.x_tests.append(x_test)
self.y_trains.append(y_train)
self.y_tests.append(y_test)
self.model.fit(self.x_trains[2], self.y_trains[2], epochs=10)
for i in range(int(2*xLen), int(3*xLen)):
pred = self.predict(self.x[i].tolist()).argmax()
self.y[i] = pred
x_train, x_test, y_train, y_test = sk.train_test_split(self.x[3*xLen:,...], self.y[3*yLen:,...], test_size=.1)
self.x_trains.append(x_train)
self.x_tests.append(x_test)
self.y_trains.append(y_train)
self.y_tests.append(y_test)
self.model.fit(self.x_trains[3], self.y_trains[3], epochs=10)
for i in range(int(3*xLen), self.x.shape[0]):
pred = self.predict(self.x[i].tolist()).argmax()
self.y[i] = pred
self.x_train, self.x_test, self.y_train, self.y_test = sk.train_test_split(self.x, self.y, test_size=.1)