forked from pmuens/alphago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
end_to_end.py
37 lines (28 loc) · 1.24 KB
/
end_to_end.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
import h5py
from keras.models import Sequential
from keras.layers import Dense
from dlgo.agent.predict import DeepLearningAgent, load_prediction_agent
from dlgo.data.parallel_processor import GoDataProcessor
from dlgo.encoders.sevenplane import SevenPlaneEncoder
from dlgo.httpfrontend import get_web_app
from dlgo.networks import large
go_board_rows, go_board_cols = 19, 19
nb_classes = go_board_rows * go_board_cols
encoder = SevenPlaneEncoder((go_board_rows, go_board_cols))
processor = GoDataProcessor(encoder=encoder.name())
X, y = processor.load_go_data(num_samples=100)
input_shape = (encoder.num_planes, go_board_rows, go_board_cols)
model = Sequential()
network_layers = large.layers(input_shape)
for layer in network_layers:
model.add(layer)
model.add(Dense(nb_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
model.fit(X, y, batch_size=128, epochs=20, verbose=1)
deep_learning_bot = DeepLearningAgent(model, encoder)
with h5py.File('./agents/deep_bot.h5', 'w') as outf:
deep_learning_bot.serialize(outf)
model_file = h5py.File('./agents/deep_bot.h5', 'r')
bot_from_file = load_prediction_agent(model_file)
web_app = get_web_app({ 'predict': bot_from_file })
web_app.run()