Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Quaternion * vector3 return NaN ?

Discussion in 'Editor & General Support' started by Wonsder, Sep 13, 2020.

  1. Wonsder

    Wonsder

    Joined:
    Jan 24, 2020
    Posts:
    37
    Hello everyone,
    I'm creating a neural networks where inputs and outputs are Vector3, and weights are Quaternions.
    Long story short, Here is my code for the maths of the nn :

    Code (CSharp):
    1.         public Vector3 Proceed(Vector3[] _in)
    2.         {
    3.             Vector3 result = Vector3.zero;
    4.  
    5.             for(int i = 0; i < _in.Length; i++)
    6.             {
    7.                 result += rotations[i] * _in[i] * weights[i];
    8.                 //NaN somewhere here... why ?
    9.             }
    10.  
    11.             return result;
    12.         }
    rotations is an array of quaternions, and weights is an array of floats.
    However, after this I have vector3 output that have component that is NaN... anyone knows where that come from ? I am doing something wrong ?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    Check i your quaternion is defined and initiated. Meaning is none 0, or Nan matrix. You should have some values, or at least be identity.

    I think also, your multiplied vector should also be none 0. Consider using normalised vector.
     
    Last edited: Sep 13, 2020
  3. Wonsder

    Wonsder

    Joined:
    Jan 24, 2020
    Posts:
    37
    Thanks for the reply.
    The quaternion is created from Quaternion.Euler(random vector3 between -180 and 180).
    It works when I normalize the result of the multiplication of the Quaternion and the vector, but by doing this I am losing information in the neural network, and I need the magnitude of the vectors.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    What I mean is, you could normalise vector before multiplication.

    You shouldn't be normalising after multiplication, as indeed, you will loose information.

    Can you you write here a matrix and vectors of one entry case, which results NaN?
    For example every value for index i=0.

    Also, what you can try, remove temporarily multiplication by vectors, and simplyultiply quaternion by forward vector. See if you got valid data, or NaN. At least will now, if quaterrnion is valid.

    If is valid, then try remove / replace one multiplication parameter, with constant.
    And see if results are valid.
     
    Last edited: Sep 13, 2020
  5. Wonsder

    Wonsder

    Joined:
    Jan 24, 2020
    Posts:
    37
    Done a bit of tweaking.
    When I normalize input vector before multiplication, everything works, But I still lose the information on the input vector length.
    I saw that the input vector's magnitude was NaN, but only after some iterations throughts the neural network
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    Looks like this may be your issue.
    You must not allow NaNs into system.
     
  7. Wonsder

    Wonsder

    Joined:
    Jan 24, 2020
    Posts:
    37
    Yes, that was it. Add if conditions before feeding the system, works nicely now.
    Thanks a lot for the help !
     
    Antypodish likes this.