From 445d7d6e7275fe68a8aabcda2ebd2cabca68fb12 Mon Sep 17 00:00:00 2001 From: linesd Date: Tue, 8 Oct 2019 16:50:13 +0100 Subject: [PATCH] new example for check_grad --- examples/check_gradient_3D_rosenbrock.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 examples/check_gradient_3D_rosenbrock.py diff --git a/examples/check_gradient_3D_rosenbrock.py b/examples/check_gradient_3D_rosenbrock.py new file mode 100644 index 0000000..af9fddd --- /dev/null +++ b/examples/check_gradient_3D_rosenbrock.py @@ -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) \ No newline at end of file