train_model.py 3.04 KB
Newer Older
1
import os
Enzo DURAND's avatar
Enzo DURAND committed
2
import sys
3
import numpy as np
Enzo DURAND's avatar
Enzo DURAND committed
4
5
import warnings
warnings.filterwarnings("ignore")
6
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 
Enzo DURAND's avatar
Enzo DURAND committed
7
sys.path.append(os.getcwd()+"/src_python")
8
9


10
11
12
13
from settings.config import MODEL_PATH, N_RES_LAYER, LEARNING_RATE, MOMENTUM, REG_CONST, BATCH_SIZE, N_EPOCHS, VERBOSE, VALIDATION_SPLIT
from settings.game_settings import GAME_NAME, N_ROW, N_COL, N_ACTION_STACK
from brain.model import CustomModel
from utils import load_data, get_random_sample, load_nn
Enzo DURAND's avatar
Enzo DURAND committed
14
15

	
Enzo DURAND's avatar
Enzo DURAND committed
16
######### Training model from loaded data and saving weights #########
17

Enzo DURAND's avatar
Enzo DURAND committed
18
if __name__ == '__main__':
hanzopgp's avatar
hanzopgp committed
19
20
	X, y_values, y_opp_values, y_distrib = load_data()
	X, y_values, y_opp_values, y_distrib = get_random_sample(X, y_values, y_opp_values, y_distrib)
Enzo DURAND's avatar
Enzo DURAND committed
21
	X = X.astype("float32")
hanzopgp's avatar
fix bug    
hanzopgp committed
22
	y = {"value_head": y_values.astype("float32"), "value_opp_head": y_opp_values.astype("float32"), "policy_head": y_distrib.reshape(y_distrib.shape[0], -1).astype("float32")} 
Enzo DURAND's avatar
Enzo DURAND committed
23

Enzo DURAND's avatar
Enzo DURAND committed
24
25
	champion_path = MODEL_PATH+GAME_NAME+"_"+"champion"+".h5"
	outsider_path = MODEL_PATH+GAME_NAME+"_"+"outsider"+".h5"
Enzo DURAND's avatar
Enzo DURAND committed
26

Enzo DURAND's avatar
Enzo DURAND committed
27
28
	# If there is an outsider, always train it because we are in the case
	# of re-training since there is both a champion and an outsider
29
	if os.path.exists(outsider_path): 
Enzo DURAND's avatar
Enzo DURAND committed
30
		model_type = "outsider"
Enzo DURAND's avatar
Enzo DURAND committed
31
		print("--> Found an outsider, re-training it")
32
33
34
35
36
37
38
		model = CustomModel(
			input_dim=X[0].shape, 
			output_dim=N_ROW*N_COL*N_ACTION_STACK, # this is the policy head output dim	 
			n_res_layer=N_RES_LAYER, 
			learning_rate=LEARNING_RATE, 
			momentum=MOMENTUM, 
			reg_const=REG_CONST)
Enzo DURAND's avatar
Enzo DURAND committed
39
40
41
		model.set_model(load_nn(model_type="outsider", inference=False))
	# Else if there is no outsider but there is a champion, we are at 2nd step 
	# and we create the outsider model with the champion as a baseline
42
	elif os.path.exists(champion_path):
Enzo DURAND's avatar
Enzo DURAND committed
43
44
45
46
		model_type = "outsider"
		print("--> Found a champion model, creating an outsider")
		model = CustomModel(
			input_dim=X[0].shape, 
47
			output_dim=N_ROW*N_COL*N_ACTION_STACK,	 
Enzo DURAND's avatar
Enzo DURAND committed
48
49
50
51
			n_res_layer=N_RES_LAYER, 
			learning_rate=LEARNING_RATE, 
			momentum=MOMENTUM, 
			reg_const=REG_CONST)
Enzo DURAND's avatar
Enzo DURAND committed
52
53
54
		model.set_model(load_nn(model_type="champion", inference=False))
	# Else if there is no model at all, we are at first step and we create the 
	# champion model from scratch
Enzo DURAND's avatar
Enzo DURAND committed
55
56
57
58
59
	else:
		model_type = "champion"
		print("--> No model found, creating the champion model")
		model = CustomModel(
			input_dim=X[0].shape, 
60
			output_dim=N_ROW*N_COL*N_ACTION_STACK,	 
Enzo DURAND's avatar
Enzo DURAND committed
61
62
63
64
65
66
			n_res_layer=N_RES_LAYER, 
			learning_rate=LEARNING_RATE, 
			momentum=MOMENTUM, 
			reg_const=REG_CONST)
		model.build_model()
		
Enzo DURAND's avatar
Enzo DURAND committed
67
	print("\n")
Enzo DURAND's avatar
Enzo DURAND committed
68
69
70
71
	history = model.fit(
		X=X, 
		y=y, 
		batch_size=BATCH_SIZE, 
72
		n_epochs=N_EPOCHS, 
Enzo DURAND's avatar
Enzo DURAND committed
73
74
		verbose=VERBOSE, 
		validation_split=VALIDATION_SPLIT)
Enzo DURAND's avatar
Enzo DURAND committed
75
76
	
	print(model.predict(np.expand_dims(X[0], axis=0)))		
Enzo DURAND's avatar
Enzo DURAND committed
77
	#model.plot_metrics(history)
78

Enzo DURAND's avatar
Enzo DURAND committed
79
80
81
82
83
84
	# If it is the first step, then there is no model yet and our
	# first model will be a champion since there is no other
	# competitor yet. If there is a champion then our model will
	# be an outsider and will need to fight later against the
	# champion to become the champion.
	model.write(model_type)
Enzo DURAND's avatar
Enzo DURAND committed
85

Enzo DURAND's avatar
Enzo DURAND committed
86
87