Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/training loops #83

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cares_reinforcement_learning/util/EnvironmentFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ def __init__(self, args) -> None:
logging.info(f"Training on Domain {args['domain']}")
logging.info(f"Training with Task {args['task']}")

self.env = suite.load(args['domain'], args['task'], task_kwargs={'random': args['seed']})
self.domain = args['domain']
self.task = args['task']
self.env = suite.load(self.domain, self.task, task_kwargs={'random': args['seed']})

@cached_property
def min_action_value(self):
Expand All @@ -129,7 +131,7 @@ def action_num(self):
return self.env.action_spec().shape[0]

def set_seed(self, seed):
self.env = suite.load(self.env.domain, self.env.task, task_kwargs={'random': seed})
self.env = suite.load(self.domain, self.task, task_kwargs={'random': seed})

def reset(self):
time_step = self.env.reset()
Expand Down
15 changes: 9 additions & 6 deletions example/example_training_loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import random
import numpy as np
from pathlib import Path
from datetime import datetime

def set_seed(seed):
torch.manual_seed(seed)
Expand Down Expand Up @@ -62,16 +63,18 @@ def main():

logging.info(f"Memory: {args['memory']}")

seed = args['seed']
iterations_folder = f"{args['algorithm']}-{args['task']}-{datetime.now().strftime('%y_%m_%d_%H:%M:%S')}"
glob_log_dir = f'{Path.home()}/cares_rl_logs/{iterations_folder}'

training_iterations = args['number_training_iterations']
for training_iteration in range(0, training_iterations):
logging.info(f"Training iteration {training_iteration+1}/{training_iterations} with Seed: {seed}")
set_seed(seed)
env.set_seed(seed)
logging.info(f"Training iteration {training_iteration+1}/{training_iterations} with Seed: {args['seed']}")
set_seed(args['seed'])
env.set_seed(args['seed'])

#create the record class - standardised results tracking
record = Record(network=agent, config={'args': args})
log_dir = args['seed']
record = Record(glob_log_dir=glob_log_dir, log_dir=log_dir, network=agent, config={'args': args})

# Train the policy or value based approach
if args["algorithm"] == "PPO":
Expand All @@ -82,7 +85,7 @@ def main():
vbe.value_based_train(env, agent, memory, record, args)
else:
raise ValueError(f"Agent type is unkown: {agent.type}")
seed += 10
args['seed'] += 10

record.save()

Expand Down