ml-agents icon indicating copy to clipboard operation
ml-agents copied to clipboard

Looking for general advise after a hack-week project using ml-agents

Open ChrisCF opened this issue 8 years ago • 9 comments

We had a Hack week in our office just before Easter and my team tried out ml-agents in one of our games. This was our first adventure in machine learning so we were working off the documentation, a bit of theoretical knowledge from some other articles and presentations. We ended up with a number of (possibly quite naive) questions I was hoping someone might be able to help us out with. (skip to the last paragraph for the tldr; questions) The middle section is the details of what we tried. (Note: the various imgur images are animated gifs that are too big to embed here).

The game we chose to try this on was Ninja Tobu. It's a 2D mobile game where the user controls a ninja by swiping across the screen to make the ninja jump. The aim is either to reach a goal at the end of the level or, in endless mode, to climb as high as possible through an infinite level. There are various spiked surfaces and NPCs it's not safe to land on. The sections of the infinite mode vary in numbers of hazards and there is a set of spikes below you that force you to keep climbing nearly constantly. It's not the simplest game we had available but we wanted to push things a bit.

Step one was refactoring some of our code so we could try imitation learning and running multiple agents at once (we did this before our hack week started - tech debt reduction).

Then at the start of the week we setup an initial set of observations, actions and rewards and tried letting the agent go at it in an empty level. We were hoping that, in this basic situation, it would fairly quickly get to the point where it could climb forever so we could start adding in some obstacles.

Observations: Characters x/y velocity (normalised based on max possible character velocity) and 8 raycasts evenly spaced radiating out around the character each producing: x/y velocity (normalised) of thing hit, vector to the thing from the character (normalised relative to max raycast distance) and a 0/1 value for whether it was something the character could land on or not. image

Rewards: Penalty for dying of -1. Penalty for being lower than previous height -0.005. Reward proportional to height gained, 1 for gaining the max possible height for a movement.

Action Vector: Mouse down/up (bool mouseButtonDown = mouseAction > 0.5f;) X Screen Position (normalised 0-1) Y Screen Position (normalised 0-1)

We basically gave the agent the exact input method a player would use. I was going to call this our first issue, but it did seem to be figuring it out, it was just taking a long time to do so. The following gif is after ~4 hours (left side), ~8 hours (right side) https://imgur.com/MZkF9Wn You might notice some mid air direction changes, this is because ninjas are allowed a single mid-air jump between each jump from a surface. The right image is actually the best double jumping we saw all week. But this rate of progress was a bit slow for our tastes. 8 hours and the agent wouldn't even have been capable of reaching the first obstacle in a normal level.

So we started working towards imitation learning and playing around with rewards in the first version while we were making those changes. We simplified the rewards. -1 for dying, +0.1 for moving and +0.01 for getting higher; and we tried more complex observations to see if they would help. None of these changes made a big difference to the first version.

Once we got imitation learning setup we immediately ran into more problems related to the agents actions. https://imgur.com/9vnK600 The agent really wasn't getting the touch up/down. So we decided to modify the input system so that the agent was dealing with jump vectors. x/y normalised strength, anything under abs(0.2) was treated as no jump. 1 was the biggest jump possible. (Was trying to encode jump/no jump information like this OK?) This worked much better and the agent was quickly able to understand how to jump. This worked for standalone reinforcement learning as well with the agent learning the basics of movement in a couple of minutes rather than over several hours. https://imgur.com/gfu9NP6 Progress! Given the faster training of the reinforcement method with these changes we ended up focusing on that, because, we thought that was more interesting than the imitation learning and it was going much faster with the changes. So it seemed viable.

Reinforcement learning now kept getting us rapidly stuck with the agent wall running and then, sitting stuck in corners. wallbug This turned out to be an issue with the game. It's possible to swipe upwards and towards a wall you are hanging on and do a tiny hop up the wall. The agent kept deciding that these micro-hops were the best way to climb, but when it hit an overhang it would get stuck and just sit there till the spikes caught up and killed it. Huge caveat here, our longest tests were probably only 2-3 hours, and a lot of the time we were stopping and making changes after 30 minutes when we started seeing the same wall hugging behaviour. Later experience suggested that we maybe weren't giving the agent enough time to overcome these situations.

