Skip to content

Commit

Permalink
new example for check_grad
Browse files Browse the repository at this point in the history
  • Loading branch information
linesd committed Oct 8, 2019
1 parent 11a89df commit 445d7d6
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions examples/check_gradient_3D_rosenbrock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
from optimizer.check_grad import check_grad
from functions.rosenbrock import rosenbrock
from functions.rosenbrock_with_args import rosenbrock_with_args

# set random seed
np.random.seed(0)

# random initialisation of X
X = np.random.normal(0, 1, size=(3, 1))

# check gradients for rosenbrock and rosenbrock with arguments
vec1, d1 = check_grad(rosenbrock, X, 1e-5)
vec2, d2 = check_grad(rosenbrock_with_args, X, 1e-5, args=(100, 400))

# check results are the same (first and second columns should match)
print("Derivatives vs finite difference for the rosenbrock function: ")
print(vec1)
print("Derivatives vs finite difference for the rosenbrock with arguments function: ")
print(vec1)

0 comments on commit 445d7d6

Please sign in to comment.