-
Notifications
You must be signed in to change notification settings - Fork 0
/
AZTrain.py
61 lines (48 loc) · 1.71 KB
/
AZTrain.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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
# this seems like a terrible idea, but it's faster for some reason
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
import typing
import tensorflow.keras as keras
import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
from az_method.MonteCarloTreeSearch import MCTS
from az_method.ReinfLearn import ReinfLearn
from tqdm import tqdm
import numpy as np
from tensorflow.keras.models import Model
RUNS = 100
GAMES_PER_RUN = 50
ROLLOUTS = 50
CURRENT_ITERATION = 0
starting_model_path = "az_models/random_model.keras"
# starting_model_path = f"az_models/model_{CURRENT_ITERATION}.keras"
CURRENT_ITERATION += 1
model: "Model" = typing.cast(
Model, keras.models.load_model(starting_model_path))
mcts_seacher = MCTS(model)
learner = ReinfLearn(model)
for training_run in range(RUNS):
print(f"Training run {CURRENT_ITERATION+training_run}")
print(f"Running with {ROLLOUTS} rollouts per move, ")
print(f"and {GAMES_PER_RUN} games per training run.")
print(f"Starting model: {starting_model_path}")
all_states = []
all_actions = []
all_values = []
for j in tqdm(range(0, GAMES_PER_RUN)):
states, actions, values = learner.play_game(ROLLOUTS)
all_states += states
all_actions += actions
all_values += values
np_states = np.array(all_states)
np_actions = np.array(all_actions)
np_values = np.array(all_values)
model.fit(
x=np_states,
y=[np_actions, np_values],
epochs=64,
batch_size=16
)
# if (CURRENT_ITERATION+training_run) % 5 == 0:
model.save(f"az_models/model_{CURRENT_ITERATION+training_run}.keras")