Hello,
I have encountered some issues while using the checkgradientfunction. The results vary across multiple runs. For example:
During one execution, the output was: # Gradient check The slope should be 2. It appears to be: 1.99973. If it is far from 2, then the gradient might be erroneous. The gradient at x must be a tangent vector at x. If so, the following number is zero up to machine precision: 3.70803e-16. If it is far from 0, the gradient is not tangent.
This suggests that the gradient calculation appears correct.
However, when I ran the function again, the result changed to: # Gradient check The slope should be 2. It appears to be: 1.00084. If it is far from 2, then the gradient might be erroneous. The gradient at x must be a tangent vector at x. If so, the following number is zero up to machine precision: 5.18517e-16. If it is far from 0, the gradient is not tangent.
This now indicates a potential issue with the gradient.
I am confused about why such inconsistencies occur. Could you help clarify whether my gradient derivation is actually correct?
Thank you for your assistance.
Sincerely, Xingyu Zhao
Thank you for your reply. Based on your suggestions, I have reviewed my code and the optimization problem. My observations are as follows:
My function is globally differentiable with respect to the optimization variables.
My function is deterministic and does not involve any stochastic components.
I performed the gradient check at a fixed point (using checkgradient(problem, elems_ini)), and the issue still occurs.
My optimization problem is built using the productmanifold operation:
elems.z = euclideanfactory(n);
elems.Y = spherecomplexfactory(n, m);
manifold = productmanifold(elems);
problem.M = manifold;
problem.cost = @(elems) cost_func(para, elems);
problem.egrad = @(elems) grad_func(para, elems);
It is worth noting that within the grad_func, I applied the chain rule: I first computed the derivative with respect to x, and then multiplied by cos(z). I suspect whether the non-one-to-one mapping of x = sin(z) could be affecting my gradient. Does this possibility exist?

