This is a repository for Unity Machine Learning Agents, an open-source Unity plugin that enables games and simulations to serve as environments for training intelligent agents.
- Unity (2021.3 or later)
- Python (3.8.13 or higher)
com.unity.ml-agentsUnity packagemlagentsPython package
You will need to have Unity (version 2021.3 or later) and Python (version 3.8.13 or higher) installed on your machine.
Navigate to your preferred directory (not the repository directory) on your PC, then run the following command to create a virtual environment. Change the last venv for your desire since it is folder name:
python -m venv venv
Activate the virtual environment to start working with the ML Agents:
venv\Scripts\activate
Upgrade pip, the Python package manager:
python -m pip install --upgrade pip
Now, install the ml-agents package:
pip install mlagents
PyTorch, a deep learning framework, is required for working with ML Agents. Install PyTorch along with torchvision and torchaudio:
pip3 install torch torchvision torchaudio
Install the specific version (3.20.3) of Protocol Buffers because ml agents package installs the wrong version for some reason:
pip install protobuf==3.20.3
When you finish your training, if you get a torch error on your terminal like Module onnx is not installed!, Run the following command:
pip install onnx
After all these installations, if everything is correctly installed, you should be able to see all the commands when you run:
mlagents-learn --help
Before training the ML agents, make sure that the virtual environment is activated. If it is not activated , repeat Step 3. Then use the following command to initiate the training. Make sure to replace "Test1" with a unique identifier each time:mlagents-learn --run-id=Test1. When you see the unity logo on your terminal, press play on the Unity scene to begin training the agents. Check their progress on the terminal window with each step. Training will end when steps reaches max_steps value in your config file. If you want to manually end the training, press CTRL+C on your terminal to terminate the training process. Enjoy teaching your agents and making them work like ants!
If you want to improve the learning speed of your Unity ML agents, here are some suggestions:
- Hyperparameters can have a huge impact on the learning speed of your agents. Learning rate, batch size, number of layers in your neural network, number of hidden units per layer, etc., can be adjusted to improve learning speed. However, note that these adjustments may require a lot of trial and error. So experiment with your config file to find the best one.
- Always start with the simplest task For example, if your agent is learning to play a game, you could start with simpler scenarios and then introduce more challenging ones as the agent improves.
- Use multiple instances. By using multiple parallel instances, you can increase the number of experiences your agent gets in the same amount of time, leading to faster learning. ML Agents allows running multiple environments in parallel. Check my scene for an example.
- Crafting a good reward function is crucial for training speed. Rewards should be frequent enough to provide useful learning signals and sparse enough to push the agent to explore. Additionally, consider normalizing your rewards to a consistent scale, like -1 to 1, to prevent the agent from prioritizing certain types of rewards over others.
- If possible, reduce the dimensionality of the state space your agent has to learn from. For example, if your agent gets a 100-dimensional vector of information but only 10 of those dimensions are actually useful, consider modifying the environment so it only returns those 10 dimensions. This simplifies the task for your agent and can improve learning speed.
- If you have a similar task that's already been solved, you could use a model pretrained on that task as a starting point for training your agent. This technique, called transfer learning, can drastically speed up training times.
- You can create a folder to keep your config files in your venv folder. I shared my config files in this repository so you can get them and modify for your needs.
- If you want to run a specific config file for the training, run this command. Remember to change the directory with yours:
mlagents-learn C:\ml-agent\venv\Configs\MazeOneConfig.yaml --run-id=MazeOneRun - Your training results will be on the directory of this project, but can't be seen on the unity project window like:
C:\ml-agent\src\ML-Agents\results - If you want to use the model of a specific training, you need to move the
.onnxto the assets folder. Example: MoveC:\ml-agent\src\ML-Agents\results\Test1\Test1.onnxtoC:\ml-agent\src\ML-Agents\Assets. So now it can be seen on the unity project window and you can use this model.