|
| 1 | +# Training with Proximal Policy Optimization(PPO) |
| 2 | + |
| 3 | +PPO is a popular reinforcement learning algorithm. See [this paper](https://arxiv.org/abs/1707.06347) for details. |
| 4 | + |
| 5 | +Here, we are only going to tell how to use our existing code to train your ML-Agent environment in editor. |
| 6 | + |
| 7 | +The example [Getting Started with the 3D Balance Ball Environment](Getting-Started-with-Balance-Ball.md) briefly shows how to use PPO to train an existing ML-Agent environment in editor. Here we are going to cover a little more details. |
| 8 | + |
| 9 | +## Overall Steps |
| 10 | +1. Create a environment using ML-Agent API. See the [instruction from Unity](https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Learning-Environment-Create-New.md) |
| 11 | +3. Change the BrainType of your brain to `InternalTrainable` in inspector. |
| 12 | +2. Create a Trainer |
| 13 | + 1. Attach a `TrainerPPO.cs` to any GameObject. |
| 14 | + 2. Create a `TrainerParamsPPO` scriptable object with proper parameters in your project and assign it to the Params field in `TrainerPPO.cs`. |
| 15 | + 3. Assign the Trainer to the `Trainer` field of your Brain. |
| 16 | +3. Create a Model |
| 17 | + 1. Attach a `RLModelPPO.cs` to any GameObject. |
| 18 | + 2. Create a `RLNetworkSimpleAC` scriptable with proper object in your project and assign it to the Network field in `RLModelPPO.cs`. |
| 19 | + 3. Assign the created Model to the `modelRef` field of in `TrainerPPO.cs` |
| 20 | + |
| 21 | +4. Play and see how it works. |
| 22 | + |
| 23 | +## Explanation of fields in the inspector |
| 24 | +We use similar parameters as in Unity ML-Agents. If something is confusing, read see their [document](https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Training-PPO.md) for mode datails. |
| 25 | + |
| 26 | +### TrainerPPO.cs |
| 27 | +* `isTraining`: Toggle this to switch between training and inference mode. Note that if isTraining if false when the game starts, the training part of the PPO model will not be initialize and you won't be able to train it in this run. Also, |
| 28 | +* `parameters`: You need to assign this field with a TrainerParamsPPO scriptable object. |
| 29 | +* `continueFromCheckpoint`: If true, when the game starts, the trainer will try to load the saved checkpoint file to resume previous training. |
| 30 | +* `checkpointPath`: the path of the checkpoint, including the file name. |
| 31 | +* `steps`: Just to show you the current step of the training. |
| 32 | + |
| 33 | +### TrainerParamsPPO |
| 34 | +* `learningRate`: Learning rate used to train the neural network. |
| 35 | +* `maxTotalSteps`: Max steps the trainer will be training. |
| 36 | +* `saveModelInterval`: The trained model will be saved every this amount of steps. |
| 37 | +* `rewardDiscountFactor`: Gamma. See PPO algorithm for details. |
| 38 | +* `rewardGAEFactor`: Lambda. See PPO algorithm for details. |
| 39 | +* `valueLossWeight`: Weight of the value loss compared with the policy loss in PPO. |
| 40 | +* `timeHorizon`: Max steps when the PPO trainer will calculate the advantages using the collected data. |
| 41 | +* `entropyLossWeight`: Weight of the entropy loss. |
| 42 | +* `clipEpsilon`: See PPO algorithm for details. The default value is usually fine. |
| 43 | +* `batchSize`: Mini batch size when training. |
| 44 | +* `bufferSizeForTrain`: PPO will train the model once when the buffer size reaches this. |
| 45 | +* `numEpochPerTrain`: For each training, the data in the buffer will be used repeatedly this amount of times. |
| 46 | +* `useHeuristicChance`: See [Training with Heuristics](#training-with-heuristics). |
| 47 | + |
| 48 | +### RLModelPPO.cs |
| 49 | +* `checkpointToLoad`: If you assign a model's saved checkpoint file to it, this will be loaded when model is initialized, regardless of the trainer's loading. Might be used when you are not using a trainer. |
| 50 | +* `Network`: You need to assign this field with a scriptable object that implements RLNetworkPPO.cs. |
| 51 | +* `optimizer`: The time of optimizer to use for this model when training. You can also set its parameters here. |
| 52 | + |
| 53 | +### RLNetworkSimpleAC |
| 54 | +This is a simple implementation of RLNetworkAC that you can create a plug it in as a neural network definition for any RLModelPPO. PPO uses actor/critic structure(See PPO algorithm). |
| 55 | +- `actorHiddenLayers`/`criticHiddenLayers`: Hidden layers of the network. The array size if the number of hidden layers. In each element, there are for parameters that defines each layer. Those do not have default values, so you have to fill them. |
| 56 | + - size: Size of this hidden layer. |
| 57 | + - initialScale: Initial scale of the weights. This might be important for training.Try something larger than 0 and smaller than 1. |
| 58 | + - useBias: Whether Use bias. Usually true. |
| 59 | + - activationFunction: Which activation function to use. Usually Relu. |
| 60 | +- `actorOutputLayerInitialScale`/`criticOutputLayerInitialScale`/`visualEncoderInitialScale`: Initial scale of the weights of the output layers. |
| 61 | +- `actorOutputLayerBias`/`criticOutputLayerBias`/`visualEncoderBias`: Whether use bias. |
| 62 | + |
| 63 | +## Training with Heuristics |
| 64 | +If you already know some policy that is better than random policy, you might give it as a hint to PPO to increase the training a bit. |
| 65 | + |
| 66 | +1. Implement the [AgentDependentDeicision](AgentDependentDeicision.md) for your policy and attach it to the agents that you want them to occasionally use this policy. |
| 67 | +2. In your trainer parameters, set `useHeuristicChance` to larger than 0. |
| 68 | +3. Use [TrainerParamOverride](TrainerParamOverride.md) to decrease the `useHeuristicChance` over time during the training. |
| 69 | + |
| 70 | +Note that your AgentDependentDeicision is only used in training mode. The chance of using it in each step for agent with the script attached depends on `useHeuristicChance`. |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
0 commit comments