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

VecEnc Support TD3 #495

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

acyclics
Copy link

@acyclics acyclics commented Oct 2, 2019

Implemented SubprocVecEnv for TD3.

Related: #452 #170

@acyclics
Copy link
Author

acyclics commented Oct 2, 2019

Ah, it seems like I would first have to make HER work with VecEnv. @araffin Any idea where to begin with that?

@@ -473,3 +456,55 @@ def save(self, save_path, cloudpickle=False):
params_to_save = self.get_parameters()

self._save_to_file(save_path, data=data, params=params_to_save, cloudpickle=cloudpickle)


class Runner(AbstractEnvRunner):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do you need a runner? It seems that you only need to save the obs variable.

Copy link
Author

Choose a reason for hiding this comment

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

I noticed that other implementations that uses VecEnv use a Runner. I used a Runner here as I feel like that best enables future developers to build on top of it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

PPO and A2C use a runner because some additional computation/transformations are needed (computation of the GAE notably) which is not the case of TD3 who only need to fill a replay buffer.
However, at some point, we will need to refactor and unify SAC/DDPG/TD3 which have a lot in common.

Copy link
Author

Choose a reason for hiding this comment

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

Ah cool. So perhaps some akin to a "runner" for all three?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not really, more a common method collect_rollout that would be part of the OffPolicy class, but this is not the subject of this PR.

stable_baselines/td3/td3.py Outdated Show resolved Hide resolved
@araffin araffin changed the title Update td3.py VecEnc Support TD3 Oct 3, 2019
else:
action = self.policy_tf.step(obs[None]).flatten()
action = self.policy_tf.step(prev_obs).flatten()
action = [np.array([a]) for a in action]
Copy link
Collaborator

Choose a reason for hiding this comment

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

why not removing the flatten instead?

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good

else:
action = self.policy_tf.step(obs[None]).flatten()
action = self.policy_tf.step(prev_obs).flatten()
action = [np.array([a]) for a in action]
# Add noise to the action, as the policy
# is deterministic, this is required for exploration
if self.action_noise is not None:
action = np.clip(action + self.action_noise(), -1, 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The noise should be different for each env

episode_rewards[-1] += reward[i]
if done[i]:
if self.action_noise is not None:
self.action_noise.reset()
Copy link
Collaborator

Choose a reason for hiding this comment

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

same remark as before, I think you should have a action_noise object per env, maybe we need to create wrapper for that or modify the noise class to handle it better

if step % self.train_freq == 0:
mb_infos_vals = []
# Update policy, critics and target networks
for grad_step in range(self.gradient_steps):
Copy link
Collaborator

Choose a reason for hiding this comment

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

By putting the update inside the for loop that is used to store new samples, it seems that you are changing the algorithm
Also be careful with step % train_freq when you don't increment step by 1

Copy link
Author

Choose a reason for hiding this comment

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

Ok, I will look into the algorithm part. As for the step % train_freq, I actually do increment step by 1 (at the end of the inner for-loop) so that should be fine.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok then, but the for step in range(0, total_timesteps, self.n_envs): was misleading

Copy link
Author

@acyclics acyclics Oct 3, 2019

Choose a reason for hiding this comment

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

I see your point. Alright, I will clarify that for-loop expression.

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.

None yet

2 participants