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

Recurring warning: Fewer observations(0) made than vector size.

Discussion in 'ML-Agents' started by frieldhelm446, Sep 30, 2020.

  1. frieldhelm446

    frieldhelm446

    Joined:
    Sep 27, 2020
    Posts:
    28
    I'm getting this warning "Fewer observations(0) made than vector size." (seems like every episode reset). Is it possible to fix this somehow? I'm adding the correct amount of observations in the CollectObservations method in my Agent.
     
  2. henrypeteet

    henrypeteet

    Unity Technologies

    Joined:
    Aug 19, 2020
    Posts:
    37
    Hi, sorry but I don't think I can't give you a clear answer without more details since this could be from any number of different issues. Would it be possible to share the bit of code you have in the below functions?
    1. CollectObservations
    2. OnEpisodeBegin
    3. Initialize
     
  3. frieldhelm446

    frieldhelm446

    Joined:
    Sep 27, 2020
    Posts:
    28
    My initialization code and onepisodebegin contain no references to observation. Here's the code for CollectObservations

    Code (CSharp):
    1. public override void CollectObservations(VectorSensor sensor)
    2.     {
    3.         // Find closest food
    4.         float closestDistance = 9999;
    5.         GameObject closestCookie = null;
    6.         velocity = rb2D.velocity.magnitude;
    7.  
    8.         var cookies = GameObject.FindGameObjectsWithTag("Food");
    9.  
    10.  
    11.         foreach (var cookie in cookies)
    12.         {
    13.             var cookiePosition = cookie.transform.position;
    14.             var distance = Vector3.Distance(cookiePosition, rb2D.position);
    15.             if (distance < closestDistance)
    16.             {
    17.                 closestDistance = distance;
    18.                 closestCookie = cookie;
    19.             }
    20.         }
    21.        
    22.         sensor.AddObservation(rb2D.position);
    23.         sensor.AddObservation(rb2D.rotation);
    24.         sensor.AddObservation(rb2D.velocity);
    25.         if(closestCookie != null)
    26.         {
    27.             sensor.AddObservation(closestCookie.transform.position.x);
    28.             sensor.AddObservation(closestCookie.transform.position.y);
    29.             sensor.AddObservation(closestDistance);
    30.         }
    31.         else
    32.         {
    33.             // padding
    34.             sensor.AddObservation(0);
    35.             sensor.AddObservation(0);
    36.             sensor.AddObservation(0);
    37.         }
    38.         CountDownLife();
    39.         AddReward(Time.deltaTime);
    40.     }
     
  4. henrypeteet

    henrypeteet

    Unity Technologies

    Joined:
    Aug 19, 2020
    Posts:
    37
    Thanks, that looks great to me too. Are you using a "Decision Requester" and/or destroying / disabling and re-enabling any agents or components?
     
  5. frieldhelm446

    frieldhelm446

    Joined:
    Sep 27, 2020
    Posts:
    28
    Yes, I'm using the decision requester with a decision period of 1. Agents are not destroyed or disabled.
     
  6. henrypeteet

    henrypeteet

    Unity Technologies

    Joined:
    Aug 19, 2020
    Posts:
    37
    I am not sure why this would be happening based on the information I have at the moment. Are you getting any other error messages? Is it possible to share what you are doing in the other agent methods?
     
  7. m4l4

    m4l4

    Joined:
    Jul 28, 2020
    Posts:
    81
    i had a similar issue on some projects. I haven't been able to pinpoint the exact problem, but i've noticed that the error message was also appearing after pressing "stop" during manual tests. I also was using a decision requester and no piece of the agent wad destroyable. Seemed like an attempt to get one more observation after the agent reset.
     
  8. rayanjenkinsed

    rayanjenkinsed

    Joined:
    Aug 13, 2020
    Posts:
    3
    (August 2022 update on this problem, if other people have been going crazy like me over it)
    Hi !

    If you're having this error, it may be because your GameObject has two (or more) Agent scripts attached to it.

    You can verify that in that in the Inspector, check if there's another Agent script existing.
     
    AmineUM6P likes this.
  9. WaxyMcRivers

    WaxyMcRivers

    Joined:
    May 9, 2016
    Posts:
    59
    Make sure the collect observation call isn't happening when your agent game object is set as inactive.

    If end episode is called before the next collect observations call, the academy will cause the agent to collect one more observation to pair the final rewards with a fresh observation. I have run into your issue when my scene logic was disabling the agent game object before this final call was happening, thus all of the sensors were disabled and not providing observation values (ie 0).