---
# System prepended metadata

title: MLagent Release20 Tutorial

---

# MLagent Release20 Tutorial
0. Toolkit setup
    1. install Pytorch :
        > 最新 CUDA 和 Pytorch : `pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118`
        > 若不是 CUDA 11.8 : https://pytorch.org/get-started/locally/
    2. install Mlagents : 
        > `pip3 install mlagents==0.30.0`
    3. install package through package manager (Optional(在我 push 後) , 你們也可以開新專案測試一下)
        * 開啟 package manager 在 unity 裡
        * 點擊左上角 + 的 UI
        * 選擇 **Add package from git URL**
        * 輸入 `git+https://github.com/Unity-Technologies/ml-agents.git?path=com.unity.ml-agents#release_20`
        >官方範例在 [ml-agents/Project/Assets/ML-Agents/Examples](https://github.com/Unity-Technologies/ml-agents/tree/develop)
    
    4. 下載 onnx
        > `pip3 install onnx`
1. 開啟專案和場景
2. Yaml 設定
    ```
    default_settings: null
    behaviors:
      Knight:
        trainer_type: ppo
        hyperparameters:
          batch_size: 2024
          buffer_size: 20240
          learning_rate: 0.0003
          beta: 0.005
          epsilon: 0.2
          lambd: 0.95
          num_epoch: 3
          learning_rate_schedule: linear
        network_settings:
          normalize: true
          hidden_units: 512
          num_layers: 3
          vis_encode_type: simple
        reward_signals:
          extrinsic:
            gamma: 0.995
            strength: 1.0
        keep_checkpoints: 10
        max_steps: 1000000
        checkpoint_interval: 50000
        time_horizon: 1000
        summary_freq: 10000
        init_path: <你存 checkpoint 和 model 的位置>\checkpoint.pt
    ```
3. Train command
    > ` mlagents-learn <yaml 檔的位置> --run-id=<model 名稱> --force` 
    > - 模型檔格式為 .onnx
4. Play 場景
5. 訓練完後可直接把 .onnx 指派給 AI ， Play 場景後就是載入 model 的 AI 了。