# CheckGradient In Manopt.jl

**URL:** <https://discourse.julialang.org/t/checkgradient-in-manopt-jl/120743>\
**Category:** Optimization (Mathematical)\
**Created:** [September 30, 2024, 11:25pm UTC](https://discourse.julialang.org/t/checkgradient-in-manopt-jl/120743 "2024-09-30T23:25:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fimiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fimiller/32/51226_2.png) [@fimiller](https://discourse.julialang.org/u/fimiller)\
**Post date:** [September 30, 2024, 11:25pm UTC](https://discourse.julialang.org/t/checkgradient-in-manopt-jl/120743/1 "2024-09-30T23:25:08Z")

</div>

I am performing an optimization problem and wish to make sure that the gradient I have is correct.

When using the `check_gradient` function, I find that the gradient is only correct _some_ of the time, (about 90% of the time).

Here is code to reproduce my problem

```julia
using Manifolds, Manopt

for test in 1:100
    local k = rand(1:20)
    local n = k + rand(1:10)
    global A = rand(k,n)
    local M = Stiefel(size(A)[2], rand(1:n-k))
    function f(M, X)
        return norm(A*X, 2)^2
    end
    function grad_f(M,X)
        euclidean_grad = 2*transpose(A)*A*X
        return project(M, X, euclidean_grad)
    end
    grad_result = check_gradient(M, g_loop, grad_g_loop)
    println("gradient matches at test $(test): ",grad_result)
    
end

```

There is a chance that my gradient is correct, but from example 4.34 here [https://www.nicolasboumal.net/book/IntroOptimManifolds\_Boumal\_2023.pdf](https://www.nicolasboumal.net/book/IntroOptimManifolds_Boumal_2023.pdf)

I do not think that is the case (that said, my matrix A is not symmetric…). Is there a chance I am misunderstanding the `check_gradient` function, and this high accuracy percentage is indicative of it working?

Thanks!
