Spaces:
Sleeping
Sleeping
File size: 2,533 Bytes
4c2a92d 44db2f9 4c2a92d 570282c 44db2f9 a777e34 44db2f9 350e00d 4c2a92d f899dd3 4c2a92d f899dd3 4c2a92d 1bd428f 62c6c3b 44db2f9 350e00d 1bd428f 4c2a92d 1bd428f 4c2a92d |
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 63 64 65 66 67 68 69 70 71 |
#!/usr/bin/env python3
import argparse
import gym
import os
import sys
import time
import matplotlib.pyplot as plt
from a3c.train import train
from a3c.eval import evaluate, evaluate_checkpoints
from wordle_env.wordle import WordleEnvBase
def training_mode(args, env, model_checkpoint_dir):
max_ep = args.games
start_time = time.time()
if args.model_name:
pretrained_model_path = os.path.join(
model_checkpoint_dir, args.model_name)
global_ep, win_ep, gnet, res = train(
env, max_ep, model_checkpoint_dir, args.gamma, pretrained_model_path)
else:
global_ep, win_ep, gnet, res = train(env, max_ep, model_checkpoint_dir, args.gamma)
print("--- %.0f seconds ---" % (time.time() - start_time))
print_results(global_ep, win_ep, res)
evaluate(gnet, env)
def evaluation_mode(args, env, model_checkpoint_dir):
print("Evaluation mode")
results = evaluate_checkpoints(model_checkpoint_dir, env)
print(results)
def print_results(global_ep, win_ep, res):
print("Jugadas:", global_ep.value)
print("Ganadas:", win_ep.value)
plt.plot(res)
plt.ylabel('Moving average ep reward')
plt.xlabel('Step')
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"enviroment", help="Enviroment (type of wordle game) used for training, example: WordleEnvFull-v0")
parser.add_argument(
"--models_dir", help="Directory where models are saved (default=checkpoints)", default='checkpoints')
subparsers = parser.add_subparsers(help='sub-command help')
parser_train = subparsers.add_parser(
'train', help='Train a model from scratch or train from pretrained model')
parser_train.add_argument(
"--games", "-g", help="Number of games to train", type=int, required=True)
parser_train.add_argument(
"--model_name", "-n", help="If want to train from a pretrained model, the name of the pretrained model file")
parser_train.add_argument(
"--gamma", help="Gamma hyperparameter value", type=float, default=0.)
parser_train.set_defaults(func=training_mode)
parser_eval = subparsers.add_parser(
'eval', help='Evaluate saved models for the enviroment')
parser_eval.set_defaults(func=evaluation_mode)
args = parser.parse_args()
env_id = args.enviroment
env = gym.make(env_id)
model_checkpoint_dir = os.path.join(args.models_dir, env.unwrapped.spec.id)
args.func(args, env, model_checkpoint_dir)
|