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

Transfer learning: RL training using loaded reward model #81

Merged
merged 10 commits into from
Aug 20, 2019

Conversation

AdamGleave
Copy link
Member

This PR creates a new registry for reward function loaders, and adds support to expert_demos to load any reward function supported in this registry, wrapping the environment.

Specifically, we actually create two registries: one for RewardNet objects, and one for callables which take obs-act-obs triples and return rewards. The latter is a more general interface, and is what is needed by expert_demos. The former, RewardNet, is more constrained (it would exclude e.g. DiscrimNetGAIL) but could be useful in cases where one needs a TensorFlow reward model (e.g. for fine-tuning/further training).

@codecov
Copy link

codecov bot commented Aug 15, 2019

Codecov Report

Merging #81 into master will increase coverage by 0.58%.
The diff coverage is 98.51%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #81      +/-   ##
==========================================
+ Coverage   80.54%   81.13%   +0.58%     
==========================================
  Files          48       49       +1     
  Lines        2832     2920      +88     
==========================================
+ Hits         2281     2369      +88     
  Misses        551      551
Impacted Files Coverage Δ
tests/test_scripts.py 100% <ø> (ø) ⬆️
src/imitation/rewards/discrim_net.py 94.65% <ø> (ø) ⬆️
src/imitation/util/util.py 100% <100%> (ø) ⬆️
tests/test_reward_net.py 100% <100%> (ø) ⬆️
src/imitation/rewards/reward_net.py 90.52% <100%> (-0.1%) ⬇️
tests/test_reward_vec_env_wrapper.py 100% <100%> (ø) ⬆️
src/imitation/scripts/expert_demos.py 88.7% <100%> (ø) ⬆️
src/imitation/algorithms/density_baselines.py 97.77% <100%> (ø) ⬆️
tests/test_policies.py 100% <100%> (ø) ⬆️
src/imitation/scripts/config/expert_demos.py 68.85% <100%> (+1.05%) ⬆️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4058764...2d1f29f. Read the comment docs.

Copy link
Member

@shwang shwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Requested changes in and made some API suggestions for rewards.serialize.

src/imitation/scripts/expert_demos.py Show resolved Hide resolved
src/imitation/rewards/serialize.py Outdated Show resolved Hide resolved
assert shaped in ["True", "False"]
shaped = shaped == "True"

# TODO(adam): leaks session
Copy link
Member

@shwang shwang Aug 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(One dumb way to solve this would be to make load_reward into a contextmanager itself, which closes the session automatically on exit.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be OK with this, but it feels just like punting the problem elsewhere. At the current callsite in scripts/expert_demos.py:105 it seems like most the code needs the reward model. Probably we'd want to deallocate the session when the RewardVecEnvWrapper gets close(d)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree that reward_fn and its Session should be closed once venv = RewardVecEnvWrapper(venv, reward_fn) is no longer used (nearly at the end of the function).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(RewardVecWrapper wouldn't make a good contextmanager because it isn't guaranteed to hold a Session (and wouldn't have access to reward_fn's Session anyways).)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditionally setting up a context from reward_fn seems hard to do via a vanilla context + a no-op context switch.

venv = util.make_vec_env(env_name, num_vec, seed=_seed,
parallel=parallel, log_dir=log_dir)
if reward_type is not None:
reward_fn = load_reward(reward_type, reward_path, venv)
venv = RewardVecEnvWrapper(venv, reward_fn)
tf.logging.info(
f"Wrapped env in reward {reward_type} from {reward_path}.")
vec_normalize = None
if normalize:
venv = vec_normalize = VecNormalize(venv)

Copy link
Member

@shwang shwang Aug 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this might be a good use case for contextlib.ExitStack.

Something like

with ExitStack() as exit_stack:
  ...
  if reward_type is not None:
    reward_fn, resources = load_reward(...)   # type: Tuple[RewardFn, List["Implements .close()"]]
    for resource in resources: 
      exit_stack.push(contextlib.closing(resource))
    venv = RewardVecEnvWrapper(venv, reward_fn)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ! I'd never seen ExitStack. I'm going to address this in a separate PR since I want to change things for policies/serialize.py as well.

src/imitation/rewards/serialize.py Outdated Show resolved Hide resolved
src/imitation/util/registry.py Outdated Show resolved Hide resolved
src/imitation/rewards/serialize.py Outdated Show resolved Hide resolved
src/imitation/rewards/serialize.py Outdated Show resolved Hide resolved
src/imitation/rewards/serialize.py Outdated Show resolved Hide resolved
src/imitation/rewards/serialize.py Outdated Show resolved Hide resolved
src/imitation/rewards/serialize.py Outdated Show resolved Hide resolved
@AdamGleave AdamGleave requested a review from shwang August 16, 2019 09:18
@AdamGleave
Copy link
Member Author

Python type annotations for Callable do not support keyword arguments annoyingly, and pytype also has trouble with if-else branches leading to callables of different types, so I've: (a) made the steps argument mandatory (and adjusted existing reward functions to ignore it) and (b) made it a positional argument not a keyword argument.

Copy link
Member

@shwang shwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AdamGleave AdamGleave merged commit 0a79409 into master Aug 20, 2019
@AdamGleave AdamGleave deleted the reward-registry branch August 20, 2019 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants