training and evaluation stats are now logged by default to model/logs/

This commit is contained in:
Christoffer Müller Madsen 2018-03-10 00:39:55 +01:00
parent 9bc1a8ba9f
commit b07f075627

63
main.py
View File

@ -1,28 +1,35 @@
import argparse import argparse
import sys import sys
import os
import time import time
def print_train_outcome(outcome, trained_eps = 0): # Define helper functions
def log_train_outcome(outcome, trained_eps = 0):
format_vars = { 'trained_eps': trained_eps, format_vars = { 'trained_eps': trained_eps,
'count': len(train_outcome), 'count': len(train_outcome),
'sum': sum(train_outcome), 'sum': sum(train_outcome),
'mean': sum(train_outcome) / len(train_outcome), 'mean': sum(train_outcome) / len(train_outcome),
'time': int(time.time()) 'time': int(time.time())
} }
print("train;{time};{trained_eps};{count};{sum};{mean}".format(**format_vars)) with open(os.path.join(config['model_path'], 'logs', "train.log"), 'a+') as f:
f.write("{time};{trained_eps};{count};{sum};{mean}".format(**format_vars) + "\n")
def print_eval_outcomes(outcomes, trained_eps = 0): def log_eval_outcomes(outcomes, trained_eps = 0):
for outcome in outcomes: for outcome in outcomes:
scores = outcome[1] scores = outcome[1]
format_vars = { 'trained_eps': trained_eps, format_vars = { 'trained_eps': trained_eps,
'method': outcome[0], 'method': outcome[0],
'count': len(scores), 'count': len(scores),
'sum': sum(scores), 'sum': sum(scores),
'mean': sum(scores) / len(scores), 'mean': sum(scores) / len(scores),
'time': int(time.time()) 'time': int(time.time())
} }
print("eval;{time};{method};{trained_eps};{count};{sum};{mean}".format(**format_vars)) with open(os.path.join(config['model_path'], 'logs', "eval.log"), 'a+') as f:
f.write("{time};{method};{trained_eps};{count};{sum};{mean}".format(**format_vars) + "\n")
# Parse command line arguments
parser = argparse.ArgumentParser(description="Backgammon games") parser = argparse.ArgumentParser(description="Backgammon games")
parser.add_argument('--episodes', action='store', dest='episode_count', parser.add_argument('--episodes', action='store', dest='episode_count',
type=int, default=1000, type=int, default=1000,
@ -37,6 +44,8 @@ parser.add_argument('--eval', action='store_true',
help='whether to evaluate the neural network with a random choice bot') help='whether to evaluate the neural network with a random choice bot')
parser.add_argument('--train', action='store_true', parser.add_argument('--train', action='store_true',
help='whether to train the neural network') help='whether to train the neural network')
parser.add_argument('--eval-after-train', action='store_true', dest='eval_after_train',
help='whether to evaluate after each training session')
parser.add_argument('--play', action='store_true', parser.add_argument('--play', action='store_true',
help='whether to play with the neural network') help='whether to play with the neural network')
parser.add_argument('--start-episode', action='store', dest='start_episode', parser.add_argument('--start-episode', action='store', dest='start_episode',
@ -52,33 +61,43 @@ config = {
'train': args.train, 'train': args.train,
'play': args.play, 'play': args.play,
'eval': args.eval, 'eval': args.eval,
'eval_after_train': args.eval_after_train,
'start_episode': args.start_episode 'start_episode': args.start_episode
} }
#print("-"*30) # Make sure directories exist
#print(type(args.eval_methods)) model_path = os.path.join(config['model_path'])
#print(args.eval_methods) log_path = os.path.join(model_path, 'logs')
#print("-"*30) if not os.path.isdir(model_path):
os.mkdir(model_path)
if not os.path.isdir(log_path):
os.mkdir(log_path)
# Set up game
import game import game
g = game.Game(config = config) g = game.Game(config = config)
g.set_up_bots() g.set_up_bots()
episode_count = args.episode_count
# Set up variables
episode_count = config['episode_count']
# Do actions specified by command-line
if args.train: if args.train:
eps = config['start_episode'] eps = config['start_episode']
while True: while True:
train_outcome = g.train_model(episodes = episode_count, trained_eps = eps) train_outcome = g.train_model(episodes = episode_count, trained_eps = eps)
eps += episode_count eps += episode_count
print_train_outcome(train_outcome, trained_eps = eps) log_train_outcome(train_outcome, trained_eps = eps)
if args.eval: if config['eval_after_train']:
eval_outcomes = g.eval(trained_eps = eps) eval_outcomes = g.eval(trained_eps = eps)
print_eval_outcomes(eval_outcomes, trained_eps = eps) log_eval_outcomes(eval_outcomes, trained_eps = eps)
sys.stdout.flush()
elif args.eval: elif args.eval:
eps = config['start_episode']
outcomes = g.eval() outcomes = g.eval()
print_eval_outcomes(outcomes, trained_eps = 0) log_eval_outcomes(outcomes, trained_eps = eps)
#elif args.play: #elif args.play:
# g.play(episodes = episode_count) # g.play(episodes = episode_count)