-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (51 loc) · 2 KB
/
main.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
import torch
import time
import os
import json
from torchinfo import summary
from game import Connect4Game
from model import Connect4Model
from trainer import Trainer
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
args = {
'batch_size': 16, # Total number of training iterations
'num_simulations': 100, # Total number of MCTS simulations to run when deciding on a move to play
'numEps': 100, # Number of full games (episodes) to run during each iteration
'epochs': 10, # Number of epochs of training per iteration
'model_dir':'models_2022-07-14-22-52-07', # 'models_' + time.strftime("%Y-%m-%d-%H-%M-%S"), #
'start_iter':216,
'learning_rate':5e-4,
'batch_with_replacement':False,
'temperature':1,
'verbose':0,
'save_freq':1,
'benchmark_games':100,
'debug_mode':False,
'parallelize':True,
'force_win':False,
'force_block_win':False,
}
if args['debug_mode']:
args['model_dir'] = 'debug_models'
game = Connect4Game()
board_size = game.get_board_size()
action_size = game.get_action_size()
model = Connect4Model(board_size, action_size, device, game) # instantiate model
# make model directory
if not os.path.exists(args['model_dir']):
os.makedirs(args['model_dir'])
# save training args
tf = open(f"{args['model_dir']}\\args.json", "w")
json.dump(args,tf)
tf.close()
# write model summary to file
architecture_summary = summary(model, input_size=(args['batch_size'],3,6,7),verbose=0)
tf = open(f"{args['model_dir']}\\model_summary.txt", "w",encoding="utf-8")
tf.write(str(architecture_summary))
tf.close()
else:
latest_model_ind = max([int(m.split('_')[0]) for m in os.listdir(args['model_dir']) if m.endswith('.pth')])
model.load_state_dict(torch.load(f"{args['model_dir']}\\{latest_model_ind}_model.pth"))
# begin training
trainer = Trainer(game, model, args)
trainer.learn()