Solved by richard in post #2 
              
              
                When you do backprogation with the first, at some point you’ll run into the derivative of acos(x), which is - 1 / sqrt( 1 - x^2 ). That can be nasty and lead to your NaNs if x is close to 1 or -1 at times. 
In particular, consider the following two functions: f(x) = cos(acos(x)) and g(x) = x. They’r…