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

Vec discount fix #27

Merged
merged 4 commits into from
Feb 27, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* Bugfix when using `td.discount` with replays coming from vectorized environments (@galatolofederico)
* env.action_size and env.state_size when the number of vectorized environments is 1. (thanks @galatolofederico)
* Actor-critic integration test being to finicky.
* `cherry.onehot` support for numpy's float and integer types. (thanks @ngoby)
2 changes: 1 addition & 1 deletion cherry/td.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def discount(gamma, rewards, dones, bootstrap=0.0):

msg = 'dones and rewards must have equal length.'
assert rewards.size(0) == dones.size(0), msg
R = th.zeros_like(rewards[0]) + bootstrap
R = th.zeros_like(rewards) + bootstrap
discounted = th.zeros_like(rewards)
length = discounted.size(0)
for t in reversed(range(length)):
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/rl_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
TAU = 0.9
NUM_SAMPLES = 10
VECTOR_SIZE = 5
TIME_STEPS = 10
NUM_ENVS = 4

"""
TODO: Should test each method to make sure that they properly handle different
Expand Down Expand Up @@ -61,6 +63,52 @@ def setUp(self):

def tearDown(self):
pass


def test_vectorized_discount(self):
state = th.randn(TIME_STEPS, NUM_ENVS, VECTOR_SIZE)
action = th.randn(TIME_STEPS, NUM_ENVS)
reward = th.randn(TIME_STEPS, NUM_ENVS)
boostrap = th.randn(NUM_ENVS)
done = th.zeros_like(reward)
for i in list(reversed(range(TIME_STEPS)))[:4]:
done[i,i%NUM_ENVS] = 1


# Computing the discounted rewards
# as non-vectorized environment
nonvec_discounted_rewards = []
for i in range(NUM_ENVS):
replay = ch.ExperienceReplay()
for t in range(TIME_STEPS):
replay.append(
state[t, i, :], action[t, i],
reward[t, i], state[t, i, :], done[t, i]
)
nonvec_discounted_rewards.append(
ch.td.discount(
GAMMA, replay.reward(), replay.done(), boostrap[i]
)
)
# Computing the discounted rewards
# as vectorized environment
replay = ch.ExperienceReplay()
for t in range(TIME_STEPS):
replay.append(
state[t, :, :], action[t, :],
reward[t, :], state[t, :, :], done[t, :]
)
vec_discounted_rewards = ch.td.discount(
GAMMA, replay.reward(), replay.done(), boostrap
)

for i in range(NUM_ENVS):
assert th.all(
nonvec_discounted_rewards[i][:, 0]
==
vec_discounted_rewards[:, i],
)


def test_discount(self):
vector = th.randn(VECTOR_SIZE)
Expand Down