2018-03-08 15:27:16 +00:00
|
|
|
import argparse
|
2018-03-08 16:13:25 +00:00
|
|
|
import sys
|
2018-03-09 23:39:55 +00:00
|
|
|
import os
|
2018-03-09 20:05:38 +00:00
|
|
|
import time
|
2018-03-08 15:27:16 +00:00
|
|
|
|
2018-03-12 14:18:44 +00:00
|
|
|
model_storage_path = 'models'
|
2018-03-11 23:11:55 +00:00
|
|
|
|
|
|
|
# Create models folder
|
2018-03-12 14:18:44 +00:00
|
|
|
if not os.path.exists(model_storage_path):
|
|
|
|
os.makedirs(model_storage_path)
|
2018-03-11 23:11:55 +00:00
|
|
|
|
2018-03-09 23:39:55 +00:00
|
|
|
# Define helper functions
|
|
|
|
def log_train_outcome(outcome, trained_eps = 0):
|
2018-03-09 20:05:38 +00:00
|
|
|
format_vars = { 'trained_eps': trained_eps,
|
2018-03-08 15:27:16 +00:00
|
|
|
'count': len(train_outcome),
|
|
|
|
'sum': sum(train_outcome),
|
2018-03-09 20:05:38 +00:00
|
|
|
'mean': sum(train_outcome) / len(train_outcome),
|
|
|
|
'time': int(time.time())
|
|
|
|
}
|
2018-03-09 23:39:55 +00:00
|
|
|
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")
|
|
|
|
|
2018-03-08 15:27:16 +00:00
|
|
|
|
2018-03-09 23:39:55 +00:00
|
|
|
def log_eval_outcomes(outcomes, trained_eps = 0):
|
2018-03-08 16:51:32 +00:00
|
|
|
for outcome in outcomes:
|
2018-03-09 23:39:55 +00:00
|
|
|
scores = outcome[1]
|
|
|
|
format_vars = { 'trained_eps': trained_eps,
|
|
|
|
'method': outcome[0],
|
|
|
|
'count': len(scores),
|
|
|
|
'sum': sum(scores),
|
|
|
|
'mean': sum(scores) / len(scores),
|
|
|
|
'time': int(time.time())
|
|
|
|
}
|
|
|
|
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")
|
2018-03-08 15:27:16 +00:00
|
|
|
|
2018-03-09 23:39:55 +00:00
|
|
|
|
|
|
|
# Parse command line arguments
|
2018-03-08 15:27:16 +00:00
|
|
|
parser = argparse.ArgumentParser(description="Backgammon games")
|
|
|
|
parser.add_argument('--episodes', action='store', dest='episode_count',
|
|
|
|
type=int, default=1000,
|
|
|
|
help='number of episodes to train')
|
2018-03-11 23:11:55 +00:00
|
|
|
parser.add_argument('--model', action='store', dest='model',
|
|
|
|
default='default',
|
|
|
|
help='name of Tensorflow model to use')
|
2018-03-08 15:27:16 +00:00
|
|
|
parser.add_argument('--eval-methods', action='store',
|
|
|
|
default=['random'], nargs='*',
|
|
|
|
help='specifies evaluation methods')
|
|
|
|
parser.add_argument('--eval', action='store_true',
|
|
|
|
help='whether to evaluate the neural network with a random choice bot')
|
|
|
|
parser.add_argument('--train', action='store_true',
|
|
|
|
help='whether to train the neural network')
|
2018-03-09 23:39:55 +00:00
|
|
|
parser.add_argument('--eval-after-train', action='store_true', dest='eval_after_train',
|
|
|
|
help='whether to evaluate after each training session')
|
2018-03-08 15:27:16 +00:00
|
|
|
parser.add_argument('--play', action='store_true',
|
|
|
|
help='whether to play with the neural network')
|
2018-03-09 23:22:20 +00:00
|
|
|
parser.add_argument('--start-episode', action='store', dest='start_episode',
|
|
|
|
type=int, default=0,
|
|
|
|
help='episode count to start at; purely for display purposes')
|
2018-03-12 14:18:44 +00:00
|
|
|
parser.add_argument('--train-perpetually', action='store_true',
|
|
|
|
help='start new training session as soon as the previous is finished')
|
|
|
|
parser.add_argument('--list-models', action='store_true',
|
|
|
|
help='list all known models')
|
2018-03-08 15:27:16 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
config = {
|
2018-03-20 12:03:21 +00:00
|
|
|
'model': args.model,
|
2018-03-12 14:18:44 +00:00
|
|
|
'model_path': os.path.join(model_storage_path, args.model),
|
2018-03-08 15:27:16 +00:00
|
|
|
'episode_count': args.episode_count,
|
|
|
|
'eval_methods': args.eval_methods,
|
|
|
|
'train': args.train,
|
|
|
|
'play': args.play,
|
2018-03-09 23:22:20 +00:00
|
|
|
'eval': args.eval,
|
2018-03-09 23:39:55 +00:00
|
|
|
'eval_after_train': args.eval_after_train,
|
2018-03-12 14:18:44 +00:00
|
|
|
'start_episode': args.start_episode,
|
2018-03-14 19:42:09 +00:00
|
|
|
'train_perpetually': args.train_perpetually,
|
|
|
|
'model_storage_path': model_storage_path
|
2018-03-08 15:27:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 23:39:55 +00:00
|
|
|
# Make sure directories exist
|
|
|
|
model_path = os.path.join(config['model_path'])
|
|
|
|
log_path = os.path.join(model_path, 'logs')
|
|
|
|
if not os.path.isdir(model_path):
|
|
|
|
os.mkdir(model_path)
|
|
|
|
if not os.path.isdir(log_path):
|
|
|
|
os.mkdir(log_path)
|
|
|
|
|
2018-03-08 15:27:16 +00:00
|
|
|
|
2018-03-20 12:03:21 +00:00
|
|
|
# Set up network
|
|
|
|
from network import Network
|
2018-03-08 15:27:16 +00:00
|
|
|
|
|
|
|
|
2018-03-09 23:39:55 +00:00
|
|
|
# Set up variables
|
|
|
|
episode_count = config['episode_count']
|
|
|
|
|
|
|
|
|
|
|
|
# Do actions specified by command-line
|
2018-03-12 14:18:44 +00:00
|
|
|
if args.list_models:
|
|
|
|
def get_eps_trained(folder):
|
|
|
|
with open(os.path.join(folder, 'episodes_trained'), 'r') as f:
|
|
|
|
return int(f.read())
|
|
|
|
model_folders = [ f.path
|
|
|
|
for f
|
|
|
|
in os.scandir(model_storage_path)
|
|
|
|
if f.is_dir() ]
|
|
|
|
models = [ (folder, get_eps_trained(folder)) for folder in model_folders ]
|
|
|
|
sys.stderr.write("Found {} model(s)\n".format(len(models)))
|
|
|
|
for model in models:
|
|
|
|
sys.stderr.write(" {name}: {eps_trained}\n".format(name = model[0], eps_trained = model[1]))
|
|
|
|
|
|
|
|
elif args.train:
|
2018-03-20 12:03:21 +00:00
|
|
|
network = Network(config, config['model'])
|
2018-03-09 23:22:20 +00:00
|
|
|
eps = config['start_episode']
|
2018-03-08 15:27:16 +00:00
|
|
|
while True:
|
2018-03-20 12:03:21 +00:00
|
|
|
train_outcome = network.train_model(episodes = episode_count, trained_eps = eps)
|
2018-03-08 15:27:16 +00:00
|
|
|
eps += episode_count
|
2018-03-09 23:39:55 +00:00
|
|
|
log_train_outcome(train_outcome, trained_eps = eps)
|
|
|
|
if config['eval_after_train']:
|
2018-03-09 20:05:38 +00:00
|
|
|
eval_outcomes = g.eval(trained_eps = eps)
|
2018-03-09 23:39:55 +00:00
|
|
|
log_eval_outcomes(eval_outcomes, trained_eps = eps)
|
2018-03-12 14:18:44 +00:00
|
|
|
if not config['train_perpetually']:
|
|
|
|
break
|
2018-03-08 15:27:16 +00:00
|
|
|
elif args.eval:
|
2018-03-09 23:39:55 +00:00
|
|
|
eps = config['start_episode']
|
2018-03-08 15:27:16 +00:00
|
|
|
outcomes = g.eval()
|
2018-03-09 23:39:55 +00:00
|
|
|
log_eval_outcomes(outcomes, trained_eps = eps)
|
2018-03-08 15:27:16 +00:00
|
|
|
#elif args.play:
|
|
|
|
# g.play(episodes = episode_count)
|
|
|
|
|