Unity ML-Agents Tutorial | AI Truffle Pigs! 🐷

This tutorial is a bit stale… for a new tutorial built with the latest version of ML-Agents, check this out:


Unity ML Agents | Penguins

In this Unity ML Agents tutorial you’ll learn how to use machine learning, specifically reinforcement learning in Unity to train penguins to find fish and feed their babies.

If you still want to do the pig tutorial, there’s still value to be gained, but there are a few changes to ML-Agents that will require changing. I’ve noted them below.

👇 Video updates further down the page!


Learn how to create AI truffle-seeking pigs with Unity ML-Agents and Tensorflow!

This page is a companion to the YouTube tutorial above on creating intelligent truffle pigs with Unity ML-Agents.


👉The ENABLE_TENSORFLOW and Adding TensorFlowSharp are no longer necessary.

  • Unity updated ML-Agents to version 0.7.0 on February 26th, 2019 and there’s a small change that affects this tutorial. It now uses Unity Inference Engine. You should be able to skip the ENABLE_TENSORFLOW step.

  • As of August 2nd, 2019, Unity has updated ML-Agents again. If you are using version 0.9, you may need to update trainer_config.yaml to the latest format. (More details here)

  • After training, you will now have a .nn file instead of a .bytes file. This works the same way, you just drag it into your brain, uncheck “Control” in the Academy, and press play to run inference.

Note: If your .nn file doesn’t work, try allowing it to fully complete training (don’t exit early with Ctrl + C). I had some .nn files fail with unhelpful errors when I didn’t let it go all the way.


As mentioned in the video, the first thing you'll need to do is follow the Installation instructions and the Basic Guide. These will get you up and running with Unity ML-Agents and ready to start the tutorial.



I highly encourage you to check out the ML-Agents Documentation, which will help you understand the project in more depth and provide a place to discuss issues that are not specific to the pig project.


3d Mesh Assets and Code

The free assets you will need for this project are here:

https://gumroad.com/l/NTzXT 👈 $0+ on Gumroad means you can get them *completely for free* by typing in a 0.

(however, if you want to give extra support, you’ll make me feel warm and fuzzy inside. 😃)

ML-Agents Code

In this video, I'm using Release 0.6, which you can get here:


The latest version is likely very similar, but if you have trouble, you can always roll back to 0.6.

Helpful Additions

Training Command

The command I used to train (in my Anaconda Prompt) with curriculum was:

    mlagents-learn config/trainer_config.yaml --curriculum=config/curricula/pig/ --run-id=pig_tutorial_01 --train

Note: If you get an error that a parameter is not present in the Environment, make sure to add Reset Parameters for "num_truffles", "num_stumps", and "spawn_range" in the Pig Academy.



You can reach out to me at @aktwelve on Twitter or connect with me on LinkedIn.

For future updates