So we fixed the wall hugging and this led to much better behaviour. We went through a day of short training runs, playing with hyper parameters and trying various combinations of observations and rewards again. I think this day was mostly wasted given how little time we were training each iteration for. At the end of that day things were looking like a bit of a bust, with presentations on projects due the next day. We setup several different agents running with different configs and left them overnight. In the morning after about 15 hours we had the closest thing to success we saw all week (for 2 of the setups). These particular setups had the simplified reward set with default hyper parameters running in simplified environments. One was a completely empty environment and one was using 3 easy level chunks with basic obstacles and no lethal hazards (The best agent: https://imgur.com/a/lyFSt) Tensorboard: image

Interestingly, this best case agent became terrible about 30 minutes later and is you can see below it was generally all over the show. allovertheplace

The things that stuck out for me as potential problem indicators (based off reading the ml-agents docs) were: Entropy increasing and info/policy loss magnitude averaging fairly similar levels (to my eyeball). (Plus the erratic cumulative reward and value estimate.) But after the time we'd already spent trying to tweak hyper parameters and seeing every change making things worse (except increasing the hidden_units: this seemed to produce the same results but faster) we weren't sure how to proceed to improve the training and we were out of time (we were only running 30 minute runs with modified hyper parameters. Perhaps not enough to see a positive difference?)

Some other things we tried along the way: A normalised jump count. 0, 0.5, 1. Indicating whether the agent was resting, on it's first jump or on its 3rd jump. Should this have been encoded in a one-hot fashion? What if more jumps were allowed (there is a power-up in the full game that provides extra air jumps) Stacking observations - This went very poorly for some reason we didn't have time to investigate. With a few sets of stacked observations the Agent ended up jumping once towards a wall and then just sitting there, very occasionally he'd jump back towards the start point after getting to the wall. Both courses of action are fatal in a few seconds. Doubling the number of ray casts. Recurrent, though we didn't expect that to work well given the use of a continuous action space (and it didn't). We wanted to build up a curriculum and slowly add more difficult levels and features so the agent could learn to play the full game. But we ended up a fair way off getting to that point. We didn't look at camera based observations because that seemed like it would be much more complicated and therefore slower to train(?)

Questions:

  1. Are their any rules of thumb around how long training should take given the size of the observation and action spaces? And therefore were our shorter 30 minute runs a waste of time?
  2. The 15+ hour training run we did seems like a long time when it takes example projects much less time to train and some of them have more observations than we do. Does this indicate a problem or is it due to the continuous action space or something else?
  3. Is there such a thing as a guide to troubleshooting for PPO training using tensorboard? (Beyond the documentation provided in this repo)
  4. In the likely event that there isn't. Can anyone recommend good resources for more introductory level information on ML, preferably with a focus on game agents rather than image classification or other areas? Or is it all just a dark art? The docs were a good starting point, but our conclusion after a few days using ml-agents is that we need significantly more base knowledge to be able to get value out of it in the kind of real world use cases we have (Maybe our expectations are too high). We just can't tell if our fundamental observations and rewards are OK and the hyper parameters need work or the other way around or if something totally different is going on or if we are doing everything horribly wrong.
  5. Any suggestions about how to proceed based on the last tensorboard image (or any other info I've provided) would be great. We're still keen to improve things when we get time.

ChrisCF avatar Apr 10 '18 06:04 ChrisCF

I can see your entropy rising too quickly try lowering the beta value, keep in mind this will also lower your agents chance of exploring, so it's a trade-off between how complex your problem is. I can also see the episode lengths seem to be directly correlated to cumulative-reward or vice-versa, this looks a little strange.

superjayman avatar Apr 10 '18 07:04 superjayman

The comment above mentions the episode length. In my experience it is extremely important to have a a short episode length. My tests by adding difficulty the longer the agents survives where not successful (they mostly get stuck in some local minima like you experienced some) so there is mostly no reason to have them alive for that long.

(what i mean with this is that you probably should add a curriculum to train the basic movement and then put him in a level with obstacles, but never have 100 meter without obstacles and then add suddenly have him face obstacles. You know its like playing dark souls with perma death. you waste so much time to get to the point where you could try to learn something new)

So what i would do is to reset the agent when he doesnt improve his height over the last x seconds, and always reset him after a max of ~500 steps (probably depends on the complexity of the problem i think). A small trick to decrease the episode length: change the fixed time steps per second (my current training's are with 10 physic ticks per second) ,or increase the decision frequency. (i havent tried this but it should work similar to reducing the physic ticks, but without changing the game itself)

I also think your game might be a bit prone to local minima (it might be to easy to beat with just going left and right)

Hengoo avatar Apr 10 '18 09:04 Hengoo

Thanks for the suggestions.

RE: Episode length. The comments are really interesting. We looked at it as "it's an infinite running scenario so it should just be allowed to go as far as possible". So we didn't have a limit on the number of steps an agent would take. We only reset when the agent died. From what you're saying that sounds like a mistake. I'll put a limit in as suggested and run some more training when I get a chance.

RE: Beta. The fact that entropy was climbing rather than going downwards threw us a bit. The docs mentioned beta in regards to the rate entropy was falling. I realise the rate of fall could be negative and therefore climbing but we kind of fixated on the fact we expected it to be falling. We thought the docs might mean it wasn't expected to go up normally ever. So we weren't sure if increasing meant something else was totally wrong or if we should adjust Beta. We'll look at that after seeing how changing the step limit goes.

The level chunks we used were all very simple, so left/right is generally enough to beat them. So i'm not totally surprised the agent settles towards that in these training sessions. Our assumption with regard to that was that if we switched to more complicated chunks later through a curriculum the agent would be able to adapt and learn to deal with more complicated movement requirements.

ChrisCF avatar Apr 10 '18 21:04 ChrisCF

Made some progress: image

The above is with the episode length capped at 500 and beta adjusted (have tweaked it a few times and then left running for 24 hours, this is the first one where entropy didn't start trending up again at some point). This was run over the weekend.

Should entropy go all the way down to zero by the end of training or should it just be trending down during training? And what constitutes "too fast/slow" as far as the rate of change goes?

ChrisCF avatar Apr 15 '18 21:04 ChrisCF

Hello ChrisCF,

what did you do to tweak the entropy? I'm having the same issues where entropy is increasing and am looking for some advice. Did you increase it or decrease it?

Edit: It seems that I made the mistake of setting beta as 1e2 instead of 1e-2

Although, even when I changed the beta the entropy continued to rise

MichaelTSakai avatar Apr 18 '18 01:04 MichaelTSakai

I started at 5.0e-3 and decreased it a couple of times ending up around 1.0e-5

ChrisCF avatar Apr 20 '18 00:04 ChrisCF

In the docs This corresponds to how random the decisions of a brain are. This should consistently decrease during training. If it decreases too soon or not at all, beta should be adjusted (when using discrete action space). But you are using continuous action space

r-lipton avatar May 04 '18 08:05 r-lipton

Folks, this discussion has been super interesting but it has also been inactive for a while. We are closing this issue due to inactivity. Feel free to reopen it if you’d like to continue to discussion though.

eshvk avatar Jun 14 '18 19:06 eshvk

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 03 '20 09:01 lock[bot]