Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Displaying custom metrics on tensorboard via ml-agents during / after training.

Discussion in 'ML-Agents' started by Sephraih, Apr 11, 2020.

  1. Sephraih

    Sephraih

    Joined:
    Jun 15, 2019
    Posts:
    11
    Hello there, I stumbled upon this in the mlagents documentation:
    Custom Metrics from C#
    To get custom metrics from a C# environment into Tensorboard, you can use the StatsSideChannel:

    var statsSideChannel = SideChannelUtils.GetSideChannel<StatsSideChannel>();
    statsSideChannel.AddStat("MyMetric", 1.0);

    Basically I have a script attached to each gym - instance, to which the agents report certain statistical values, such as how long they survived, how much damage they have done, kills gained etc. The script, then simply prints out certain values at certain times, which i would like to instead direct to tensorboard.
    I, being a complete boon, have however no idea how i should integrate the lines above into my code, i cannot seem to find a reference to sidechannelutils.

    - to keep this simple i made a small example class; i would now love to send the updated "hks" in the "UpdateHks(float n)" to tensorboard whenever the method is accessed:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Statistics : MonoBehaviour
    {
    public float hks=0; //highest killing spree achieved during game by agent

    public void UpdateHks(float n) {
    hks = hks > n ? hks : n;
    }

    }

    What would i need to add to this class to be able to do so? - Thanks a bunch! :)
     
  2. LexVolkov

    LexVolkov

    Joined:
    Sep 14, 2014
    Posts:
    62
    using MLAgents;
    using MLAgents.SideChannels;

    maybe this is not enough?
     
  3. Sephraih

    Sephraih

    Joined:
    Jun 15, 2019
    Posts:
    11
    Thanks a bunch, that missing import did bring me closer to the issue - when implementing the code earlier, i wasn't able to find any import i was missing, now there seems to be a reason for it I don't really understand yet :/

    I have never dealt with assembly references - I've found this page further describing the side channels and am going to have a look at it, however unsure whether that would fix the issue, or whether I need to update some other setting / environment variable etc. - or is it due to me running an outdated ml-agents version? (0.14.0)
    sidechanassressemb.png
     
  4. andrewcoh_unity

    andrewcoh_unity

    Unity Technologies

    Joined:
    Sep 5, 2019
    Posts:
    162
    What version of ML-Agents are you using?
     
  5. Sephraih

    Sephraih

    Joined:
    Jun 15, 2019
    Posts:
    11
    0.14.0 with python 3.7 (the combination seemed to work so far)
     
  6. andrewcoh_unity

    andrewcoh_unity

    Unity Technologies

    Joined:
    Sep 5, 2019
    Posts:
    162
    The SideChannels namespace is introduced in version 0.15 so this won't work in 0.14.
     
  7. Sephraih

    Sephraih

    Joined:
    Jun 15, 2019
    Posts:
    11
    Explains a lot, hah - thanks!
     
  8. celion_unity

    celion_unity

    Joined:
    Jun 12, 2019
    Posts:
    289
    And additionally, the "Custom Metrics from C#" was added after the 0.15.0 release; it's only available if you're installing from the current master branch. The next release will include it and will be out before the end of the month (hopefully).