Skip to content
This repository was archived by the owner on May 28, 2024. It is now read-only.
This repository was archived by the owner on May 28, 2024. It is now read-only.

PyTorch Port #28

Closed
Closed

Description

@Kaixhin

Thread to discuss PyTorch port of PlaNet.

Firstly, I've already fixed the RNN bug here. Some things I haven't checked thoroughly are the sizes of layers/activations within the neural networks and the max transition memory size (currently 500k). I think there is probably space to parallelise some ops, will need to look into it (overshooting is the main overhead, going from O(L) with the standard KL objective to O(L^2)). I've also just switched all ReLUs to ELUs, but the results I'll post in this comment are with ReLUs (note these are incomplete because I killed runs early).

Finger Spin (can't remember if no overshooting or not but shouldn't affect results much anyway)
rewards

Walker Walk (no overshooting)
rewards

Walker Walk
rewards

Activity

  1. danijar commented on Apr 23, 2019

    @danijar
    Contributor

    Thanks for sharing! Comparing your two walker plots, it seems like they are quite similar in terms of performance up to the 350 episodes that the first experiment ran for. Are these training curves or testing curves (i.e. with or without action noise)?

  2. Kaixhin commented on Apr 23, 2019

    @Kaixhin
    Author

    These are training curves for a single agent/seed (I'm currently not running any test episodes - raised an issue for that: Kaixhin/PlaNet#1). In a way roughly mirrored the plots you had so my instinct was that things were probably working. Only have 1 GPU with > 4GB RAM for this and other work so unfortunately can't test much.

    FYI currently moving for loops from the main code into the model. Not nearly as clean, but the only way to try and optimise things if at all.

  3. danijar commented on Apr 24, 2019

    @danijar
    Contributor

    Maybe it gets to similar performance when training on shorter sequences but larger batch sizes. This could speed up the code quite a bit. With the fixed RSSM that you've already implemented, PlaNet now also works without overshooting and without global prior. While it would be nice to keep overshooting as it still helps in some cases, it might be a lot faster to disable it by default if it's still working well. By the way, are you encoder and decoding all images of the current chunk at once or separately for each time step? This can make a big difference.

  4. Kaixhin commented on Apr 24, 2019

    @Kaixhin
    Author

    Yes - shorter sequences and no overshooting would definitely speed things up in general. I also suspect that reducing the overshooting length could be reduced even if enabled - have you done any experiments on this (e.g., at the chunk length it's far longer than the planning length)? Over a year ago I tried to use a Deep Kalman Filter to do model-based RL with long time dependencies and the prior's predictions just collapsed to the mode, so overshooting sounded like a potential solution to this problem I had.

    My current code does encoding and decoding separately per timestep, but the version I'm working on now will do it for a chunk at once.

  5. piojanu commented on Apr 26, 2019

    @piojanu

    @Kaixhin amazing work! I have a question, what does it mean: "the prior's predictions just collapsed to the mode"? Specifically, what do you understand by "mode"? Could you post some references where I could read more on the topic?

  6. Kaixhin commented on Apr 26, 2019

    @Kaixhin
    Author

    @piojanu thank you :) It's a bit off-topic but briefly I did some work on MBRL with latent space planning called DELIP, and in some experiments that I didn't get to work reliably I was trying to predict trajectories of about 100 timesteps in the future. In this case the trajectories happened to fall into a couple of different clusters, and the learned model predicted the most common ones but completely missed ones in the smaller clusters. Well, I did train some models to get it right, but I wanted to find a more reliable setup before publishing results. Unfortunately missed the window to do so but hoping I could pick up something similar again using PlaNet as a strong starting point.

  7. Kaixhin commented on Apr 27, 2019

    @Kaixhin
    Author

    Update on my refactor - tqdm is predicting about 1 week (I said the previous version was about 2 weeks, but that was an upper bound). Had a look at GPU comparisons - this one is giving a small multiplier on using a V100 vs. a 1080Ti, so with a dedicated headless server and a better GPU this timing should be a bit less too. Changes include:

    • Running with MUJOCO_GL=egl (not sure this makes much difference for me though)
    • Moving the Python for loop over sequences inside the model forward pass (no speedup to be expected here)
    • Encoding and decoding (and predicting rewards) batched over sequences (significant but not huge speedup)
    • Removed saving images during data collection (obvious speedup); replaced with video writing for first test episode
    • Separate test phase (no action noise) every 25 episodes, with 10 episodes each (slowdown, but necessary)

    Started a run with Walker Walk with default hyperparams, fingers crossed it replicates what you have.

  8. piojanu commented on Apr 27, 2019

    @piojanu

    @Kaixhin thanks for explanation, now I get it 👌 The paper though looks great. I'm especially interested in how you adopted the Monte Carlo planner to learned model, because I'll do something similar in PlaNet in next steps! I'll read it for sure, thanks 😄

  9. Kaixhin commented on Apr 28, 2019

    @Kaixhin
    Author

    @danijar just checking a few details on the losses. In general I try to sum over elements (e.g. MSE loss on pixels, KL loss on 30D Gaussian), average over batch, sum over time, but perhaps this is a bit off.

  10. danijar commented on Apr 29, 2019

    @danijar
    Contributor

    Exactly, losses are summed across the feature dimension and averaged across batch and time. Your summary of free nats sounds correct to me.

  11. Kaixhin commented on Apr 29, 2019

    @Kaixhin
    Author

    Going back to the loss in the paper (looked at #19, where it seems like the implementation details follow the paper but not explicitly so):

    loss

    All losses (observation NLL + reward NLL + KL) should actually be summed over time? On top of this the KL losses are normalised by the maximum chunk length (so 1/D factor) - not sure I can pick up from the paper whether the global KL should also be normalised by this as well? Though with β1 = D this normalisation is effectively undone for the standard KL loss (β>1 = 1 means no change for the overshooting part).

  12. danijar commented on May 1, 2019

    @danijar
    Contributor

    Yes, all losses are averaged over both batch and time dimensions, including the global divergence.

  13. Kaixhin commented on May 1, 2019

    @Kaixhin
    Author

    Sorry for not being clear - I've tried to write up the loss informally to check - let me know if this matches what you have:

    latex_5979855b34f96b3506b14e97367211a9

    1. Everything operates over time and batches, and is also averaged over these dimensions (equivalent to a sum and everything having 1/T and 1/B scaling; rightmost brackets missing for some reason).
    2. MSE loss on observations involves summing over channel, height and width dimensions.
    3. MSE loss on rewards (no extra dimension here - just scalars).
    4. Summation over latent dimension for KL losses (implementation detail as we actually calculate the KL independently per dimension, and this allows the free nats technique).
    5. A 1/D scaling on the KL losses (standard + overshooting), but not global.
    6. β1 (= 50) x standard 1-step KL losses with 2 free nats.
    7. Telescoping over the overshooting distance D, β>1 (= 1) x overshooting KL losses with 2 free nats and gradients stopped for the posterior samples.
    8. βg (= 0.1) x global KL loss (two right brackets missing here).
  14. 14 remaining items

  15. Kaixhin commented on May 21, 2019

    @Kaixhin
    Author

    I've got some results and pretrained models in a release, with the rest to come. Everything looks good, except ball_in_cup-catch is pretty high variance. Something I forgot was to add compression to 5 bits (I used 8 bits) - so I can try this. Any idea how much difference this might make?

    Edit: Also, is there some sort of exploration schedule?

  16. piojanu commented on May 23, 2019

    @piojanu

    @Kaixhin there is code for it e.g. here it's applied:

    scale *= self._config.exploration.schedule(self._step)

    It's not used as you can see (exploration_ramps is 0):
    def linear(step, ramp):
    # https://www.desmos.com/calculator/q9egmfx3xr
    if ramp == 0:
    result = tf.constant(1, tf.float32)

  17. danijar commented on May 24, 2019

    @danijar
    Contributor

    @Kaixhin The plots looks great! I've also heard several people using your implementation of PlaNet already, so it seem to have a good impact. As @piojanu says (you have a really good idea of the code now haha!) I'm not using any schedules, neither for learning rate nor for exploration. The features are in the code though and I've played around with them at some point. Do you want to add the figures on the release page for completeness?

  18. Kaixhin commented on May 25, 2019

    @Kaixhin
    Author

    Yep great to hear that people have been using my port 😄 I'll add the figures on the release page once I've done a final check on whether those are the settings that should be used (see below).

    Still looking at ball_in_cup-catch. Added 5-bit quantisation (including random uniform noise for the appropriate bin size; also centred the data by adding -0.5 just to finish making preprocessing the same). Unfortunately results look even worse (below without and with the learning rate schedule you originally had). There's a chance that I've messed something up with experience replay storage (because I'm quantising back to bytes for smaller storage), but I'm just going to revert back to the simple 8-bit version without noise and rerun to see if I get something like the previous results, which were at least roughly in the right area even if the variance on that env is very high.

    newplot
    newplot (1)

  19. danijar commented on May 25, 2019

    @danijar
    Contributor

    I see. I'm plotting the median rather than the mean in the PlaNet paper, because that's what the dm_control paper does. In general, the median makes sense to me but it can be problematic for tasks with basically binary reward like ball in cup. However, the max values in your plots are definitely too low. Are you giving the agent 3 free nats and is this without overshooting?

    If you want to try the preprocessing, it should be easy to check whether it's correct by applying the inverse transform and looking at the resulting images. If they look good, and the preprocessed values are in range -0.5 to 0.5, then it's likely to be correct.

  20. Kaixhin commented on May 27, 2019

    @Kaixhin
    Author

    Yes these results are from using the camera ready hyperparameters. I think there was an issue in my discretisation for saving observations in experience replay memory (I had preprocess fine, but not postprocess). Might be OK now (seems to learn quickly, but the policy performance is still a bit unstable)?

    newplot

  21. danijar commented on May 27, 2019

    @danijar
    Contributor

    It might be fine, although it's still a bit surprising. There might be a difference in how we read the data, or neural network initialization etc because of difference between PyTorch and TensorFlow. If you can run multiple seeds, I'd try plotting the median aggregating over seeds and multiple episodes. That way, we'll have comparable figures. But since the performance matches quite closely on the other tasks I wouldn't worry too much about it.

  22. Kaixhin commented on May 30, 2019

    @Kaixhin
    Author

    I've considered network initialisation, but for this I doubt it would be a big factor if you're not doing something custom. PyTorch initialises both weights and biases with something similar to Kaiming init, and I believe TensorFlow uses truncated normal for weights and zeroed biases; I'm not sure it's worth implementing truncated normal for PyTorch.

    I've added median plotting (dark blue line) and tried a different seed and it looks alright. Median and mean seem to track each other pretty well for this. Getting more experiments on the other domains to make sure that performance is similar, so fingers crossed.
    newplot

  23. danijar commented on Jun 22, 2019

    @danijar
    Contributor

    Sorry for the late reply. How many episodes/seeds is the median taken over? It's surprising that the median is somewhat close to 500 because I'd expect the agent to either get the ball in (close to 1000) or not (close to 0). So the median should be close to one of these values most of the time, if aggregating over enough episodes/seeds. On the other hand, as I said earlier, I wouldn't worry too much about this, since the performance looks very nice on the other tasks.

  24. Kaixhin commented on Jun 22, 2019

    @Kaixhin
    Author

    This is just 1 seed (don't have resources to run any more experiments on this sadly). Stats are over 10 evaluation episodes, and I've seen many where some miss once or twice but do get it in eventually. In the releases I've noted performance replication.

    As you said, this hopefully isn't indicative of any problems given that all the other results look fine, so I think I'll close this issue up here. Thanks for all the help!

  25. danijar commented on Jun 23, 2019

    @danijar
    Contributor

    Sounds good, thanks for the great work!

  26. mctigger commented on Jul 9, 2020

    @mctigger

    Hey @Kaixhin
    I am wondering if you were able to reproduce the results from the paper with your implementation? If I understand the graphs correctly, the results in the paper indicate much faster convergence than the PyTorch port?

  27. Kaixhin commented on Jul 9, 2020

    @Kaixhin
    Author

    @mctigger from release 1.2, cartpole-swingup, reacher-easy, finger-spin and walker-walk are about the same, though ball_in_cup-catch is a bit unstable and cheetah-run is slow. The latest version of the code now includes action clipping (Kaixhin/PlaNet#22), which wasn't specified in the paper but is used in this codebase, so with that it should ideally be comparable (but I don't have resources to test atm).

  28. mctigger commented on Jul 9, 2020

    @mctigger

    Thank you for the quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions