diff --git a/doc/pub/week6/html/._week6-bs000.html b/doc/pub/week6/html/._week6-bs000.html index 66314f1e..816dfa3a 100644 --- a/doc/pub/week6/html/._week6-bs000.html +++ b/doc/pub/week6/html/._week6-bs000.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -266,7 +268,7 @@

    Week 8 February 19-23: Gradient Methods


    -

    Jan 1, 2024

    +

    February 23, 2024


    @@ -291,7 +293,7 @@

    Jan 1, 2024

  • 9
  • 10
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs001.html b/doc/pub/week6/html/._week6-bs001.html index 22641eb5..117d691f 100644 --- a/doc/pub/week6/html/._week6-bs001.html +++ b/doc/pub/week6/html/._week6-bs001.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,7 +250,7 @@

     

     

     

    -

    Overview of week 8

    +

    Overview

    @@ -303,7 +305,7 @@

    Overview of week 8

  • 10
  • 11
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs002.html b/doc/pub/week6/html/._week6-bs002.html index 0e6f1c02..e0a5aaa6 100644 --- a/doc/pub/week6/html/._week6-bs002.html +++ b/doc/pub/week6/html/._week6-bs002.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -250,7 +252,7 @@

    Brief reminder on Newton-Raphson's method

    -

    Let us quickly remind ourselves how we derive the above method.

    +

    Let us quickly remind ourselves on how we derive the above method.

    Perhaps the most celebrated of all one-dimensional root-finding routines is Newton's method, also called the Newton-Raphson @@ -278,7 +280,7 @@

    Brief reminder
  • 11
  • 12
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs003.html b/doc/pub/week6/html/._week6-bs003.html index 10c75050..8b17fcad 100644 --- a/doc/pub/week6/html/._week6-bs003.html +++ b/doc/pub/week6/html/._week6-bs003.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -299,7 +301,7 @@

    The equations

  • 12
  • 13
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs004.html b/doc/pub/week6/html/._week6-bs004.html index 47eafb0f..fb1c3294 100644 --- a/doc/pub/week6/html/._week6-bs004.html +++ b/doc/pub/week6/html/._week6-bs004.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -282,7 +284,7 @@

    Simple geometric interpr
  • 13
  • 14
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs005.html b/doc/pub/week6/html/._week6-bs005.html index 580ac8aa..ef8d2e9f 100644 --- a/doc/pub/week6/html/._week6-bs005.html +++ b/doc/pub/week6/html/._week6-bs005.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -321,7 +323,7 @@

    Extending to more th
  • 14
  • 15
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs006.html b/doc/pub/week6/html/._week6-bs006.html index 0ab76ae8..c3474135 100644 --- a/doc/pub/week6/html/._week6-bs006.html +++ b/doc/pub/week6/html/._week6-bs006.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -289,7 +291,7 @@

    Steepest descent

  • 15
  • 16
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs007.html b/doc/pub/week6/html/._week6-bs007.html index 1f747d23..a460e13f 100644 --- a/doc/pub/week6/html/._week6-bs007.html +++ b/doc/pub/week6/html/._week6-bs007.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -286,7 +288,7 @@

    More on Steepest descent

  • 16
  • 17
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs008.html b/doc/pub/week6/html/._week6-bs008.html index 0acae2f2..3be1347e 100644 --- a/doc/pub/week6/html/._week6-bs008.html +++ b/doc/pub/week6/html/._week6-bs008.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -294,7 +296,7 @@

    The ideal

  • 17
  • 18
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs009.html b/doc/pub/week6/html/._week6-bs009.html index 56a2a533..e603d8c3 100644 --- a/doc/pub/week6/html/._week6-bs009.html +++ b/doc/pub/week6/html/._week6-bs009.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -288,7 +290,7 @@

    The sensitiven
  • 18
  • 19
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs010.html b/doc/pub/week6/html/._week6-bs010.html index 1279e421..2ce2eba7 100644 --- a/doc/pub/week6/html/._week6-bs010.html +++ b/doc/pub/week6/html/._week6-bs010.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -289,7 +291,7 @@

    Convex functions

  • 19
  • 20
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs011.html b/doc/pub/week6/html/._week6-bs011.html index 31c196cb..c4b3b616 100644 --- a/doc/pub/week6/html/._week6-bs011.html +++ b/doc/pub/week6/html/._week6-bs011.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -277,7 +279,7 @@

    Convex function

  • 20
  • 21
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs012.html b/doc/pub/week6/html/._week6-bs012.html index 157fd995..0a84d781 100644 --- a/doc/pub/week6/html/._week6-bs012.html +++ b/doc/pub/week6/html/._week6-bs012.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -313,7 +315,7 @@

    Conditions on convex func
  • 21
  • 22
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs013.html b/doc/pub/week6/html/._week6-bs013.html index ff173f0f..1c3ebfeb 100644 --- a/doc/pub/week6/html/._week6-bs013.html +++ b/doc/pub/week6/html/._week6-bs013.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -300,7 +302,7 @@

    More on convex functions

  • 22
  • 23
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs014.html b/doc/pub/week6/html/._week6-bs014.html index c795bf17..20b62f3c 100644 --- a/doc/pub/week6/html/._week6-bs014.html +++ b/doc/pub/week6/html/._week6-bs014.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -292,7 +294,7 @@

    Some simple problems

  • 23
  • 24
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs015.html b/doc/pub/week6/html/._week6-bs015.html index e27f81c7..210ab020 100644 --- a/doc/pub/week6/html/._week6-bs015.html +++ b/doc/pub/week6/html/._week6-bs015.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -304,7 +306,7 @@

    Standard steepest descent

    24
  • 25
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs016.html b/doc/pub/week6/html/._week6-bs016.html index 800e315a..1dd4ce41 100644 --- a/doc/pub/week6/html/._week6-bs016.html +++ b/doc/pub/week6/html/._week6-bs016.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -286,7 +288,7 @@

    Gradient method

  • 25
  • 26
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs017.html b/doc/pub/week6/html/._week6-bs017.html index 24e8713e..783c7d0d 100644 --- a/doc/pub/week6/html/._week6-bs017.html +++ b/doc/pub/week6/html/._week6-bs017.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -293,7 +295,7 @@

    Steepest descent method

  • 26
  • 27
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs018.html b/doc/pub/week6/html/._week6-bs018.html index a467d40f..f9a854f5 100644 --- a/doc/pub/week6/html/._week6-bs018.html +++ b/doc/pub/week6/html/._week6-bs018.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -301,7 +303,7 @@

    Steepest descent method

  • 27
  • 28
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs019.html b/doc/pub/week6/html/._week6-bs019.html index 3bc3bb7a..cadb56be 100644 --- a/doc/pub/week6/html/._week6-bs019.html +++ b/doc/pub/week6/html/._week6-bs019.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -314,7 +316,7 @@

    Final expressions

  • 28
  • 29
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs020.html b/doc/pub/week6/html/._week6-bs020.html index 6b7889c1..fbf7acd1 100644 --- a/doc/pub/week6/html/._week6-bs020.html +++ b/doc/pub/week6/html/._week6-bs020.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,7 +250,32 @@

     

     

     

    -

    Code examples for steepest descent

    +

    Our simple \( 2\times 2 \) example

    + +

    Last week we introduced the simple two-dimensional function

    +$$ +f(x_1,x_2)=x_1^2+x_1x_2+10x_2^2-5x_1-3x_2, +$$ + +

    which is of the form (in terms of vectors and matrices)

    +$$ +f(\boldsymbol{x})=\frac{1}{2}\boldsymbol{x}^T\boldsymbol{A}\boldsymbol{x}-\boldsymbol{b}^T\boldsymbol{x}, +$$ + +

    where we have

    +$$ +\boldsymbol{x}=\begin{bmatrix} x_1 \\ x_2\end{bmatrix}, +$$ + +$$ +\boldsymbol{b}=\begin{bmatrix} 5 \\ 3\end{bmatrix}, +$$ + +

    and

    +$$ +\boldsymbol{A}=\begin{bmatrix} 2 & 1\\ 1& 20\end{bmatrix}. +$$ +

    @@ -275,7 +302,7 @@

    Code examples for ste
  • 29
  • 30
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs021.html b/doc/pub/week6/html/._week6-bs021.html index 80d8332c..bb03e53a 100644 --- a/doc/pub/week6/html/._week6-bs021.html +++ b/doc/pub/week6/html/._week6-bs021.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,59 +250,29 @@

     

     

     

    -

    Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come

    -
    -
    - +

    Derivatives and more

    - -
    -
    -
    -
    -
    -
    #include <cmath>
    -#include <iostream>
    -#include <fstream>
    -#include <iomanip>
    -#include "vectormatrixclass.h"
    -using namespace  std;
    -//   Main function begins here
    -int main(int  argc, char * argv[]){
    -  int dim = 2;
    -  Vector x(dim),xsd(dim), b(dim),x0(dim);
    -  Matrix A(dim,dim);
    +

    Optimizing the above equation, that is

    +$$ +\nabla f = 0 = \boldsymbol{A}\boldsymbol{x}-\boldsymbol{b}, +$$ - // Set our initial guess - x0(0) = x0(1) = 0; - // Set the matrix - A(0,0) = 3; A(1,0) = 2; A(0,1) = 2; A(1,1) = 6; - b(0) = 2; b(1) = -8; - cout << "The Matrix A that we are using: " << endl; - A.Print(); - cout << endl; - xsd = SteepestDescent(A,b,x0); - cout << "The approximate solution using Steepest Descent is: " << endl; - xsd.Print(); - cout << endl; -} -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +

    which leads to a simple matrix-inversion problem

    +$$ +\boldsymbol{x}=\boldsymbol{A}^{-1}\boldsymbol{b}. +$$ + +

    This problem is easy to solve since we can calculate the inverse. Alternatively, we can solve the two coupled equations with two unknowns

    +$$ +\frac{\partial f}{\partial x_1}=2x_1+x_2-5=0, +$$ + +

    and

    +$$ +\frac{\partial f}{\partial x_2}=x_1+20x_2-3=0, +$$ +

    with solutions \( x_1=97/39 \) and \( x_2=1/39 \).

    @@ -327,7 +299,7 @@

    30
  • 31
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs022.html b/doc/pub/week6/html/._week6-bs022.html index 1e63bd27..bb393173 100644 --- a/doc/pub/week6/html/._week6-bs022.html +++ b/doc/pub/week6/html/._week6-bs022.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,7 +250,7 @@

     

     

     

    -

    The routine for the steepest descent method

    +

    Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come

    @@ -259,26 +261,30 @@

    The routine
    -
    Vector SteepestDescent(Matrix A, Vector b, Vector x0){
    -  int IterMax, i;
    -  int dim = x0.Dimension();
    -  const double tolerance = 1.0e-14;
    -  Vector x(dim),f(dim),z(dim);
    -  double c,alpha,d;
    -  IterMax = 30;
    -  x = x0;
    -  r = A*x-b;
    -  i = 0;
    -  while (i <= IterMax){
    -    z = A*r;
    -    c = dot(r,r);
    -    alpha = c/dot(r,z);
    -    x = x - alpha*r;
    -    r =  A*x-b;
    -    if(sqrt(dot(r,r)) < tolerance) break;
    -    i++;
    -  }
    -  return x;
    +  
    #include <cmath>
    +#include <iostream>
    +#include <fstream>
    +#include <iomanip>
    +#include "vectormatrixclass.h"
    +using namespace  std;
    +//   Main function begins here
    +int main(int  argc, char * argv[]){
    +  int dim = 2;
    +  Vector x(dim),xsd(dim), b(dim),x0(dim);
    +  Matrix A(dim,dim);
    +
    +  // Set our initial guess
    +  x0(0) = x0(1) = 0;
    +  // Set the matrix
    +  A(0,0) =  2;    A(1,0) =  1;   A(0,1) =  1;   A(1,1) =  20;
    +  b(0) = 5; b(1) = 3;
    +  cout << "The Matrix A that we are using: " << endl;
    +  A.Print();
    +  cout << endl;
    +  xsd = SteepestDescent(A,b,x0);
    +  cout << "The approximate solution using Steepest Descent is: " << endl;
    +  xsd.Print();
    +  cout << endl;
     }
     
    @@ -323,7 +329,7 @@

    The routine
  • 31
  • 32
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs023.html b/doc/pub/week6/html/._week6-bs023.html index 637f1326..e65e946e 100644 --- a/doc/pub/week6/html/._week6-bs023.html +++ b/doc/pub/week6/html/._week6-bs023.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,102 +250,10 @@

     

     

     

    -

    Steepest descent example

    - - - -
    -
    -
    -
    -
    -
    import numpy as np
    -import numpy.linalg as la
    -
    -import scipy.optimize as sopt
    -
    -import matplotlib.pyplot as pt
    -from mpl_toolkits.mplot3d import axes3d
    -
    -def f(x):
    -    return 0.5*x[0]**2 + 2.5*x[1]**2
    -
    -def df(x):
    -    return np.array([x[0], 5*x[1]])
    -
    -fig = pt.figure()
    -ax = fig.gca(projection="3d")
    -
    -xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j]
    -fmesh = f(np.array([xmesh, ymesh]))
    -ax.plot_surface(xmesh, ymesh, fmesh)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    And then as countor plot

    - - -
    -
    -
    -
    -
    -
    pt.axis("equal")
    -pt.contour(xmesh, ymesh, fmesh)
    -guesses = [np.array([2, 2./5])]
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Find guesses

    - - -
    -
    -
    -
    -
    -
    x = guesses[-1]
    -s = -df(x)
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Run it!

    +

    The routine for the steepest descent method

    +
    +
    +
    @@ -351,13 +261,27 @@

    Steepest descent example

    -
    def f1d(alpha):
    -    return f(x + alpha*s)
    -
    -alpha_opt = sopt.golden(f1d)
    -next_guess = x + alpha_opt * s
    -guesses.append(next_guess)
    -print(next_guess)
    +  
    Vector SteepestDescent(Matrix A, Vector b, Vector x0){
    +  int IterMax, i;
    +  int dim = x0.Dimension();
    +  const double tolerance = 1.0e-14;
    +  Vector x(dim),f(dim),z(dim);
    +  double c,alpha,d;
    +  IterMax = 30;
    +  x = x0;
    +  r = A*x-b;
    +  i = 0;
    +  while (i <= IterMax){
    +    z = A*r;
    +    c = dot(r,r);
    +    alpha = c/dot(r,z);
    +    x = x - alpha*r;
    +    r =  A*x-b;
    +    if(sqrt(dot(r,r)) < tolerance) break;
    +    i++;
    +  }
    +  return x;
    +}
     
    @@ -372,32 +296,7 @@

    Steepest descent example

    - -

    What happened?

    - - -
    -
    -
    -
    -
    -
    pt.axis("equal")
    -pt.contour(xmesh, ymesh, fmesh, 50)
    -it_array = np.array(guesses)
    -pt.plot(it_array.T[0], it_array.T[1], "x-")
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    @@ -426,7 +325,7 @@

    Steepest descent example

  • 32
  • 33
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs024.html b/doc/pub/week6/html/._week6-bs024.html index ceff792b..07ca9834 100644 --- a/doc/pub/week6/html/._week6-bs024.html +++ b/doc/pub/week6/html/._week6-bs024.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,34 +250,156 @@

     

     

     

    -

    Conjugate gradient method

    -
    -
    - -

    In the CG method we define so-called conjugate directions and two vectors -\( \hat{s} \) and \( \hat{t} \) -are said to be -conjugate if -

    -$$ -\begin{equation*} -\hat{s}^T\hat{A}\hat{t}= 0. -\end{equation*} -$$ +

    Steepest descent example

    -

    The philosophy of the CG method is to perform searches in various conjugate directions -of our vectors \( \hat{x}_i \) obeying the above criterion, namely -

    -$$ -\begin{equation*} -\hat{x}_i^T\hat{A}\hat{x}_j= 0. -\end{equation*} -$$ -

    Two vectors are conjugate if they are orthogonal with respect to -this inner product. Being conjugate is a symmetric relation: if \( \hat{s} \) is conjugate to \( \hat{t} \), then \( \hat{t} \) is conjugate to \( \hat{s} \). -

    + +
    +
    +
    +
    +
    +
    import numpy as np
    +import numpy.linalg as la
    +
    +import scipy.optimize as sopt
    +
    +import matplotlib.pyplot as pt
    +from mpl_toolkits.mplot3d import axes3d
    +
    +def f(x):
    +    return x[0]**2 + 10.0*x[1]**2+x[0]*x[1]-5.0*x[0]-3*x[2]
    +
    +def df(x):
    +    return np.array(2*[x[0]+x[1]-5.0, x[0]+20*x[1]]-3.0)
    +
    +fig = pt.figure()
    +ax = fig.gca(projection="3d")
    +
    +xmesh, ymesh = np.mgrid[-2:3:00j,-2:3:00j]
    +fmesh = f(np.array([xmesh, ymesh]))
    +ax.plot_surface(xmesh, ymesh, fmesh)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    And then as countor plot

    + + +
    +
    +
    +
    +
    +
    pt.axis("equal")
    +pt.contour(xmesh, ymesh, fmesh)
    +guesses = [np.array([3.0, 0.05])]
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    Find guesses

    + + +
    +
    +
    +
    +
    +
    x = guesses[-1]
    +s = -df(x)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    Run it!

    + + +
    +
    +
    +
    +
    +
    def f1d(alpha):
    +    return f(x + alpha*s)
    +
    +alpha_opt = sopt.golden(f1d)
    +next_guess = x + alpha_opt * s
    +guesses.append(next_guess)
    +print(next_guess)
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    What happened?

    + + +
    +
    +
    +
    +
    +
    pt.axis("equal")
    +pt.contour(xmesh, ymesh, fmesh, 50)
    +it_array = np.array(guesses)
    +pt.plot(it_array.T[0], it_array.T[1], "x-")
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    @@ -304,7 +428,7 @@

    Conjugate gradient method

    33
  • 34
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs025.html b/doc/pub/week6/html/._week6-bs025.html index d60fdf3e..845a0424 100644 --- a/doc/pub/week6/html/._week6-bs025.html +++ b/doc/pub/week6/html/._week6-bs025.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -252,14 +254,29 @@

    Conjugate gradient method

    -

    An example is given by the eigenvectors of the matrix

    +

    In the CG method we define so-called conjugate directions and two vectors +\( \hat{s} \) and \( \hat{t} \) +are said to be +conjugate if +

    $$ \begin{equation*} -\hat{v}_i^T\hat{A}\hat{v}_j= \lambda\hat{v}_i^T\hat{v}_j, +\hat{s}^T\hat{A}\hat{t}= 0. \end{equation*} $$ -

    which is zero unless \( i=j \).

    +

    The philosophy of the CG method is to perform searches in various conjugate directions +of our vectors \( \hat{x}_i \) obeying the above criterion, namely +

    +$$ +\begin{equation*} +\hat{x}_i^T\hat{A}\hat{x}_j= 0. +\end{equation*} +$$ + +

    Two vectors are conjugate if they are orthogonal with respect to +this inner product. Being conjugate is a symmetric relation: if \( \hat{s} \) is conjugate to \( \hat{t} \), then \( \hat{t} \) is conjugate to \( \hat{s} \). +

    @@ -289,7 +306,7 @@

    Conjugate gradient method

    34
  • 35
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs026.html b/doc/pub/week6/html/._week6-bs026.html index 8848a82f..5ce5fcc3 100644 --- a/doc/pub/week6/html/._week6-bs026.html +++ b/doc/pub/week6/html/._week6-bs026.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -252,25 +254,14 @@

    Conjugate gradient method

    -

    Assume now that we have a symmetric positive-definite matrix \( \hat{A} \) of size -\( n\times n \). At each iteration \( i+1 \) we obtain the conjugate direction of a vector -

    +

    An example is given by the eigenvectors of the matrix

    $$ \begin{equation*} -\hat{x}_{i+1}=\hat{x}_{i}+\alpha_i\hat{p}_{i}. +\hat{v}_i^T\hat{A}\hat{v}_j= \lambda\hat{v}_i^T\hat{v}_j, \end{equation*} $$ -

    We assume that \( \hat{p}_{i} \) is a sequence of \( n \) mutually conjugate directions. -Then the \( \hat{p}_{i} \) form a basis of \( R^n \) and we can expand the solution -$ \hat{A}\hat{x} = \hat{b}$ in this basis, namely -

    - -$$ -\begin{equation*} - \hat{x} = \sum^{n}_{i=1} \alpha_i \hat{p}_i. -\end{equation*} -$$ +

    which is zero unless \( i=j \).

    @@ -300,7 +291,7 @@

    Conjugate gradient method

    35
  • 36
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs027.html b/doc/pub/week6/html/._week6-bs027.html index ef0091a8..33c64bf4 100644 --- a/doc/pub/week6/html/._week6-bs027.html +++ b/doc/pub/week6/html/._week6-bs027.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -252,26 +254,23 @@

    Conjugate gradient method

    -

    The coefficients are given by

    +

    Assume now that we have a symmetric positive-definite matrix \( \hat{A} \) of size +\( n\times n \). At each iteration \( i+1 \) we obtain the conjugate direction of a vector +

    $$ \begin{equation*} - \mathbf{A}\mathbf{x} = \sum^{n}_{i=1} \alpha_i \mathbf{A} \mathbf{p}_i = \mathbf{b}. +\hat{x}_{i+1}=\hat{x}_{i}+\alpha_i\hat{p}_{i}. \end{equation*} $$ -

    Multiplying with \( \hat{p}_k^T \) from the left gives

    +

    We assume that \( \hat{p}_{i} \) is a sequence of \( n \) mutually conjugate directions. +Then the \( \hat{p}_{i} \) form a basis of \( R^n \) and we can expand the solution +$ \hat{A}\hat{x} = \hat{b}$ in this basis, namely +

    $$ \begin{equation*} - \hat{p}_k^T \hat{A}\hat{x} = \sum^{n}_{i=1} \alpha_i\hat{p}_k^T \hat{A}\hat{p}_i= \hat{p}_k^T \hat{b}, -\end{equation*} -$$ - -

    and we can define the coefficients \( \alpha_k \) as

    - -$$ -\begin{equation*} - \alpha_k = \frac{\hat{p}_k^T \hat{b}}{\hat{p}_k^T \hat{A} \hat{p}_k} + \hat{x} = \sum^{n}_{i=1} \alpha_i \hat{p}_i. \end{equation*} $$
    @@ -303,7 +302,7 @@

    Conjugate gradient method

    36
  • 37
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs028.html b/doc/pub/week6/html/._week6-bs028.html index acc46bcf..11a83841 100644 --- a/doc/pub/week6/html/._week6-bs028.html +++ b/doc/pub/week6/html/._week6-bs028.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,36 +250,32 @@

     

     

     

    -

    Conjugate gradient method and iterations

    +

    Conjugate gradient method

    +

    The coefficients are given by

    +$$ +\begin{equation*} + \mathbf{A}\mathbf{x} = \sum^{n}_{i=1} \alpha_i \mathbf{A} \mathbf{p}_i = \mathbf{b}. +\end{equation*} +$$ -

    If we choose the conjugate vectors \( \hat{p}_k \) carefully, -then we may not need all of them to obtain a good approximation to the solution -\( \hat{x} \). -We want to regard the conjugate gradient method as an iterative method. -This will us to solve systems where \( n \) is so large that the direct -method would take too much time. -

    +

    Multiplying with \( \hat{p}_k^T \) from the left gives

    -

    We denote the initial guess for \( \hat{x} \) as \( \hat{x}_0 \). -We can assume without loss of generality that -

    $$ \begin{equation*} -\hat{x}_0=0, + \hat{p}_k^T \hat{A}\hat{x} = \sum^{n}_{i=1} \alpha_i\hat{p}_k^T \hat{A}\hat{p}_i= \hat{p}_k^T \hat{b}, \end{equation*} $$ -

    or consider the system

    +

    and we can define the coefficients \( \alpha_k \) as

    + $$ \begin{equation*} -\hat{A}\hat{z} = \hat{b}-\hat{A}\hat{x}_0, + \alpha_k = \frac{\hat{p}_k^T \hat{b}}{\hat{p}_k^T \hat{A} \hat{p}_k} \end{equation*} $$ - -

    instead.

    @@ -307,7 +305,7 @@

    Conjugate gradi
  • 37
  • 38
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs029.html b/doc/pub/week6/html/._week6-bs029.html index fad7ee08..d7d7c03f 100644 --- a/doc/pub/week6/html/._week6-bs029.html +++ b/doc/pub/week6/html/._week6-bs029.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,32 +250,36 @@

     

     

     

    -

    Conjugate gradient method

    +

    Conjugate gradient method and iterations

    -

    One can show that the solution \( \hat{x} \) is also the unique minimizer of the quadratic form

    + +

    If we choose the conjugate vectors \( \hat{p}_k \) carefully, +then we may not need all of them to obtain a good approximation to the solution +\( \hat{x} \). +We want to regard the conjugate gradient method as an iterative method. +This will us to solve systems where \( n \) is so large that the direct +method would take too much time. +

    + +

    We denote the initial guess for \( \hat{x} \) as \( \hat{x}_0 \). +We can assume without loss of generality that +

    $$ \begin{equation*} - f(\hat{x}) = \frac{1}{2}\hat{x}^T\hat{A}\hat{x} - \hat{x}^T \hat{x} , \quad \hat{x}\in\mathbf{R}^n. +\hat{x}_0=0, \end{equation*} $$ -

    This suggests taking the first basis vector \( \hat{p}_1 \) -to be the gradient of \( f \) at \( \hat{x}=\hat{x}_0 \), -which equals -

    +

    or consider the system

    $$ \begin{equation*} -\hat{A}\hat{x}_0-\hat{b}, +\hat{A}\hat{z} = \hat{b}-\hat{A}\hat{x}_0, \end{equation*} $$ -

    and -\( \hat{x}_0=0 \) it is equal \( -\hat{b} \). -The other vectors in the basis will be conjugate to the gradient, -hence the name conjugate gradient method. -

    +

    instead.

    @@ -303,7 +309,7 @@

    Conjugate gradient method

    38
  • 39
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs030.html b/doc/pub/week6/html/._week6-bs030.html index dfe4a4b0..eef4f1a3 100644 --- a/doc/pub/week6/html/._week6-bs030.html +++ b/doc/pub/week6/html/._week6-bs030.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -252,26 +254,28 @@

    Conjugate gradient method

    -

    Let \( \hat{r}_k \) be the residual at the \( k \)-th step:

    +

    One can show that the solution \( \hat{x} \) is also the unique minimizer of the quadratic form

    $$ \begin{equation*} -\hat{r}_k=\hat{b}-\hat{A}\hat{x}_k. + f(\hat{x}) = \frac{1}{2}\hat{x}^T\hat{A}\hat{x} - \hat{x}^T \hat{x} , \quad \hat{x}\in\mathbf{R}^n. \end{equation*} $$ -

    Note that \( \hat{r}_k \) is the negative gradient of \( f \) at -\( \hat{x}=\hat{x}_k \), -so the gradient descent method would be to move in the direction \( \hat{r}_k \). -Here, we insist that the directions \( \hat{p}_k \) are conjugate to each other, -so we take the direction closest to the gradient \( \hat{r}_k \) -under the conjugacy constraint. -This gives the following expression +

    This suggests taking the first basis vector \( \hat{p}_1 \) +to be the gradient of \( f \) at \( \hat{x}=\hat{x}_0 \), +which equals

    $$ \begin{equation*} -\hat{p}_{k+1}=\hat{r}_k-\frac{\hat{p}_k^T \hat{A}\hat{r}_k}{\hat{p}_k^T\hat{A}\hat{p}_k} \hat{p}_k. +\hat{A}\hat{x}_0-\hat{b}, \end{equation*} $$ + +

    and +\( \hat{x}_0=0 \) it is equal \( -\hat{b} \). +The other vectors in the basis will be conjugate to the gradient, +hence the name conjugate gradient method. +

    @@ -301,7 +305,7 @@

    Conjugate gradient method

    39
  • 40
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs031.html b/doc/pub/week6/html/._week6-bs031.html index f420846c..a4ca86b1 100644 --- a/doc/pub/week6/html/._week6-bs031.html +++ b/doc/pub/week6/html/._week6-bs031.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -252,33 +254,25 @@

    Conjugate gradient method

    -

    We can also compute the residual iteratively as

    +

    Let \( \hat{r}_k \) be the residual at the \( k \)-th step:

    $$ \begin{equation*} -\hat{r}_{k+1}=\hat{b}-\hat{A}\hat{x}_{k+1}, - \end{equation*} +\hat{r}_k=\hat{b}-\hat{A}\hat{x}_k. +\end{equation*} $$ -

    which equals

    +

    Note that \( \hat{r}_k \) is the negative gradient of \( f \) at +\( \hat{x}=\hat{x}_k \), +so the gradient descent method would be to move in the direction \( \hat{r}_k \). +Here, we insist that the directions \( \hat{p}_k \) are conjugate to each other, +so we take the direction closest to the gradient \( \hat{r}_k \) +under the conjugacy constraint. +This gives the following expression +

    $$ \begin{equation*} -\hat{b}-\hat{A}(\hat{x}_k+\alpha_k\hat{p}_k), - \end{equation*} -$$ - -

    or

    -$$ -\begin{equation*} -(\hat{b}-\hat{A}\hat{x}_k)-\alpha_k\hat{A}\hat{p}_k, - \end{equation*} -$$ - -

    which gives

    - -$$ -\begin{equation*} -\hat{r}_{k+1}=\hat{r}_k-\hat{A}\hat{p}_{k}, - \end{equation*} +\hat{p}_{k+1}=\hat{r}_k-\frac{\hat{p}_k^T \hat{A}\hat{r}_k}{\hat{p}_k^T\hat{A}\hat{p}_k} \hat{p}_k. +\end{equation*} $$
    @@ -309,7 +303,7 @@

    Conjugate gradient method

    40
  • 41
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs032.html b/doc/pub/week6/html/._week6-bs032.html index 3dd80c0c..b9b82a27 100644 --- a/doc/pub/week6/html/._week6-bs032.html +++ b/doc/pub/week6/html/._week6-bs032.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,55 +250,38 @@

     

     

     

    -

    Simple implementation of the Conjugate gradient algorithm

    +

    Conjugate gradient method

    +

    We can also compute the residual iteratively as

    +$$ +\begin{equation*} +\hat{r}_{k+1}=\hat{b}-\hat{A}\hat{x}_{k+1}, + \end{equation*} +$$ - -
    -
    -
    -
    -
    -
      Vector ConjugateGradient(Matrix A, Vector b, Vector x0){
    -  int dim = x0.Dimension();
    -  const double tolerance = 1.0e-14;
    -  Vector x(dim),r(dim),v(dim),z(dim);
    -  double c,t,d;
    +

    which equals

    +$$ +\begin{equation*} +\hat{b}-\hat{A}(\hat{x}_k+\alpha_k\hat{p}_k), + \end{equation*} +$$ - x = x0; - r = b - A*x; - v = r; - c = dot(r,r); - int i = 0; IterMax = dim; - while(i <= IterMax){ - z = A*v; - t = c/dot(v,z); - x = x + t*v; - r = r - t*z; - d = dot(r,r); - if(sqrt(d) < tolerance) - break; - v = r + (d/c)*v; - c = d; i++; - } - return x; -} -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +

    or

    +$$ +\begin{equation*} +(\hat{b}-\hat{A}\hat{x}_k)-\alpha_k\hat{A}\hat{p}_k, + \end{equation*} +$$ + +

    which gives

    + +$$ +\begin{equation*} +\hat{r}_{k+1}=\hat{r}_k-\hat{A}\hat{p}_{k}, + \end{equation*} +$$
    @@ -326,7 +311,7 @@

    41
  • 42
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs033.html b/doc/pub/week6/html/._week6-bs033.html index 854b3ee2..d732257b 100644 --- a/doc/pub/week6/html/._week6-bs033.html +++ b/doc/pub/week6/html/._week6-bs033.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,31 +250,55 @@

     

     

     

    -

    Broyden–Fletcher–Goldfarb–Shanno algorithm

    +

    Simple implementation of the Conjugate gradient algorithm

    -

    The optimization problem is to minimize \( f(\mathbf {x} ) \) where \( \mathbf {x} \) is a vector in \( R^{n} \), and \( f \) is a differentiable scalar function. There are no constraints on the values that \( \mathbf {x} \) can take.

    -

    The algorithm begins at an initial estimate for the optimal value \( \mathbf {x}_{0} \) and proceeds iteratively to get a better estimate at each stage.

    + +
    +
    +
    +
    +
    +
      Vector ConjugateGradient(Matrix A, Vector b, Vector x0){
    +  int dim = x0.Dimension();
    +  const double tolerance = 1.0e-14;
    +  Vector x(dim),r(dim),v(dim),z(dim);
    +  double c,t,d;
     
    -

    The search direction \( p_k \) at stage \( k \) is given by the solution of the analogue of the Newton equation

    -$$ -B_{k}\mathbf {p} _{k}=-\nabla f(\mathbf {x}_{k}), -$$ - -

    where \( B_{k} \) is an approximation to the Hessian matrix, which is -updated iteratively at each stage, and \( \nabla f(\mathbf {x} _{k}) \) -is the gradient of the function -evaluated at \( x_k \). -A line search in the direction \( p_k \) is then used to -find the next point \( x_{k+1} \) by minimising -

    -$$ -f(\mathbf {x}_{k}+\alpha \mathbf {p}_{k}), -$$ - -

    over the scalar \( \alpha > 0 \).

    + x = x0; + r = b - A*x; + v = r; + c = dot(r,r); + int i = 0; IterMax = dim; + while(i <= IterMax){ + z = A*v; + t = c/dot(v,z); + x = x + t*v; + r = r - t*z; + d = dot(r,r); + if(sqrt(d) < tolerance) + break; + v = r + (d/c)*v; + c = d; i++; + } + return x; +} +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    @@ -302,7 +328,7 @@

    Broyden–
  • 42
  • 43
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs034.html b/doc/pub/week6/html/._week6-bs034.html index c30ada46..42a372b7 100644 --- a/doc/pub/week6/html/._week6-bs034.html +++ b/doc/pub/week6/html/._week6-bs034.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,21 +250,34 @@

     

     

     

    -

    Stochastic Gradient Descent

    +

    Broyden–Fletcher–Goldfarb–Shanno algorithm

    +
    +
    + +

    The optimization problem is to minimize \( f(\mathbf {x} ) \) where \( \mathbf {x} \) is a vector in \( R^{n} \), and \( f \) is a differentiable scalar function. There are no constraints on the values that \( \mathbf {x} \) can take.

    -

    Stochastic gradient descent (SGD) and variants thereof address some of -the shortcomings of the Gradient descent method discussed above. -

    +

    The algorithm begins at an initial estimate for the optimal value \( \mathbf {x}_{0} \) and proceeds iteratively to get a better estimate at each stage.

    + +

    The search direction \( p_k \) at stage \( k \) is given by the solution of the analogue of the Newton equation

    +$$ +B_{k}\mathbf {p} _{k}=-\nabla f(\mathbf {x}_{k}), +$$ -

    The underlying idea of SGD comes from the observation that a given -function, which we want to minimize, can almost always be written as a -sum over \( n \) data points \( \{\mathbf{x}_i\}_{i=1}^n \), +

    where \( B_{k} \) is an approximation to the Hessian matrix, which is +updated iteratively at each stage, and \( \nabla f(\mathbf {x} _{k}) \) +is the gradient of the function +evaluated at \( x_k \). +A line search in the direction \( p_k \) is then used to +find the next point \( x_{k+1} \) by minimising

    $$ -C(\mathbf{\beta}) = \sum_{i=1}^n c_i(\mathbf{x}_i, -\mathbf{\beta}). +f(\mathbf {x}_{k}+\alpha \mathbf {p}_{k}), $$ +

    over the scalar \( \alpha > 0 \).

    +
    +
    +

    @@ -289,7 +304,7 @@

    Stochastic Gradient Descent
  • 43
  • 44
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs035.html b/doc/pub/week6/html/._week6-bs035.html index 36b9815c..44eef00c 100644 --- a/doc/pub/week6/html/._week6-bs035.html +++ b/doc/pub/week6/html/._week6-bs035.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,22 +250,21 @@

     

     

     

    -

    Computation of gradients

    +

    Stochastic Gradient Descent

    -

    This in turn means that the gradient can be -computed as a sum over \( i \)-gradients +

    Stochastic gradient descent (SGD) and variants thereof address some of +the shortcomings of the Gradient descent method discussed above. +

    + +

    The underlying idea of SGD comes from the observation that a given +function, which we want to minimize, can almost always be written as a +sum over \( n \) data points \( \{\mathbf{x}_i\}_{i=1}^n \),

    $$ -\nabla_\beta C(\mathbf{\beta}) = \sum_i^n \nabla_\beta c_i(\mathbf{x}_i, -\mathbf{\beta}). +C(\mathbf{\beta}) = \sum_{i=1}^n c_i(\mathbf{x}_i, +\mathbf{\beta}). $$ -

    Stochasticity/randomness is introduced by only taking the -gradient on a subset of the data called minibatches. If there are \( n \) -data points and the size of each minibatch is \( M \), there will be \( n/M \) -minibatches. We denote these minibatches by \( B_k \) where -\( k=1,\cdots,n/M \). -

    @@ -290,7 +291,7 @@

    Computation of gradients

  • 44
  • 45
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs036.html b/doc/pub/week6/html/._week6-bs036.html index 4d9dce72..e5e5376e 100644 --- a/doc/pub/week6/html/._week6-bs036.html +++ b/doc/pub/week6/html/._week6-bs036.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,28 +250,22 @@

     

     

     

    -

    SGD example

    -

    As an example, suppose we have \( 10 \) data points \( (\mathbf{x}_1,\cdots, \mathbf{x}_{10}) \) -and we choose to have \( M=5 \) minibathces, -then each minibatch contains two data points. In particular we have -\( B_1 = (\mathbf{x}_1,\mathbf{x}_2), \cdots, B_5 = -(\mathbf{x}_9,\mathbf{x}_{10}) \). Note that if you choose \( M=1 \) you -have only a single batch with all data points and on the other extreme, -you may choose \( M=n \) resulting in a minibatch for each datapoint, i.e -\( B_k = \mathbf{x}_k \). -

    +

    Computation of gradients

    -

    The idea is now to approximate the gradient by replacing the sum over -all data points with a sum over the data points in one the minibatches -picked at random in each gradient descent step +

    This in turn means that the gradient can be +computed as a sum over \( i \)-gradients

    $$ -\nabla_{\beta} -C(\mathbf{\beta}) = \sum_{i=1}^n \nabla_\beta c_i(\mathbf{x}_i, -\mathbf{\beta}) \rightarrow \sum_{i \in B_k}^n \nabla_\beta -c_i(\mathbf{x}_i, \mathbf{\beta}). +\nabla_\beta C(\mathbf{\beta}) = \sum_i^n \nabla_\beta c_i(\mathbf{x}_i, +\mathbf{\beta}). $$ +

    Stochasticity/randomness is introduced by only taking the +gradient on a subset of the data called minibatches. If there are \( n \) +data points and the size of each minibatch is \( M \), there will be \( n/M \) +minibatches. We denote these minibatches by \( B_k \) where +\( k=1,\cdots,n/M \). +

    @@ -295,6 +291,8 @@

    SGD example

  • 44
  • 45
  • 46
  • +
  • ...
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs037.html b/doc/pub/week6/html/._week6-bs037.html index 78d5c6d9..4384ec88 100644 --- a/doc/pub/week6/html/._week6-bs037.html +++ b/doc/pub/week6/html/._week6-bs037.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,20 +250,28 @@

     

     

     

    -

    The gradient step

    +

    SGD example

    +

    As an example, suppose we have \( 10 \) data points \( (\mathbf{x}_1,\cdots, \mathbf{x}_{10}) \) +and we choose to have \( M=5 \) minibathces, +then each minibatch contains two data points. In particular we have +\( B_1 = (\mathbf{x}_1,\mathbf{x}_2), \cdots, B_5 = +(\mathbf{x}_9,\mathbf{x}_{10}) \). Note that if you choose \( M=1 \) you +have only a single batch with all data points and on the other extreme, +you may choose \( M=n \) resulting in a minibatch for each datapoint, i.e +\( B_k = \mathbf{x}_k \). +

    -

    Thus a gradient descent step now looks like

    +

    The idea is now to approximate the gradient by replacing the sum over +all data points with a sum over the data points in one the minibatches +picked at random in each gradient descent step +

    $$ -\beta_{j+1} = \beta_j - \gamma_j \sum_{i \in B_k}^n \nabla_\beta c_i(\mathbf{x}_i, -\mathbf{\beta}) +\nabla_{\beta} +C(\mathbf{\beta}) = \sum_{i=1}^n \nabla_\beta c_i(\mathbf{x}_i, +\mathbf{\beta}) \rightarrow \sum_{i \in B_k}^n \nabla_\beta +c_i(\mathbf{x}_i, \mathbf{\beta}). $$ -

    where \( k \) is picked at random with equal -probability from \( [1,n/M] \). An iteration over the number of -minibathces (n/M) is commonly referred to as an epoch. Thus it is -typical to choose a number of epochs and for each epoch iterate over -the number of minibatches, as exemplified in the code below. -

    @@ -286,6 +296,7 @@

    The gradient step

  • 44
  • 45
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs038.html b/doc/pub/week6/html/._week6-bs038.html index c449298f..fc2a7d66 100644 --- a/doc/pub/week6/html/._week6-bs038.html +++ b/doc/pub/week6/html/._week6-bs038.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,51 +250,19 @@

     

     

     

    -

    Simple example code

    +

    The gradient step

    +

    Thus a gradient descent step now looks like

    +$$ +\beta_{j+1} = \beta_j - \gamma_j \sum_{i \in B_k}^n \nabla_\beta c_i(\mathbf{x}_i, +\mathbf{\beta}) +$$ - -
    -
    -
    -
    -
    -
    import numpy as np 
    -
    -n = 100 #100 datapoints 
    -M = 5   #size of each minibatch
    -m = int(n/M) #number of minibatches
    -n_epochs = 10 #number of epochs
    -
    -j = 0
    -for epoch in range(1,n_epochs+1):
    -    for i in range(m):
    -        k = np.random.randint(m) #Pick the k-th minibatch at random
    -        #Compute the gradient using the data in minibatch Bk
    -        #Compute new suggestion for 
    -        j += 1
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

    Taking the gradient only on a subset of the data has two important -benefits. First, it introduces randomness which decreases the chance -that our opmization scheme gets stuck in a local minima. Second, if -the size of the minibatches are small relative to the number of -datapoints (\( M < n \)), the computation of the gradient is much -cheaper since we sum over the datapoints in the \( k-th \) minibatch and not -all \( n \) datapoints. +

    where \( k \) is picked at random with equal +probability from \( [1,n/M] \). An iteration over the number of +minibathces (n/M) is commonly referred to as an epoch. Thus it is +typical to choose a number of epochs and for each epoch iterate over +the number of minibatches, as exemplified in the code below.

    @@ -317,6 +287,7 @@

    Simple example code

  • 44
  • 45
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs039.html b/doc/pub/week6/html/._week6-bs039.html index 216815ef..cc0ef023 100644 --- a/doc/pub/week6/html/._week6-bs039.html +++ b/doc/pub/week6/html/._week6-bs039.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,18 +250,51 @@

     

     

     

    -

    When do we stop?

    +

    Simple example code

    -

    A natural question is when do we stop the search for a new minimum? -One possibility is to compute the full gradient after a given number -of epochs and check if the norm of the gradient is smaller than some -threshold and stop if true. However, the condition that the gradient -is zero is valid also for local minima, so this would only tell us -that we are close to a local/global minimum. However, we could also -evaluate the cost function at this point, store the result and -continue the search. If the test kicks in at a later stage we can -compare the values of the cost function and keep the \( \beta \) that -gave the lowest value. + + +

    +
    +
    +
    +
    +
    import numpy as np 
    +
    +n = 100 #100 datapoints 
    +M = 5   #size of each minibatch
    +m = int(n/M) #number of minibatches
    +n_epochs = 10 #number of epochs
    +
    +j = 0
    +for epoch in range(1,n_epochs+1):
    +    for i in range(m):
    +        k = np.random.randint(m) #Pick the k-th minibatch at random
    +        #Compute the gradient using the data in minibatch Bk
    +        #Compute new suggestion for 
    +        j += 1
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    Taking the gradient only on a subset of the data has two important +benefits. First, it introduces randomness which decreases the chance +that our opmization scheme gets stuck in a local minima. Second, if +the size of the minibatches are small relative to the number of +datapoints (\( M < n \)), the computation of the gradient is much +cheaper since we sum over the datapoints in the \( k-th \) minibatch and not +all \( n \) datapoints.

    @@ -283,6 +318,7 @@

    When do we stop?

  • 44
  • 45
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs040.html b/doc/pub/week6/html/._week6-bs040.html index 131bffff..61dc8c38 100644 --- a/doc/pub/week6/html/._week6-bs040.html +++ b/doc/pub/week6/html/._week6-bs040.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,69 +250,20 @@

     

     

     

    -

    Slightly different approach

    +

    When do we stop?

    -

    Another approach is to let the step length \( \gamma_j \) depend on the -number of epochs in such a way that it becomes very small after a -reasonable time such that we do not move at all. +

    A natural question is when do we stop the search for a new minimum? +One possibility is to compute the full gradient after a given number +of epochs and check if the norm of the gradient is smaller than some +threshold and stop if true. However, the condition that the gradient +is zero is valid also for local minima, so this would only tell us +that we are close to a local/global minimum. However, we could also +evaluate the cost function at this point, store the result and +continue the search. If the test kicks in at a later stage we can +compare the values of the cost function and keep the \( \beta \) that +gave the lowest value.

    -

    As an example, let \( e = 0,1,2,3,\cdots \) denote the current epoch and let \( t_0, t_1 > 0 \) be two fixed numbers. Furthermore, let \( t = e \cdot m + i \) where \( m \) is the number of minibatches and \( i=0,\cdots,m-1 \). Then the function $$\gamma_j(t; t_0, t_1) = \frac{t_0}{t+t_1} $$ goes to zero as the number of epochs gets large. I.e. we start with a step length \( \gamma_j (0; t_0, t_1) = t_0/t_1 \) which decays in time \( t \).

    - -

    In this way we can fix the number of epochs, compute \( \beta \) and -evaluate the cost function at the end. Repeating the computation will -give a different result since the scheme is random by design. Then we -pick the final \( \beta \) that gives the lowest value of the cost -function. -

    - - - -
    -
    -
    -
    -
    -
    import numpy as np 
    -
    -def step_length(t,t0,t1):
    -    return t0/(t+t1)
    -
    -n = 100 #100 datapoints 
    -M = 5   #size of each minibatch
    -m = int(n/M) #number of minibatches
    -n_epochs = 500 #number of epochs
    -t0 = 1.0
    -t1 = 10
    -
    -gamma_j = t0/t1
    -j = 0
    -for epoch in range(1,n_epochs+1):
    -    for i in range(m):
    -        k = np.random.randint(m) #Pick the k-th minibatch at random
    -        #Compute the gradient using the data in minibatch Bk
    -        #Compute new suggestion for beta
    -        t = epoch*m+i
    -        gamma_j = step_length(t,t0,t1)
    -        j += 1
    -
    -print("gamma_j after %d epochs: %g" % (n_epochs,gamma_j))
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - -

      @@ -331,6 +284,7 @@

      Slightly different approach
    • 44
    • 45
    • 46
    • +
    • 47
    • »
    diff --git a/doc/pub/week6/html/._week6-bs041.html b/doc/pub/week6/html/._week6-bs041.html index 18c31396..382ac30f 100644 --- a/doc/pub/week6/html/._week6-bs041.html +++ b/doc/pub/week6/html/._week6-bs041.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,7 +250,21 @@

     

     

     

    -

    Program for stochastic gradient

    +

    Slightly different approach

    + +

    Another approach is to let the step length \( \gamma_j \) depend on the +number of epochs in such a way that it becomes very small after a +reasonable time such that we do not move at all. +

    + +

    As an example, let \( e = 0,1,2,3,\cdots \) denote the current epoch and let \( t_0, t_1 > 0 \) be two fixed numbers. Furthermore, let \( t = e \cdot m + i \) where \( m \) is the number of minibatches and \( i=0,\cdots,m-1 \). Then the function $$\gamma_j(t; t_0, t_1) = \frac{t_0}{t+t_1} $$ goes to zero as the number of epochs gets large. I.e. we start with a step length \( \gamma_j (0; t_0, t_1) = t_0/t_1 \) which decays in time \( t \).

    + +

    In this way we can fix the number of epochs, compute \( \beta \) and +evaluate the cost function at the end. Repeating the computation will +give a different result since the scheme is random by design. Then we +pick the final \( \beta \) that gives the lowest value of the cost +function. +

    @@ -257,72 +273,30 @@

    Program for stochastic g
    -
    # Importing various packages
    -from math import exp, sqrt
    -from random import random, seed
    -import numpy as np
    -import matplotlib.pyplot as plt
    -from sklearn.linear_model import SGDRegressor
    -
    -x = 2*np.random.rand(100,1)
    -y = 4+3*x+np.random.randn(100,1)
    -
    -xb = np.c_[np.ones((100,1)), x]
    -theta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y)
    -print("Own inversion")
    -print(theta_linreg)
    -sgdreg = SGDRegressor(n_iter = 50, penalty=None, eta0=0.1)
    -sgdreg.fit(x,y.ravel())
    -print("sgdreg from scikit")
    -print(sgdreg.intercept_, sgdreg.coef_)
    -
    +  
    import numpy as np 
     
    -theta = np.random.randn(2,1)
    -
    -eta = 0.1
    -Niterations = 1000
    -m = 100
    -
    -for iter in range(Niterations):
    -    gradients = 2.0/m*xb.T.dot(xb.dot(theta)-y)
    -    theta -= eta*gradients
    -print("theta frm own gd")
    -print(theta)
    -
    -xnew = np.array([[0],[2]])
    -xbnew = np.c_[np.ones((2,1)), xnew]
    -ypredict = xbnew.dot(theta)
    -ypredict2 = xbnew.dot(theta_linreg)
    -
    -
    -n_epochs = 50
    -t0, t1 = 5, 50
    -m = 100
    -def learning_schedule(t):
    +def step_length(t,t0,t1):
         return t0/(t+t1)
     
    -theta = np.random.randn(2,1)
    +n = 100 #100 datapoints 
    +M = 5   #size of each minibatch
    +m = int(n/M) #number of minibatches
    +n_epochs = 500 #number of epochs
    +t0 = 1.0
    +t1 = 10
     
    -for epoch in range(n_epochs):
    +gamma_j = t0/t1
    +j = 0
    +for epoch in range(1,n_epochs+1):
         for i in range(m):
    -        random_index = np.random.randint(m)
    -        xi = xb[random_index:random_index+1]
    -        yi = y[random_index:random_index+1]
    -        gradients = 2 * xi.T.dot(xi.dot(theta)-yi)
    -        eta = learning_schedule(epoch*m+i)
    -        theta = theta - eta*gradients
    -print("theta from own sdg")
    -print(theta)
    -
    +        k = np.random.randint(m) #Pick the k-th minibatch at random
    +        #Compute the gradient using the data in minibatch Bk
    +        #Compute new suggestion for beta
    +        t = epoch*m+i
    +        gamma_j = step_length(t,t0,t1)
    +        j += 1
     
    -plt.plot(xnew, ypredict, "r-")
    -plt.plot(xnew, ypredict2, "b-")
    -plt.plot(x, y ,'ro')
    -plt.axis([0,2.0,0, 15.0])
    -plt.xlabel(r'$x$')
    -plt.ylabel(r'$y$')
    -plt.title(r'Random numbers ')
    -plt.show()
    +print("gamma_j after %d epochs: %g" % (n_epochs,gamma_j))
     
    @@ -358,6 +332,7 @@

    Program for stochastic g
  • 44
  • 45
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs042.html b/doc/pub/week6/html/._week6-bs042.html index 87e8dc4c..445193fa 100644 --- a/doc/pub/week6/html/._week6-bs042.html +++ b/doc/pub/week6/html/._week6-bs042.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,16 +250,97 @@

     

     

     

    -

    Using gradient descent methods, limitations

    +

    Program for stochastic gradient

    + + + +
    +
    +
    +
    +
    +
    # Importing various packages
    +from math import exp, sqrt
    +from random import random, seed
    +import numpy as np
    +import matplotlib.pyplot as plt
    +from sklearn.linear_model import SGDRegressor
    +
    +x = 2*np.random.rand(100,1)
    +y = 4+3*x+np.random.randn(100,1)
    +
    +xb = np.c_[np.ones((100,1)), x]
    +theta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y)
    +print("Own inversion")
    +print(theta_linreg)
    +sgdreg = SGDRegressor(n_iter = 50, penalty=None, eta0=0.1)
    +sgdreg.fit(x,y.ravel())
    +print("sgdreg from scikit")
    +print(sgdreg.intercept_, sgdreg.coef_)
    +
    +
    +theta = np.random.randn(2,1)
    +
    +eta = 0.1
    +Niterations = 1000
    +m = 100
    +
    +for iter in range(Niterations):
    +    gradients = 2.0/m*xb.T.dot(xb.dot(theta)-y)
    +    theta -= eta*gradients
    +print("theta frm own gd")
    +print(theta)
    +
    +xnew = np.array([[0],[2]])
    +xbnew = np.c_[np.ones((2,1)), xnew]
    +ypredict = xbnew.dot(theta)
    +ypredict2 = xbnew.dot(theta_linreg)
    +
    +
    +n_epochs = 50
    +t0, t1 = 5, 50
    +m = 100
    +def learning_schedule(t):
    +    return t0/(t+t1)
    +
    +theta = np.random.randn(2,1)
    +
    +for epoch in range(n_epochs):
    +    for i in range(m):
    +        random_index = np.random.randint(m)
    +        xi = xb[random_index:random_index+1]
    +        yi = y[random_index:random_index+1]
    +        gradients = 2 * xi.T.dot(xi.dot(theta)-yi)
    +        eta = learning_schedule(epoch*m+i)
    +        theta = theta - eta*gradients
    +print("theta from own sdg")
    +print(theta)
    +
    +
    +plt.plot(xnew, ypredict, "r-")
    +plt.plot(xnew, ypredict2, "b-")
    +plt.plot(x, y ,'ro')
    +plt.axis([0,2.0,0, 15.0])
    +plt.xlabel(r'$x$')
    +plt.ylabel(r'$y$')
    +plt.title(r'Random numbers ')
    +plt.show()
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + -
      -
    • Gradient descent (GD) finds local minima of our function. Since the GD algorithm is deterministic, if it converges, it will converge to a local minimum of our energy function. Because in ML we are often dealing with extremely rugged landscapes with many local minima, this can lead to poor performance.
    • -
    • GD is sensitive to initial conditions. One consequence of the local nature of GD is that initial conditions matter. Depending on where one starts, one will end up at a different local minima. Therefore, it is very important to think about how one initializes the training process. This is true for GD as well as more complicated variants of GD.
    • -
    • Gradients are computationally expensive to calculate for large datasets. In many cases in statistics and ML, the energy function is a sum of terms, with one term for each data point. For example, in linear regression, \( E \propto \sum_{i=1}^n (y_i - \mathbf{w}^T\cdot\mathbf{x}_i)^2 \); for logistic regression, the square error is replaced by the cross entropy. To calculate the gradient we have to sum over all \( n \) data points. Doing this at every GD step becomes extremely computationally expensive. An ingenious solution to this, is to calculate the gradients using small subsets of the data called "mini batches". This has the added benefit of introducing stochasticity into our algorithm.
    • -
    • GD is very sensitive to choices of learning rates. GD is extremely sensitive to the choice of learning rates. If the learning rate is very small, the training process take an extremely long time. For larger learning rates, GD can diverge and give poor results. Furthermore, depending on what the local landscape looks like, we have to modify the learning rates to ensure convergence. Ideally, we would adaptively choose the learning rates to match the landscape.
    • -
    • GD treats all directions in parameter space uniformly. Another major drawback of GD is that unlike Newton's method, the learning rate for GD is the same in all directions in parameter space. For this reason, the maximum learning rate is set by the behavior of the steepest direction and this can significantly slow down training. Ideally, we would like to take large steps in flat directions and small steps in steep directions. Since we are exploring rugged landscapes where curvatures change, this requires us to keep track of not only the gradient but second derivatives. The ideal scenario would be to calculate the Hessian but this proves to be too computationally expensive.
    • -
    • GD can take exponential time to escape saddle points, even with random initialization. As we mentioned, GD is extremely sensitive to initial condition since it determines the particular local minimum GD would eventually reach. However, even with a good initialization scheme, through the introduction of randomness, GD can still take exponential time to escape saddle points.
    • -

      @@ -276,6 +359,7 @@

      Using gradien
    • 44
    • 45
    • 46
    • +
    • 47
    • »
    diff --git a/doc/pub/week6/html/._week6-bs043.html b/doc/pub/week6/html/._week6-bs043.html index b85930ad..6fb3be9b 100644 --- a/doc/pub/week6/html/._week6-bs043.html +++ b/doc/pub/week6/html/._week6-bs043.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,23 +250,16 @@

     

     

     

    -

    Codes from numerical recipes

    -
    -
    - -

    You can however use codes we have adapted from the text Numerical Recipes in C++, see chapter 10.7. -Here we present a program, which you also can find at the webpage of the course we use the functions dfpmin and lnsrch. This is a variant of the Broyden et al algorithm discussed in the previous slide. -

    +

    Using gradient descent methods, limitations

      -
    • The program uses the harmonic oscillator in one dimensions as example.
    • -
    • The program does not use armadillo to handle vectors and matrices, but employs rather my own vector-matrix class. These auxiliary functions, and the main program model.cpp can all be found under the program link here.
    • +
    • Gradient descent (GD) finds local minima of our function. Since the GD algorithm is deterministic, if it converges, it will converge to a local minimum of our energy function. Because in ML we are often dealing with extremely rugged landscapes with many local minima, this can lead to poor performance.
    • +
    • GD is sensitive to initial conditions. One consequence of the local nature of GD is that initial conditions matter. Depending on where one starts, one will end up at a different local minima. Therefore, it is very important to think about how one initializes the training process. This is true for GD as well as more complicated variants of GD.
    • +
    • Gradients are computationally expensive to calculate for large datasets. In many cases in statistics and ML, the energy function is a sum of terms, with one term for each data point. For example, in linear regression, \( E \propto \sum_{i=1}^n (y_i - \mathbf{w}^T\cdot\mathbf{x}_i)^2 \); for logistic regression, the square error is replaced by the cross entropy. To calculate the gradient we have to sum over all \( n \) data points. Doing this at every GD step becomes extremely computationally expensive. An ingenious solution to this, is to calculate the gradients using small subsets of the data called "mini batches". This has the added benefit of introducing stochasticity into our algorithm.
    • +
    • GD is very sensitive to choices of learning rates. GD is extremely sensitive to the choice of learning rates. If the learning rate is very small, the training process take an extremely long time. For larger learning rates, GD can diverge and give poor results. Furthermore, depending on what the local landscape looks like, we have to modify the learning rates to ensure convergence. Ideally, we would adaptively choose the learning rates to match the landscape.
    • +
    • GD treats all directions in parameter space uniformly. Another major drawback of GD is that unlike Newton's method, the learning rate for GD is the same in all directions in parameter space. For this reason, the maximum learning rate is set by the behavior of the steepest direction and this can significantly slow down training. Ideally, we would like to take large steps in flat directions and small steps in steep directions. Since we are exploring rugged landscapes where curvatures change, this requires us to keep track of not only the gradient but second derivatives. The ideal scenario would be to calculate the Hessian but this proves to be too computationally expensive.
    • +
    • GD can take exponential time to escape saddle points, even with random initialization. As we mentioned, GD is extremely sensitive to initial condition since it determines the particular local minimum GD would eventually reach. However, even with a good initialization scheme, through the introduction of randomness, GD can still take exponential time to escape saddle points.
    -

    Below we show only excerpts from the main program. For the full program, see the above link.

    -
    -
    - -

      @@ -282,6 +277,7 @@

      Codes from numerical recipe
    • 44
    • 45
    • 46
    • +
    • 47
    • »
    diff --git a/doc/pub/week6/html/._week6-bs044.html b/doc/pub/week6/html/._week6-bs044.html index 39e688bd..6ce00641 100644 --- a/doc/pub/week6/html/._week6-bs044.html +++ b/doc/pub/week6/html/._week6-bs044.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,52 +250,19 @@

     

     

     

    -

    Finding the minimum of the harmonic oscillator model in one dimension

    +

    Codes from numerical recipes

    +

    You can however use codes we have adapted from the text Numerical Recipes in C++, see chapter 10.7. +Here we present a program, which you also can find at the webpage of the course we use the functions dfpmin and lnsrch. This is a variant of the Broyden et al algorithm discussed in the previous slide. +

    - -
    -
    -
    -
    -
    -
    //   Main function begins here
    -int main()
    -{
    -     int n, iter;
    -     double gtol, fret;
    -     double alpha;
    -     n = 1;
    -//   reserve space in memory for vectors containing the variational
    -//   parameters
    -     Vector g(n), p(n);
    -     cout << "Read in guess for alpha" << endl;
    -     cin >> alpha;
    -     gtol = 1.0e-5;
    -//   now call dfmin and compute the minimum
    -     p(0) = alpha;
    -     dfpmin(p, n, gtol, &iter, &fret, Efunction, dEfunction);
    -     cout << "Value of energy minimum = " << fret << endl;
    -     cout << "Number of iterations = " << iter << endl;
    -     cout << "Value of alpha at minimum = " << p(0) << endl;
    -      return 0;
    -}  // end of main program
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
      +
    • The program uses the harmonic oscillator in one dimensions as example.
    • +
    • The program does not use armadillo to handle vectors and matrices, but employs rather my own vector-matrix class. These auxiliary functions, and the main program model.cpp can all be found under the program link here.
    • +
    +

    Below we show only excerpts from the main program. For the full program, see the above link.

    @@ -314,6 +283,7 @@

    44
  • 45
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs045.html b/doc/pub/week6/html/._week6-bs045.html index bb4b96db..34c3f875 100644 --- a/doc/pub/week6/html/._week6-bs045.html +++ b/doc/pub/week6/html/._week6-bs045.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -248,15 +250,10 @@

     

     

     

    -

    Functions to observe

    +

    Finding the minimum of the harmonic oscillator model in one dimension

    -

    The functions Efunction and dEfunction compute the expectation value of the energy and its derivative. -They use the the quasi-Newton method of Broyden, Fletcher, Goldfarb, and Shanno (BFGS) -It uses the first derivatives only. The BFGS algorithm has proven good performance even for non-smooth optimizations. -These functions need to be changed when you want to your own derivatives. -

    @@ -264,18 +261,27 @@

    Functions to observe

    -
    //  this function defines the expectation value of the local energy
    -double Efunction(Vector  &x)
    +  
    //   Main function begins here
    +int main()
     {
    -  double value = x(0)*x(0)*0.5+1.0/(8*x(0)*x(0));
    -  return value;
    -} // end of function to evaluate
    -
    -//  this function defines the derivative of the energy 
    -void dEfunction(Vector &x, Vector &g)
    -{
    -  g(0) = x(0)-1.0/(4*x(0)*x(0)*x(0));
    -} // end of function to evaluate
    +     int n, iter;
    +     double gtol, fret;
    +     double alpha;
    +     n = 1;
    +//   reserve space in memory for vectors containing the variational
    +//   parameters
    +     Vector g(n), p(n);
    +     cout << "Read in guess for alpha" << endl;
    +     cin >> alpha;
    +     gtol = 1.0e-5;
    +//   now call dfmin and compute the minimum
    +     p(0) = alpha;
    +     dfpmin(p, n, gtol, &iter, &fret, Efunction, dEfunction);
    +     cout << "Value of energy minimum = " << fret << endl;
    +     cout << "Number of iterations = " << iter << endl;
    +     cout << "Value of alpha at minimum = " << p(0) << endl;
    +      return 0;
    +}  // end of main program
     
    @@ -290,12 +296,6 @@

    Functions to observe

    - -

    You need to change these functions in order to compute the local energy for your system. I used 1000 -cycles per call to get a new value of \( \langle E_L[\alpha]\rangle \). -When I compute the local energy I also compute its derivative. -After roughly 10-20 iterations I got a converged result in terms of \( \alpha \). -

    @@ -315,6 +315,8 @@

    Functions to observe

  • 44
  • 45
  • 46
  • +
  • 47
  • +
  • »
  • diff --git a/doc/pub/week6/html/._week6-bs046.html b/doc/pub/week6/html/._week6-bs046.html index c2daa80e..2905d021 100644 --- a/doc/pub/week6/html/._week6-bs046.html +++ b/doc/pub/week6/html/._week6-bs046.html @@ -8,8 +8,8 @@ - -Week 8 February 20-24: Gradient Methods + +Week 8 February 19-23: Gradient Methods @@ -36,53 +36,7 @@
  • Overview of week 8, February 20-24
  • -
  • Top-down start
  • -
  • Motivation
  • -
  • Simple example and demonstration
  • -
  • Simple example and demonstration
  • -
  • Exercise 1: Find the local energy for the harmonic oscillator
  • -
  • Variance in the simple model
  • -
  • Computing the derivatives
  • -
  • Expressions for finding the derivatives of the local energy
  • -
  • Derivatives of the local energy
  • -
  • Exercise 2: General expression for the derivative of the energy
  • -
  • Python program for 2-electrons in 2 dimensions
  • -
  • Using Broyden's algorithm in scipy
  • -
  • Brief reminder on Newton-Raphson's method
  • -
  • The equations
  • -
  • Simple geometric interpretation
  • -
  • Extending to more than one variable
  • -
  • Steepest descent
  • -
  • More on Steepest descent
  • -
  • The ideal
  • -
  • The sensitiveness of the gradient descent
  • -
  • Convex functions
  • -
  • Convex function
  • -
  • Conditions on convex functions
  • -
  • More on convex functions
  • -
  • Some simple problems
  • -
  • Standard steepest descent
  • -
  • Gradient method
  • -
  • Steepest descent method
  • -
  • Steepest descent method
  • -
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Overview
  • +
  • Brief reminder on Newton-Raphson's method
  • +
  • The equations
  • +
  • Simple geometric interpretation
  • +
  • Extending to more than one variable
  • +
  • Steepest descent
  • +
  • More on Steepest descent
  • +
  • The ideal
  • +
  • The sensitiveness of the gradient descent
  • +
  • Convex functions
  • +
  • Convex function
  • +
  • Conditions on convex functions
  • +
  • More on convex functions
  • +
  • Some simple problems
  • +
  • Standard steepest descent
  • +
  • Gradient method
  • +
  • Steepest descent method
  • +
  • Steepest descent method
  • +
  • Final expressions
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -306,20 +250,56 @@

     

     

     

    -

    Stochastic Gradient Descent

    - -

    Stochastic gradient descent (SGD) and variants thereof address some of -the shortcomings of the Gradient descent method discussed above. +

    Functions to observe

    +
    +
    + +

    The functions Efunction and dEfunction compute the expectation value of the energy and its derivative. +They use the the quasi-Newton method of Broyden, Fletcher, Goldfarb, and Shanno (BFGS) +It uses the first derivatives only. The BFGS algorithm has proven good performance even for non-smooth optimizations. +These functions need to be changed when you want to your own derivatives.

    -

    The underlying idea of SGD comes from the observation that a given -function, which we want to minimize, can almost always be written as a -sum over \( n \) data points \( \{\mathbf{x}_i\}_{i=1}^n \), + +

    +
    +
    +
    +
    +
    //  this function defines the expectation value of the local energy
    +double Efunction(Vector  &x)
    +{
    +  double value = x(0)*x(0)*0.5+1.0/(8*x(0)*x(0));
    +  return value;
    +} // end of function to evaluate
    +
    +//  this function defines the derivative of the energy 
    +void dEfunction(Vector &x, Vector &g)
    +{
    +  g(0) = x(0)-1.0/(4*x(0)*x(0)*x(0));
    +} // end of function to evaluate
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +
    + +

    You need to change these functions in order to compute the local energy for your system. I used 1000 +cycles per call to get a new value of \( \langle E_L[\alpha]\rangle \). +When I compute the local energy I also compute its derivative. +After roughly 10-20 iterations I got a converged result in terms of \( \alpha \).

    -$$ -C(\mathbf{\beta}) = \sum_{i=1}^n c_i(\mathbf{x}_i, -\mathbf{\beta}). -$$ +
    +

    @@ -337,18 +317,6 @@

    Stochastic Gradient Descent
  • 45
  • 46
  • 47
  • -
  • 48
  • -
  • 49
  • -
  • 50
  • -
  • 51
  • -
  • 52
  • -
  • 53
  • -
  • 54
  • -
  • 55
  • -
  • 56
  • -
  • ...
  • -
  • 58
  • -
  • »
  • diff --git a/doc/pub/week6/html/week6-bs.html b/doc/pub/week6/html/week6-bs.html index 66314f1e..816dfa3a 100644 --- a/doc/pub/week6/html/week6-bs.html +++ b/doc/pub/week6/html/week6-bs.html @@ -36,7 +36,7 @@
  • Overview of week 8
  • +
  • Overview
  • Brief reminder on Newton-Raphson's method
  • The equations
  • Simple geometric interpretation
  • @@ -211,32 +212,33 @@
  • Steepest descent method
  • Steepest descent method
  • Final expressions
  • -
  • Code examples for steepest descent
  • -
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • -
  • The routine for the steepest descent method
  • -
  • Steepest descent example
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method and iterations
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Conjugate gradient method
  • -
  • Simple implementation of the Conjugate gradient algorithm
  • -
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • -
  • Stochastic Gradient Descent
  • -
  • Computation of gradients
  • -
  • SGD example
  • -
  • The gradient step
  • -
  • Simple example code
  • -
  • When do we stop?
  • -
  • Slightly different approach
  • -
  • Program for stochastic gradient
  • -
  • Using gradient descent methods, limitations
  • -
  • Codes from numerical recipes
  • -
  • Finding the minimum of the harmonic oscillator model in one dimension
  • -
  • Functions to observe
  • +
  • Our simple \( 2\times 2 \) example
  • +
  • Derivatives and more
  • +
  • Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come
  • +
  • The routine for the steepest descent method
  • +
  • Steepest descent example
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method and iterations
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Conjugate gradient method
  • +
  • Simple implementation of the Conjugate gradient algorithm
  • +
  • Broyden–Fletcher–Goldfarb–Shanno algorithm
  • +
  • Stochastic Gradient Descent
  • +
  • Computation of gradients
  • +
  • SGD example
  • +
  • The gradient step
  • +
  • Simple example code
  • +
  • When do we stop?
  • +
  • Slightly different approach
  • +
  • Program for stochastic gradient
  • +
  • Using gradient descent methods, limitations
  • +
  • Codes from numerical recipes
  • +
  • Finding the minimum of the harmonic oscillator model in one dimension
  • +
  • Functions to observe
  • @@ -266,7 +268,7 @@

    Week 8 February 19-23: Gradient Methods


    -

    Jan 1, 2024

    +

    February 23, 2024


    @@ -291,7 +293,7 @@

    Jan 1, 2024

  • 9
  • 10
  • ...
  • -
  • 46
  • +
  • 47
  • »
  • diff --git a/doc/pub/week6/html/week6-reveal.html b/doc/pub/week6/html/week6-reveal.html index e81e3f08..7d127766 100644 --- a/doc/pub/week6/html/week6-reveal.html +++ b/doc/pub/week6/html/week6-reveal.html @@ -184,7 +184,7 @@

    Week 8 February 19-23: Gradient Methods


    -

    Jan 1, 2024

    +

    February 23, 2024


    @@ -195,7 +195,7 @@

    Jan 1, 2024

    -

    Overview of week 8

    +

    Overview

    Topics

    @@ -235,7 +235,7 @@

    Overview of week 8

    Brief reminder on Newton-Raphson's method

    -

    Let us quickly remind ourselves how we derive the above method.

    +

    Let us quickly remind ourselves on how we derive the above method.

    Perhaps the most celebrated of all one-dimensional root-finding routines is Newton's method, also called the Newton-Raphson @@ -740,7 +740,75 @@

    Final expressions

    -

    Code examples for steepest descent

    +

    Our simple \( 2\times 2 \) example

    + +

    Last week we introduced the simple two-dimensional function

    +

     
    +$$ +f(x_1,x_2)=x_1^2+x_1x_2+10x_2^2-5x_1-3x_2, +$$ +

     
    + +

    which is of the form (in terms of vectors and matrices)

    +

     
    +$$ +f(\boldsymbol{x})=\frac{1}{2}\boldsymbol{x}^T\boldsymbol{A}\boldsymbol{x}-\boldsymbol{b}^T\boldsymbol{x}, +$$ +

     
    + +

    where we have

    +

     
    +$$ +\boldsymbol{x}=\begin{bmatrix} x_1 \\ x_2\end{bmatrix}, +$$ +

     
    + +

     
    +$$ +\boldsymbol{b}=\begin{bmatrix} 5 \\ 3\end{bmatrix}, +$$ +

     
    + +

    and

    +

     
    +$$ +\boldsymbol{A}=\begin{bmatrix} 2 & 1\\ 1& 20\end{bmatrix}. +$$ +

     
    +

    + +
    +

    Derivatives and more

    + +

    Optimizing the above equation, that is

    +

     
    +$$ +\nabla f = 0 = \boldsymbol{A}\boldsymbol{x}-\boldsymbol{b}, +$$ +

     
    + +

    which leads to a simple matrix-inversion problem

    +

     
    +$$ +\boldsymbol{x}=\boldsymbol{A}^{-1}\boldsymbol{b}. +$$ +

     
    + +

    This problem is easy to solve since we can calculate the inverse. Alternatively, we can solve the two coupled equations with two unknowns

    +

     
    +$$ +\frac{\partial f}{\partial x_1}=2x_1+x_2-5=0, +$$ +

     
    + +

    and

    +

     
    +$$ +\frac{\partial f}{\partial x_2}=x_1+20x_2-3=0, +$$ +

     
    + +

    with solutions \( x_1=97/39 \) and \( x_2=1/39 \).

    @@ -770,8 +838,8 @@

    0) = x0(1) = 0; // Set the matrix - A(0,0) = 3; A(1,0) = 2; A(0,1) = 2; A(1,1) = 6; - b(0) = 2; b(1) = -8; + A(0,0) = 2; A(1,0) = 1; A(0,1) = 1; A(1,1) = 20; + b(0) = 5; b(1) = 3; cout << "The Matrix A that we are using: " << endl; A.Print(); cout << endl; @@ -866,15 +934,15 @@

    Steepest descent example

    from mpl_toolkits.mplot3d import axes3d def f(x): - return 0.5*x[0]**2 + 2.5*x[1]**2 + return x[0]**2 + 10.0*x[1]**2+x[0]*x[1]-5.0*x[0]-3*x[2] def df(x): - return np.array([x[0], 5*x[1]]) + return np.array(2*[x[0]+x[1]-5.0, x[0]+20*x[1]]-3.0) fig = pt.figure() ax = fig.gca(projection="3d") -xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j] +xmesh, ymesh = np.mgrid[-2:3:00j,-2:3:00j] fmesh = f(np.array([xmesh, ymesh])) ax.plot_surface(xmesh, ymesh, fmesh) @@ -902,7 +970,7 @@

    Steepest descent example

    pt.axis("equal")
     pt.contour(xmesh, ymesh, fmesh)
    -guesses = [np.array([2, 2./5])]
    +guesses = [np.array([3.0, 0.05])]
     
    diff --git a/doc/pub/week6/html/week6-solarized.html b/doc/pub/week6/html/week6-solarized.html index d9b357e5..bbd9d53e 100644 --- a/doc/pub/week6/html/week6-solarized.html +++ b/doc/pub/week6/html/week6-solarized.html @@ -63,7 +63,7 @@










    -

    Overview of week 8

    +

    Overview

    Topics

    @@ -266,7 +267,7 @@

    Overview of week 8











    Brief reminder on Newton-Raphson's method

    -

    Let us quickly remind ourselves how we derive the above method.

    +

    Let us quickly remind ourselves on how we derive the above method.

    Perhaps the most celebrated of all one-dimensional root-finding routines is Newton's method, also called the Newton-Raphson @@ -705,7 +706,57 @@

    Final expressions











    -

    Code examples for steepest descent

    +

    Our simple \( 2\times 2 \) example

    + +

    Last week we introduced the simple two-dimensional function

    +$$ +f(x_1,x_2)=x_1^2+x_1x_2+10x_2^2-5x_1-3x_2, +$$ + +

    which is of the form (in terms of vectors and matrices)

    +$$ +f(\boldsymbol{x})=\frac{1}{2}\boldsymbol{x}^T\boldsymbol{A}\boldsymbol{x}-\boldsymbol{b}^T\boldsymbol{x}, +$$ + +

    where we have

    +$$ +\boldsymbol{x}=\begin{bmatrix} x_1 \\ x_2\end{bmatrix}, +$$ + +$$ +\boldsymbol{b}=\begin{bmatrix} 5 \\ 3\end{bmatrix}, +$$ + +

    and

    +$$ +\boldsymbol{A}=\begin{bmatrix} 2 & 1\\ 1& 20\end{bmatrix}. +$$ + + +









    +

    Derivatives and more

    + +

    Optimizing the above equation, that is

    +$$ +\nabla f = 0 = \boldsymbol{A}\boldsymbol{x}-\boldsymbol{b}, +$$ + +

    which leads to a simple matrix-inversion problem

    +$$ +\boldsymbol{x}=\boldsymbol{A}^{-1}\boldsymbol{b}. +$$ + +

    This problem is easy to solve since we can calculate the inverse. Alternatively, we can solve the two coupled equations with two unknowns

    +$$ +\frac{\partial f}{\partial x_1}=2x_1+x_2-5=0, +$$ + +

    and

    +$$ +\frac{\partial f}{\partial x_2}=x_1+20x_2-3=0, +$$ + +

    with solutions \( x_1=97/39 \) and \( x_2=1/39 \).











    Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come

    @@ -734,8 +785,8 @@

    0) = x0(1) = 0; // Set the matrix - A(0,0) = 3; A(1,0) = 2; A(0,1) = 2; A(1,1) = 6; - b(0) = 2; b(1) = -8; + A(0,0) = 2; A(1,0) = 1; A(0,1) = 1; A(1,1) = 20; + b(0) = 5; b(1) = 3; cout << "The Matrix A that we are using: " << endl; A.Print(); cout << endl; @@ -830,15 +881,15 @@

    Steepest descent example

    from mpl_toolkits.mplot3d import axes3d def f(x): - return 0.5*x[0]**2 + 2.5*x[1]**2 + return x[0]**2 + 10.0*x[1]**2+x[0]*x[1]-5.0*x[0]-3*x[2] def df(x): - return np.array([x[0], 5*x[1]]) + return np.array(2*[x[0]+x[1]-5.0, x[0]+20*x[1]]-3.0) fig = pt.figure() ax = fig.gca(projection="3d") -xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j] +xmesh, ymesh = np.mgrid[-2:3:00j,-2:3:00j] fmesh = f(np.array([xmesh, ymesh])) ax.plot_surface(xmesh, ymesh, fmesh) @@ -866,7 +917,7 @@

    Steepest descent example

    pt.axis("equal")
     pt.contour(xmesh, ymesh, fmesh)
    -guesses = [np.array([2, 2./5])]
    +guesses = [np.array([3.0, 0.05])]
     
    diff --git a/doc/pub/week6/html/week6.html b/doc/pub/week6/html/week6.html index 78b14dcf..7f9b21a2 100644 --- a/doc/pub/week6/html/week6.html +++ b/doc/pub/week6/html/week6.html @@ -140,7 +140,7 @@










    -

    Overview of week 8

    +

    Overview

    Topics

    @@ -343,7 +344,7 @@

    Overview of week 8











    Brief reminder on Newton-Raphson's method

    -

    Let us quickly remind ourselves how we derive the above method.

    +

    Let us quickly remind ourselves on how we derive the above method.

    Perhaps the most celebrated of all one-dimensional root-finding routines is Newton's method, also called the Newton-Raphson @@ -782,7 +783,57 @@

    Final expressions











    -

    Code examples for steepest descent

    +

    Our simple \( 2\times 2 \) example

    + +

    Last week we introduced the simple two-dimensional function

    +$$ +f(x_1,x_2)=x_1^2+x_1x_2+10x_2^2-5x_1-3x_2, +$$ + +

    which is of the form (in terms of vectors and matrices)

    +$$ +f(\boldsymbol{x})=\frac{1}{2}\boldsymbol{x}^T\boldsymbol{A}\boldsymbol{x}-\boldsymbol{b}^T\boldsymbol{x}, +$$ + +

    where we have

    +$$ +\boldsymbol{x}=\begin{bmatrix} x_1 \\ x_2\end{bmatrix}, +$$ + +$$ +\boldsymbol{b}=\begin{bmatrix} 5 \\ 3\end{bmatrix}, +$$ + +

    and

    +$$ +\boldsymbol{A}=\begin{bmatrix} 2 & 1\\ 1& 20\end{bmatrix}. +$$ + + +









    +

    Derivatives and more

    + +

    Optimizing the above equation, that is

    +$$ +\nabla f = 0 = \boldsymbol{A}\boldsymbol{x}-\boldsymbol{b}, +$$ + +

    which leads to a simple matrix-inversion problem

    +$$ +\boldsymbol{x}=\boldsymbol{A}^{-1}\boldsymbol{b}. +$$ + +

    This problem is easy to solve since we can calculate the inverse. Alternatively, we can solve the two coupled equations with two unknowns

    +$$ +\frac{\partial f}{\partial x_1}=2x_1+x_2-5=0, +$$ + +

    and

    +$$ +\frac{\partial f}{\partial x_2}=x_1+20x_2-3=0, +$$ + +

    with solutions \( x_1=97/39 \) and \( x_2=1/39 \).











    Simple codes for steepest descent and conjugate gradient using a \( 2\times 2 \) matrix, in c++, Python code to come

    @@ -811,8 +862,8 @@

    // Set our initial guess x0(0) = x0(1) = 0; // Set the matrix - A(0,0) = 3; A(1,0) = 2; A(0,1) = 2; A(1,1) = 6; - b(0) = 2; b(1) = -8; + A(0,0) = 2; A(1,0) = 1; A(0,1) = 1; A(1,1) = 20; + b(0) = 5; b(1) = 3; cout << "The Matrix A that we are using: " << endl; A.Print(); cout << endl; @@ -907,15 +958,15 @@

    Steepest descent example

    from mpl_toolkits.mplot3d import axes3d def f(x): - return 0.5*x[0]**2 + 2.5*x[1]**2 + return x[0]**2 + 10.0*x[1]**2+x[0]*x[1]-5.0*x[0]-3*x[2] def df(x): - return np.array([x[0], 5*x[1]]) + return np.array(2*[x[0]+x[1]-5.0, x[0]+20*x[1]]-3.0) fig = pt.figure() ax = fig.gca(projection="3d") -xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j] +xmesh, ymesh = np.mgrid[-2:3:00j,-2:3:00j] fmesh = f(np.array([xmesh, ymesh])) ax.plot_surface(xmesh, ymesh, fmesh) @@ -943,7 +994,7 @@

    Steepest descent example

    pt.axis("equal")
     pt.contour(xmesh, ymesh, fmesh)
    -guesses = [np.array([2, 2./5])]
    +guesses = [np.array([3.0, 0.05])]
     
    diff --git a/doc/pub/week6/ipynb/ipynb-week6-src.tar.gz b/doc/pub/week6/ipynb/ipynb-week6-src.tar.gz index 6a0da1aa..1e3626b3 100644 Binary files a/doc/pub/week6/ipynb/ipynb-week6-src.tar.gz and b/doc/pub/week6/ipynb/ipynb-week6-src.tar.gz differ diff --git a/doc/pub/week6/ipynb/week6.ipynb b/doc/pub/week6/ipynb/week6.ipynb index b8533316..e5c6d03e 100644 --- a/doc/pub/week6/ipynb/week6.ipynb +++ b/doc/pub/week6/ipynb/week6.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "5de1046f", + "id": "c6312993", "metadata": { "editable": true }, @@ -14,7 +14,7 @@ }, { "cell_type": "markdown", - "id": "526e1001", + "id": "399cf8f3", "metadata": { "editable": true }, @@ -22,19 +22,17 @@ "# Week 8 February 19-23: Gradient Methods\n", "**Morten Hjorth-Jensen Email morten.hjorth-jensen@fys.uio.no**, Department of Physics and Center fo Computing in Science Education, University of Oslo, Oslo, Norway and Department of Physics and Astronomy and Facility for Rare Isotope Beams, Michigan State University, East Lansing, Michigan, USA\n", "\n", - "Date: **Jan 1, 2024**\n", - "\n", - "Copyright 1999-2024, Morten Hjorth-Jensen Email morten.hjorth-jensen@fys.uio.no. Released under CC Attribution-NonCommercial 4.0 license" + "Date: **February 23, 2024**" ] }, { "cell_type": "markdown", - "id": "d3bfde0d", + "id": "8e0fe466", "metadata": { "editable": true }, "source": [ - "## Overview of week 8\n", + "## Overview\n", "**Topics.**\n", "\n", "* Gradient methods:\n", @@ -62,14 +60,14 @@ }, { "cell_type": "markdown", - "id": "c2f741dc", + "id": "d829d9d1", "metadata": { "editable": true }, "source": [ "## Brief reminder on Newton-Raphson's method\n", "\n", - "Let us quickly remind ourselves how we derive the above method.\n", + "Let us quickly remind ourselves on how we derive the above method.\n", "\n", "Perhaps the most celebrated of all one-dimensional root-finding\n", "routines is Newton's method, also called the Newton-Raphson\n", @@ -82,7 +80,7 @@ }, { "cell_type": "markdown", - "id": "bb61f9a3", + "id": "af9ec2f1", "metadata": { "editable": true }, @@ -98,7 +96,7 @@ }, { "cell_type": "markdown", - "id": "aa83df6c", + "id": "a4bb6a8f", "metadata": { "editable": true }, @@ -114,7 +112,7 @@ }, { "cell_type": "markdown", - "id": "0b3130e9", + "id": "6e6db269", "metadata": { "editable": true }, @@ -125,7 +123,7 @@ }, { "cell_type": "markdown", - "id": "cf2ee39a", + "id": "ce7d6f81", "metadata": { "editable": true }, @@ -137,7 +135,7 @@ }, { "cell_type": "markdown", - "id": "3ad0419e", + "id": "9504fa42", "metadata": { "editable": true }, @@ -147,7 +145,7 @@ }, { "cell_type": "markdown", - "id": "8eb7b0fc", + "id": "dde294d4", "metadata": { "editable": true }, @@ -159,7 +157,7 @@ }, { "cell_type": "markdown", - "id": "d6cdbf02", + "id": "5da2441e", "metadata": { "editable": true }, @@ -169,7 +167,7 @@ }, { "cell_type": "markdown", - "id": "6e5049ce", + "id": "60d9cfe3", "metadata": { "editable": true }, @@ -181,7 +179,7 @@ }, { "cell_type": "markdown", - "id": "0f79bc56", + "id": "7deb2440", "metadata": { "editable": true }, @@ -203,7 +201,7 @@ }, { "cell_type": "markdown", - "id": "3f987415", + "id": "d8a09b5c", "metadata": { "editable": true }, @@ -216,7 +214,7 @@ }, { "cell_type": "markdown", - "id": "57470e73", + "id": "035bcb72", "metadata": { "editable": true }, @@ -229,7 +227,7 @@ }, { "cell_type": "markdown", - "id": "3242a8cf", + "id": "06601d88", "metadata": { "editable": true }, @@ -239,7 +237,7 @@ }, { "cell_type": "markdown", - "id": "32ae877e", + "id": "e4e9b7a5", "metadata": { "editable": true }, @@ -257,7 +255,7 @@ }, { "cell_type": "markdown", - "id": "15eeb099", + "id": "63477e16", "metadata": { "editable": true }, @@ -267,7 +265,7 @@ }, { "cell_type": "markdown", - "id": "64db3e1d", + "id": "f4b477f5", "metadata": { "editable": true }, @@ -282,7 +280,7 @@ }, { "cell_type": "markdown", - "id": "72974027", + "id": "acd8f765", "metadata": { "editable": true }, @@ -292,7 +290,7 @@ }, { "cell_type": "markdown", - "id": "44b1c568", + "id": "1752cfa0", "metadata": { "editable": true }, @@ -306,7 +304,7 @@ }, { "cell_type": "markdown", - "id": "446abad9", + "id": "2debe988", "metadata": { "editable": true }, @@ -316,7 +314,7 @@ }, { "cell_type": "markdown", - "id": "4c15096a", + "id": "299eeb27", "metadata": { "editable": true }, @@ -330,7 +328,7 @@ }, { "cell_type": "markdown", - "id": "c8c821f2", + "id": "fe4294f8", "metadata": { "editable": true }, @@ -345,7 +343,7 @@ }, { "cell_type": "markdown", - "id": "ca0b65ce", + "id": "1ce3c134", "metadata": { "editable": true }, @@ -362,7 +360,7 @@ }, { "cell_type": "markdown", - "id": "d3d2845e", + "id": "f406e4a4", "metadata": { "editable": true }, @@ -374,7 +372,7 @@ }, { "cell_type": "markdown", - "id": "810e75a6", + "id": "9fb06ed3", "metadata": { "editable": true }, @@ -388,7 +386,7 @@ }, { "cell_type": "markdown", - "id": "65bc1819", + "id": "dcefbbfb", "metadata": { "editable": true }, @@ -403,7 +401,7 @@ }, { "cell_type": "markdown", - "id": "0440d62c", + "id": "6b2282cf", "metadata": { "editable": true }, @@ -415,7 +413,7 @@ }, { "cell_type": "markdown", - "id": "3c9bcd27", + "id": "7b33a598", "metadata": { "editable": true }, @@ -426,7 +424,7 @@ }, { "cell_type": "markdown", - "id": "b35e135c", + "id": "4dbfaa03", "metadata": { "editable": true }, @@ -454,7 +452,7 @@ }, { "cell_type": "markdown", - "id": "79c113b9", + "id": "b93734d8", "metadata": { "editable": true }, @@ -476,7 +474,7 @@ }, { "cell_type": "markdown", - "id": "f7a0e2c8", + "id": "cc1ab56d", "metadata": { "editable": true }, @@ -498,7 +496,7 @@ }, { "cell_type": "markdown", - "id": "8d0950fa", + "id": "8091cbc6", "metadata": { "editable": true }, @@ -510,7 +508,7 @@ }, { "cell_type": "markdown", - "id": "79883a31", + "id": "42354ed2", "metadata": { "editable": true }, @@ -547,7 +545,7 @@ }, { "cell_type": "markdown", - "id": "ca9fe452", + "id": "d60db400", "metadata": { "editable": true }, @@ -575,7 +573,7 @@ }, { "cell_type": "markdown", - "id": "11214605", + "id": "0e613f15", "metadata": { "editable": true }, @@ -605,7 +603,7 @@ }, { "cell_type": "markdown", - "id": "1cba192b", + "id": "05bffda7", "metadata": { "editable": true }, @@ -625,7 +623,7 @@ }, { "cell_type": "markdown", - "id": "f375e1c0", + "id": "d5e96d65", "metadata": { "editable": true }, @@ -637,7 +635,7 @@ }, { "cell_type": "markdown", - "id": "31823991", + "id": "edb1517e", "metadata": { "editable": true }, @@ -647,7 +645,7 @@ }, { "cell_type": "markdown", - "id": "edeb41da", + "id": "cf46f9ab", "metadata": { "editable": true }, @@ -659,7 +657,7 @@ }, { "cell_type": "markdown", - "id": "c2bc7d37", + "id": "42027921", "metadata": { "editable": true }, @@ -671,7 +669,7 @@ }, { "cell_type": "markdown", - "id": "6fc0979e", + "id": "ad4b4921", "metadata": { "editable": true }, @@ -683,7 +681,7 @@ }, { "cell_type": "markdown", - "id": "c9aa3df0", + "id": "7e26232b", "metadata": { "editable": true }, @@ -695,7 +693,7 @@ }, { "cell_type": "markdown", - "id": "b0bd9ac1", + "id": "249e804e", "metadata": { "editable": true }, @@ -706,7 +704,7 @@ }, { "cell_type": "markdown", - "id": "9658cfca", + "id": "12a1d411", "metadata": { "editable": true }, @@ -719,7 +717,7 @@ }, { "cell_type": "markdown", - "id": "98fbc161", + "id": "425ecf31", "metadata": { "editable": true }, @@ -731,7 +729,7 @@ }, { "cell_type": "markdown", - "id": "ccf9d36e", + "id": "63e4fb8d", "metadata": { "editable": true }, @@ -741,7 +739,7 @@ }, { "cell_type": "markdown", - "id": "d727e73a", + "id": "de874252", "metadata": { "editable": true }, @@ -753,7 +751,7 @@ }, { "cell_type": "markdown", - "id": "45c42ec0", + "id": "7bf4f529", "metadata": { "editable": true }, @@ -763,7 +761,7 @@ }, { "cell_type": "markdown", - "id": "64263cd8", + "id": "14dfd7e8", "metadata": { "editable": true }, @@ -774,7 +772,7 @@ }, { "cell_type": "markdown", - "id": "68120470", + "id": "0d4369c2", "metadata": { "editable": true }, @@ -786,7 +784,7 @@ }, { "cell_type": "markdown", - "id": "7af5bed8", + "id": "693b4789", "metadata": { "editable": true }, @@ -798,7 +796,7 @@ }, { "cell_type": "markdown", - "id": "c41b34ed", + "id": "f4975202", "metadata": { "editable": true }, @@ -810,7 +808,7 @@ }, { "cell_type": "markdown", - "id": "9118575e", + "id": "1c9715f3", "metadata": { "editable": true }, @@ -821,7 +819,7 @@ }, { "cell_type": "markdown", - "id": "4fce9ab6", + "id": "ed8fcaa5", "metadata": { "editable": true }, @@ -832,7 +830,7 @@ }, { "cell_type": "markdown", - "id": "77849e16", + "id": "595a6c44", "metadata": { "editable": true }, @@ -844,7 +842,7 @@ }, { "cell_type": "markdown", - "id": "eb93558b", + "id": "e02ec1e3", "metadata": { "editable": true }, @@ -854,7 +852,7 @@ }, { "cell_type": "markdown", - "id": "9c34d2d2", + "id": "ee586e88", "metadata": { "editable": true }, @@ -866,7 +864,7 @@ }, { "cell_type": "markdown", - "id": "66f5575c", + "id": "b653a9a1", "metadata": { "editable": true }, @@ -876,7 +874,7 @@ }, { "cell_type": "markdown", - "id": "215d1406", + "id": "4fbb348b", "metadata": { "editable": true }, @@ -888,7 +886,7 @@ }, { "cell_type": "markdown", - "id": "79bc01fc", + "id": "bca87614", "metadata": { "editable": true }, @@ -898,7 +896,7 @@ }, { "cell_type": "markdown", - "id": "eb29f4cb", + "id": "4ca73207", "metadata": { "editable": true }, @@ -910,7 +908,7 @@ }, { "cell_type": "markdown", - "id": "7c69130b", + "id": "b97651c8", "metadata": { "editable": true }, @@ -920,7 +918,7 @@ }, { "cell_type": "markdown", - "id": "5840e8eb", + "id": "88a3242e", "metadata": { "editable": true }, @@ -932,17 +930,209 @@ }, { "cell_type": "markdown", - "id": "52e413da", + "id": "2d77985f", + "metadata": { + "editable": true + }, + "source": [ + "## Our simple $2\\times 2$ example\n", + "\n", + "Last week we introduced the simple two-dimensional function" + ] + }, + { + "cell_type": "markdown", + "id": "d6bf1899", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "f(x_1,x_2)=x_1^2+x_1x_2+10x_2^2-5x_1-3x_2,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "294f0746", + "metadata": { + "editable": true + }, + "source": [ + "which is of the form (in terms of vectors and matrices)" + ] + }, + { + "cell_type": "markdown", + "id": "aa812aa8", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "f(\\boldsymbol{x})=\\frac{1}{2}\\boldsymbol{x}^T\\boldsymbol{A}\\boldsymbol{x}-\\boldsymbol{b}^T\\boldsymbol{x},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "c9cd000a", + "metadata": { + "editable": true + }, + "source": [ + "where we have" + ] + }, + { + "cell_type": "markdown", + "id": "62c2d155", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\boldsymbol{x}=\\begin{bmatrix} x_1 \\\\ x_2\\end{bmatrix},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "14b04d34", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\boldsymbol{b}=\\begin{bmatrix} 5 \\\\ 3\\end{bmatrix},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "12e18a6f", + "metadata": { + "editable": true + }, + "source": [ + "and" + ] + }, + { + "cell_type": "markdown", + "id": "5554c203", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\boldsymbol{A}=\\begin{bmatrix} 2 & 1\\\\ 1& 20\\end{bmatrix}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "37c1f654", + "metadata": { + "editable": true + }, + "source": [ + "## Derivatives and more\n", + "\n", + "Optimizing the above equation, that is" + ] + }, + { + "cell_type": "markdown", + "id": "fe4cfa4d", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\nabla f = 0 = \\boldsymbol{A}\\boldsymbol{x}-\\boldsymbol{b},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "f5733b60", + "metadata": { + "editable": true + }, + "source": [ + "which leads to a simple matrix-inversion problem" + ] + }, + { + "cell_type": "markdown", + "id": "f6b5934b", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\boldsymbol{x}=\\boldsymbol{A}^{-1}\\boldsymbol{b}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "52ca4f24", + "metadata": { + "editable": true + }, + "source": [ + "This problem is easy to solve since we can calculate the inverse. Alternatively, we can solve the two coupled equations with two unknowns" + ] + }, + { + "cell_type": "markdown", + "id": "83ff488d", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\frac{\\partial f}{\\partial x_1}=2x_1+x_2-5=0,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "5b33ecc7", + "metadata": { + "editable": true + }, + "source": [ + "and" + ] + }, + { + "cell_type": "markdown", + "id": "589431f4", + "metadata": { + "editable": true + }, + "source": [ + "$$\n", + "\\frac{\\partial f}{\\partial x_2}=x_1+20x_2-3=0,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "6dbc5e2c", "metadata": { "editable": true }, "source": [ - "## Code examples for steepest descent" + "with solutions $x_1=97/39$ and $x_2=1/39$." ] }, { "cell_type": "markdown", - "id": "c0c9ceeb", + "id": "7cf4b6cb", "metadata": { "editable": true }, @@ -953,7 +1143,7 @@ { "cell_type": "code", "execution_count": 1, - "id": "974ae940", + "id": "5093cd2d", "metadata": { "collapsed": false, "editable": true @@ -975,8 +1165,8 @@ " // Set our initial guess\n", " x0(0) = x0(1) = 0;\n", " // Set the matrix\n", - " A(0,0) = 3; A(1,0) = 2; A(0,1) = 2; A(1,1) = 6;\n", - " b(0) = 2; b(1) = -8;\n", + " A(0,0) = 2; A(1,0) = 1; A(0,1) = 1; A(1,1) = 20;\n", + " b(0) = 5; b(1) = 3;\n", " cout << \"The Matrix A that we are using: \" << endl;\n", " A.Print();\n", " cout << endl;\n", @@ -989,7 +1179,7 @@ }, { "cell_type": "markdown", - "id": "b8c61151", + "id": "64917123", "metadata": { "editable": true }, @@ -1000,7 +1190,7 @@ { "cell_type": "code", "execution_count": 2, - "id": "72f263d6", + "id": "6bcf9a1e", "metadata": { "collapsed": false, "editable": true @@ -1032,7 +1222,7 @@ }, { "cell_type": "markdown", - "id": "8ecddf75", + "id": "f9824f2e", "metadata": { "editable": true }, @@ -1043,7 +1233,7 @@ { "cell_type": "code", "execution_count": 3, - "id": "b0a4398c", + "id": "f6c193d4", "metadata": { "collapsed": false, "editable": true @@ -1061,22 +1251,22 @@ "from mpl_toolkits.mplot3d import axes3d\n", "\n", "def f(x):\n", - " return 0.5*x[0]**2 + 2.5*x[1]**2\n", + " return x[0]**2 + 10.0*x[1]**2+x[0]*x[1]-5.0*x[0]-3*x[2]\n", "\n", "def df(x):\n", - " return np.array([x[0], 5*x[1]])\n", + " return np.array(2*[x[0]+x[1]-5.0, x[0]+20*x[1]]-3.0)\n", "\n", "fig = pt.figure()\n", "ax = fig.gca(projection=\"3d\")\n", "\n", - "xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j]\n", + "xmesh, ymesh = np.mgrid[-2:3:00j,-2:3:00j]\n", "fmesh = f(np.array([xmesh, ymesh]))\n", "ax.plot_surface(xmesh, ymesh, fmesh)" ] }, { "cell_type": "markdown", - "id": "cf5e9cc5", + "id": "f7c40b47", "metadata": { "editable": true }, @@ -1087,7 +1277,7 @@ { "cell_type": "code", "execution_count": 4, - "id": "06657f3f", + "id": "faaef1c5", "metadata": { "collapsed": false, "editable": true @@ -1096,12 +1286,12 @@ "source": [ "pt.axis(\"equal\")\n", "pt.contour(xmesh, ymesh, fmesh)\n", - "guesses = [np.array([2, 2./5])]" + "guesses = [np.array([3.0, 0.05])]" ] }, { "cell_type": "markdown", - "id": "176ae352", + "id": "cc812942", "metadata": { "editable": true }, @@ -1112,7 +1302,7 @@ { "cell_type": "code", "execution_count": 5, - "id": "d1c5b2c1", + "id": "d0ec2880", "metadata": { "collapsed": false, "editable": true @@ -1125,7 +1315,7 @@ }, { "cell_type": "markdown", - "id": "062cef07", + "id": "5b051e83", "metadata": { "editable": true }, @@ -1136,7 +1326,7 @@ { "cell_type": "code", "execution_count": 6, - "id": "16a52449", + "id": "3c08b684", "metadata": { "collapsed": false, "editable": true @@ -1154,7 +1344,7 @@ }, { "cell_type": "markdown", - "id": "2813061d", + "id": "cbcaf40b", "metadata": { "editable": true }, @@ -1165,7 +1355,7 @@ { "cell_type": "code", "execution_count": 7, - "id": "e796c4b3", + "id": "7f186b8a", "metadata": { "collapsed": false, "editable": true @@ -1180,7 +1370,7 @@ }, { "cell_type": "markdown", - "id": "10d91825", + "id": "5fff7daf", "metadata": { "editable": true }, @@ -1194,7 +1384,7 @@ }, { "cell_type": "markdown", - "id": "1127f27a", + "id": "a9d17d2d", "metadata": { "editable": true }, @@ -1206,7 +1396,7 @@ }, { "cell_type": "markdown", - "id": "5a9b678b", + "id": "67ce3688", "metadata": { "editable": true }, @@ -1217,7 +1407,7 @@ }, { "cell_type": "markdown", - "id": "09ebd4fd", + "id": "a0fbafc1", "metadata": { "editable": true }, @@ -1229,7 +1419,7 @@ }, { "cell_type": "markdown", - "id": "15cb7a4d", + "id": "c52db4ba", "metadata": { "editable": true }, @@ -1240,7 +1430,7 @@ }, { "cell_type": "markdown", - "id": "5b0f136f", + "id": "01dd8f76", "metadata": { "editable": true }, @@ -1251,7 +1441,7 @@ }, { "cell_type": "markdown", - "id": "a1f71955", + "id": "69b68e6a", "metadata": { "editable": true }, @@ -1263,7 +1453,7 @@ }, { "cell_type": "markdown", - "id": "959319c8", + "id": "28c861ca", "metadata": { "editable": true }, @@ -1273,7 +1463,7 @@ }, { "cell_type": "markdown", - "id": "037eb459", + "id": "a6ec3aed", "metadata": { "editable": true }, @@ -1285,7 +1475,7 @@ }, { "cell_type": "markdown", - "id": "7d405260", + "id": "10fee31a", "metadata": { "editable": true }, @@ -1297,7 +1487,7 @@ }, { "cell_type": "markdown", - "id": "462318cc", + "id": "eeb65674", "metadata": { "editable": true }, @@ -1309,7 +1499,7 @@ }, { "cell_type": "markdown", - "id": "4ca71b0e", + "id": "acfb574c", "metadata": { "editable": true }, @@ -1321,7 +1511,7 @@ }, { "cell_type": "markdown", - "id": "1861412e", + "id": "cad3ff45", "metadata": { "editable": true }, @@ -1332,7 +1522,7 @@ }, { "cell_type": "markdown", - "id": "ffce66de", + "id": "c91cb637", "metadata": { "editable": true }, @@ -1344,7 +1534,7 @@ }, { "cell_type": "markdown", - "id": "6965d8c5", + "id": "56866152", "metadata": { "editable": true }, @@ -1354,7 +1544,7 @@ }, { "cell_type": "markdown", - "id": "c1461945", + "id": "dc885f5c", "metadata": { "editable": true }, @@ -1366,7 +1556,7 @@ }, { "cell_type": "markdown", - "id": "4629ec6d", + "id": "ecbda348", "metadata": { "editable": true }, @@ -1376,7 +1566,7 @@ }, { "cell_type": "markdown", - "id": "698e78db", + "id": "1cb56c1e", "metadata": { "editable": true }, @@ -1388,7 +1578,7 @@ }, { "cell_type": "markdown", - "id": "5d622d0f", + "id": "0e113c26", "metadata": { "editable": true }, @@ -1408,7 +1598,7 @@ }, { "cell_type": "markdown", - "id": "9eb61f7e", + "id": "2a417239", "metadata": { "editable": true }, @@ -1420,7 +1610,7 @@ }, { "cell_type": "markdown", - "id": "207b6250", + "id": "f8eca051", "metadata": { "editable": true }, @@ -1430,7 +1620,7 @@ }, { "cell_type": "markdown", - "id": "770700ff", + "id": "f301cd17", "metadata": { "editable": true }, @@ -1442,7 +1632,7 @@ }, { "cell_type": "markdown", - "id": "0e3c9523", + "id": "3ce80544", "metadata": { "editable": true }, @@ -1452,7 +1642,7 @@ }, { "cell_type": "markdown", - "id": "50c9c3c2", + "id": "06d430ae", "metadata": { "editable": true }, @@ -1463,7 +1653,7 @@ }, { "cell_type": "markdown", - "id": "174822db", + "id": "df4c262c", "metadata": { "editable": true }, @@ -1475,7 +1665,7 @@ }, { "cell_type": "markdown", - "id": "d7642a4d", + "id": "70ca1a9d", "metadata": { "editable": true }, @@ -1487,7 +1677,7 @@ }, { "cell_type": "markdown", - "id": "5f5886ec", + "id": "2a80836b", "metadata": { "editable": true }, @@ -1499,7 +1689,7 @@ }, { "cell_type": "markdown", - "id": "77147f5d", + "id": "4546d9d3", "metadata": { "editable": true }, @@ -1512,7 +1702,7 @@ }, { "cell_type": "markdown", - "id": "4b11863a", + "id": "18e13ae9", "metadata": { "editable": true }, @@ -1523,7 +1713,7 @@ }, { "cell_type": "markdown", - "id": "dd229365", + "id": "877633e0", "metadata": { "editable": true }, @@ -1535,7 +1725,7 @@ }, { "cell_type": "markdown", - "id": "8e5226db", + "id": "b5a1975d", "metadata": { "editable": true }, @@ -1551,7 +1741,7 @@ }, { "cell_type": "markdown", - "id": "31868ff1", + "id": "d4a13896", "metadata": { "editable": true }, @@ -1563,7 +1753,7 @@ }, { "cell_type": "markdown", - "id": "3e9cc375", + "id": "2dbb6628", "metadata": { "editable": true }, @@ -1574,7 +1764,7 @@ }, { "cell_type": "markdown", - "id": "94f8e34b", + "id": "85875f49", "metadata": { "editable": true }, @@ -1586,7 +1776,7 @@ }, { "cell_type": "markdown", - "id": "4f8577dd", + "id": "9efe856c", "metadata": { "editable": true }, @@ -1596,7 +1786,7 @@ }, { "cell_type": "markdown", - "id": "70047561", + "id": "470484e3", "metadata": { "editable": true }, @@ -1608,7 +1798,7 @@ }, { "cell_type": "markdown", - "id": "dd061615", + "id": "049c6212", "metadata": { "editable": true }, @@ -1618,7 +1808,7 @@ }, { "cell_type": "markdown", - "id": "05a93c74", + "id": "09e38b0d", "metadata": { "editable": true }, @@ -1630,7 +1820,7 @@ }, { "cell_type": "markdown", - "id": "08f1de70", + "id": "ffa86d35", "metadata": { "editable": true }, @@ -1640,7 +1830,7 @@ }, { "cell_type": "markdown", - "id": "5f5e4d66", + "id": "72841aae", "metadata": { "editable": true }, @@ -1652,7 +1842,7 @@ }, { "cell_type": "markdown", - "id": "a616ef50", + "id": "88f3cf38", "metadata": { "editable": true }, @@ -1663,7 +1853,7 @@ { "cell_type": "code", "execution_count": 8, - "id": "9a478ddc", + "id": "213b6a91", "metadata": { "collapsed": false, "editable": true @@ -1698,7 +1888,7 @@ }, { "cell_type": "markdown", - "id": "138dfb0b", + "id": "d2847fc8", "metadata": { "editable": true }, @@ -1713,7 +1903,7 @@ }, { "cell_type": "markdown", - "id": "d4d5e866", + "id": "46f17a0b", "metadata": { "editable": true }, @@ -1725,7 +1915,7 @@ }, { "cell_type": "markdown", - "id": "a3d05b4b", + "id": "480f6bf1", "metadata": { "editable": true }, @@ -1740,7 +1930,7 @@ }, { "cell_type": "markdown", - "id": "8ba7f2e7", + "id": "45a18714", "metadata": { "editable": true }, @@ -1752,7 +1942,7 @@ }, { "cell_type": "markdown", - "id": "664e3fd0", + "id": "2e64b382", "metadata": { "editable": true }, @@ -1762,7 +1952,7 @@ }, { "cell_type": "markdown", - "id": "707b52c3", + "id": "47d2904d", "metadata": { "editable": true }, @@ -1779,7 +1969,7 @@ }, { "cell_type": "markdown", - "id": "38b5a05d", + "id": "a6549f0b", "metadata": { "editable": true }, @@ -1792,7 +1982,7 @@ }, { "cell_type": "markdown", - "id": "d0296fe4", + "id": "e0679ec8", "metadata": { "editable": true }, @@ -1805,7 +1995,7 @@ }, { "cell_type": "markdown", - "id": "889bf1df", + "id": "138d5b06", "metadata": { "editable": true }, @@ -1818,7 +2008,7 @@ }, { "cell_type": "markdown", - "id": "df0afd99", + "id": "058943d0", "metadata": { "editable": true }, @@ -1832,7 +2022,7 @@ }, { "cell_type": "markdown", - "id": "c95f5131", + "id": "66cb46b8", "metadata": { "editable": true }, @@ -1854,7 +2044,7 @@ }, { "cell_type": "markdown", - "id": "c6769f15", + "id": "37f5c873", "metadata": { "editable": true }, @@ -1869,7 +2059,7 @@ }, { "cell_type": "markdown", - "id": "a98967e5", + "id": "28417f9f", "metadata": { "editable": true }, @@ -1881,7 +2071,7 @@ }, { "cell_type": "markdown", - "id": "23e75103", + "id": "1371ee83", "metadata": { "editable": true }, @@ -1894,7 +2084,7 @@ }, { "cell_type": "markdown", - "id": "2a58dfa3", + "id": "c57a595a", "metadata": { "editable": true }, @@ -1908,7 +2098,7 @@ }, { "cell_type": "markdown", - "id": "d903c9d4", + "id": "bb4a5d25", "metadata": { "editable": true }, @@ -1919,7 +2109,7 @@ { "cell_type": "code", "execution_count": 9, - "id": "d18da38a", + "id": "af5d1798", "metadata": { "collapsed": false, "editable": true @@ -1944,7 +2134,7 @@ }, { "cell_type": "markdown", - "id": "4b84043b", + "id": "b0545474", "metadata": { "editable": true }, @@ -1960,7 +2150,7 @@ }, { "cell_type": "markdown", - "id": "c18b6fac", + "id": "c8d6d0cb", "metadata": { "editable": true }, @@ -1981,7 +2171,7 @@ }, { "cell_type": "markdown", - "id": "e7666b43", + "id": "b333b841", "metadata": { "editable": true }, @@ -2004,7 +2194,7 @@ { "cell_type": "code", "execution_count": 10, - "id": "abd298fb", + "id": "1c55be00", "metadata": { "collapsed": false, "editable": true @@ -2039,7 +2229,7 @@ }, { "cell_type": "markdown", - "id": "05bf0548", + "id": "009e6326", "metadata": { "editable": true }, @@ -2050,7 +2240,7 @@ { "cell_type": "code", "execution_count": 11, - "id": "386fe71e", + "id": "67b4dc09", "metadata": { "collapsed": false, "editable": true @@ -2127,7 +2317,7 @@ }, { "cell_type": "markdown", - "id": "ea3b78e2", + "id": "5de3c7a3", "metadata": { "editable": true }, @@ -2149,7 +2339,7 @@ }, { "cell_type": "markdown", - "id": "4e067664", + "id": "80f0f66a", "metadata": { "editable": true }, @@ -2167,7 +2357,7 @@ }, { "cell_type": "markdown", - "id": "db981908", + "id": "f38903db", "metadata": { "editable": true }, @@ -2178,7 +2368,7 @@ { "cell_type": "code", "execution_count": 12, - "id": "343da543", + "id": "67377884", "metadata": { "collapsed": false, "editable": true @@ -2210,7 +2400,7 @@ }, { "cell_type": "markdown", - "id": "c3169fa5", + "id": "4e7447d8", "metadata": { "editable": true }, @@ -2225,7 +2415,7 @@ { "cell_type": "code", "execution_count": 13, - "id": "46b9e3b4", + "id": "15dc6a71", "metadata": { "collapsed": false, "editable": true @@ -2248,7 +2438,7 @@ }, { "cell_type": "markdown", - "id": "0d6a9b34", + "id": "73b9f44a", "metadata": { "editable": true }, diff --git a/doc/pub/week6/pdf/week6.pdf b/doc/pub/week6/pdf/week6.pdf index 186f2195..e3c7638d 100644 Binary files a/doc/pub/week6/pdf/week6.pdf and b/doc/pub/week6/pdf/week6.pdf differ diff --git a/doc/src/week6/.ipynb_checkpoints/week6-checkpoint.ipynb b/doc/src/week6/.ipynb_checkpoints/week6-checkpoint.ipynb new file mode 100644 index 00000000..9e3c6c27 --- /dev/null +++ b/doc/src/week6/.ipynb_checkpoints/week6-checkpoint.ipynb @@ -0,0 +1,2166 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "c6312993", + "metadata": {}, + "source": [ + "\n", + "" + ] + }, + { + "cell_type": "markdown", + "id": "399cf8f3", + "metadata": {}, + "source": [ + "# Week 8 February 19-23: Gradient Methods\n", + "**Morten Hjorth-Jensen Email morten.hjorth-jensen@fys.uio.no**, Department of Physics and Center fo Computing in Science Education, University of Oslo, Oslo, Norway and Department of Physics and Astronomy and Facility for Rare Isotope Beams, Michigan State University, East Lansing, Michigan, USA\n", + "\n", + "Date: **February 23, 2024**" + ] + }, + { + "cell_type": "markdown", + "id": "8e0fe466", + "metadata": {}, + "source": [ + "## Overview\n", + "**Topics.**\n", + "\n", + "* Gradient methods:\n", + "\n", + "a. Semi-Newton methods (Broyden's algorithm)\n", + "\n", + "b. Steepest descent and conjugate gradient descent\n", + "\n", + "c. Stochastic gradient descent\n", + "\n", + "**Video and handwriten notes.**\n", + "\n", + "1. [Video of lecture TBA](https://youtu.be/)\n", + "\n", + "2. [Handwritten notes](https://github.com/CompPhysics/ComputationalPhysics2/blob/gh-pages/doc/HandWrittenNotes/2024/NotesFebruary23.pdf)\n", + "\n", + "**Teaching Material, videos and written material.**\n", + "\n", + "* These lecture notes\n", + "\n", + "* [Video on the Conjugate Gradient methods](https://www.youtube.com/watch?v=eAYohMUpPMA&ab_channel=TomCarlone)\n", + "\n", + "* Recommended background literature, [Convex Optimization](https://web.stanford.edu/~boyd/cvxbook/) by Boyd and Vandenberghe. Their [lecture slides](https://web.stanford.edu/~boyd/cvxbook/bv_cvxslides.pdf) are very useful (warning, these are some 300 pages)." + ] + }, + { + "cell_type": "markdown", + "id": "d829d9d1", + "metadata": {}, + "source": [ + "## Brief reminder on Newton-Raphson's method\n", + "\n", + "Let us quickly remind ourselves on how we derive the above method.\n", + "\n", + "Perhaps the most celebrated of all one-dimensional root-finding\n", + "routines is Newton's method, also called the Newton-Raphson\n", + "method. This method requires the evaluation of both the\n", + "function $f$ and its derivative $f'$ at arbitrary points. \n", + "If you can only calculate the derivative\n", + "numerically and/or your function is not of the smooth type, we\n", + "normally discourage the use of this method." + ] + }, + { + "cell_type": "markdown", + "id": "af9ec2f1", + "metadata": {}, + "source": [ + "## The equations\n", + "\n", + "The Newton-Raphson formula consists geometrically of extending the\n", + "tangent line at a current point until it crosses zero, then setting\n", + "the next guess to the abscissa of that zero-crossing. The mathematics\n", + "behind this method is rather simple. Employing a Taylor expansion for\n", + "$x$ sufficiently close to the solution $s$, we have" + ] + }, + { + "cell_type": "markdown", + "id": "a4bb6a8f", + "metadata": {}, + "source": [ + "\n", + "
    \n", + "\n", + "$$\n", + "f(s)=0=f(x)+(s-x)f'(x)+\\frac{(s-x)^2}{2}f''(x) +\\dots.\n", + " \\label{eq:taylornr} \\tag{1}\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "6e6db269", + "metadata": {}, + "source": [ + "For small enough values of the function and for well-behaved\n", + "functions, the terms beyond linear are unimportant, hence we obtain" + ] + }, + { + "cell_type": "markdown", + "id": "ce7d6f81", + "metadata": {}, + "source": [ + "$$\n", + "f(x)+(s-x)f'(x)\\approx 0,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "9504fa42", + "metadata": {}, + "source": [ + "yielding" + ] + }, + { + "cell_type": "markdown", + "id": "dde294d4", + "metadata": {}, + "source": [ + "$$\n", + "s\\approx x-\\frac{f(x)}{f'(x)}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "5da2441e", + "metadata": {}, + "source": [ + "Having in mind an iterative procedure, it is natural to start iterating with" + ] + }, + { + "cell_type": "markdown", + "id": "60d9cfe3", + "metadata": {}, + "source": [ + "$$\n", + "x_{n+1}=x_n-\\frac{f(x_n)}{f'(x_n)}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "7deb2440", + "metadata": {}, + "source": [ + "## Simple geometric interpretation\n", + "\n", + "The above is Newton-Raphson's method. It has a simple geometric\n", + "interpretation, namely $x_{n+1}$ is the point where the tangent from\n", + "$(x_n,f(x_n))$ crosses the $x$-axis. Close to the solution,\n", + "Newton-Raphson converges fast to the desired result. However, if we\n", + "are far from a root, where the higher-order terms in the series are\n", + "important, the Newton-Raphson formula can give grossly inaccurate\n", + "results. For instance, the initial guess for the root might be so far\n", + "from the true root as to let the search interval include a local\n", + "maximum or minimum of the function. If an iteration places a trial\n", + "guess near such a local extremum, so that the first derivative nearly\n", + "vanishes, then Newton-Raphson may fail totally" + ] + }, + { + "cell_type": "markdown", + "id": "d8a09b5c", + "metadata": {}, + "source": [ + "## Extending to more than one variable\n", + "\n", + "Newton's method can be generalized to systems of several non-linear equations\n", + "and variables. Consider the case with two equations" + ] + }, + { + "cell_type": "markdown", + "id": "035bcb72", + "metadata": {}, + "source": [ + "$$\n", + "\\begin{array}{cc} f_1(x_1,x_2) &=0\\\\\n", + " f_2(x_1,x_2) &=0,\\end{array}\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "06601d88", + "metadata": {}, + "source": [ + "which we Taylor expand to obtain" + ] + }, + { + "cell_type": "markdown", + "id": "e4e9b7a5", + "metadata": {}, + "source": [ + "$$\n", + "\\begin{array}{cc} 0=f_1(x_1+h_1,x_2+h_2)=&f_1(x_1,x_2)+h_1\n", + " \\partial f_1/\\partial x_1+h_2\n", + " \\partial f_1/\\partial x_2+\\dots\\\\\n", + " 0=f_2(x_1+h_1,x_2+h_2)=&f_2(x_1,x_2)+h_1\n", + " \\partial f_2/\\partial x_1+h_2\n", + " \\partial f_2/\\partial x_2+\\dots\n", + " \\end{array}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "63477e16", + "metadata": {}, + "source": [ + "Defining the Jacobian matrix $\\hat{J}$ we have" + ] + }, + { + "cell_type": "markdown", + "id": "f4b477f5", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{J}=\\left( \\begin{array}{cc}\n", + " \\partial f_1/\\partial x_1 & \\partial f_1/\\partial x_2 \\\\\n", + " \\partial f_2/\\partial x_1 &\\partial f_2/\\partial x_2\n", + " \\end{array} \\right),\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "acd8f765", + "metadata": {}, + "source": [ + "we can rephrase Newton's method as" + ] + }, + { + "cell_type": "markdown", + "id": "1752cfa0", + "metadata": {}, + "source": [ + "$$\n", + "\\left(\\begin{array}{c} x_1^{n+1} \\\\ x_2^{n+1} \\end{array} \\right)=\n", + "\\left(\\begin{array}{c} x_1^{n} \\\\ x_2^{n} \\end{array} \\right)+\n", + "\\left(\\begin{array}{c} h_1^{n} \\\\ h_2^{n} \\end{array} \\right),\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "2debe988", + "metadata": {}, + "source": [ + "where we have defined" + ] + }, + { + "cell_type": "markdown", + "id": "299eeb27", + "metadata": {}, + "source": [ + "$$\n", + "\\left(\\begin{array}{c} h_1^{n} \\\\ h_2^{n} \\end{array} \\right)=\n", + " -{\\bf \\hat{J}}^{-1}\n", + " \\left(\\begin{array}{c} f_1(x_1^{n},x_2^{n}) \\\\ f_2(x_1^{n},x_2^{n}) \\end{array} \\right).\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "fe4294f8", + "metadata": {}, + "source": [ + "We need thus to compute the inverse of the Jacobian matrix and it\n", + "is to understand that difficulties may\n", + "arise in case $\\hat{J}$ is nearly singular.\n", + "\n", + "It is rather straightforward to extend the above scheme to systems of\n", + "more than two non-linear equations. In our case, the Jacobian matrix is given by the Hessian that represents the second derivative of cost function." + ] + }, + { + "cell_type": "markdown", + "id": "1ce3c134", + "metadata": {}, + "source": [ + "## Steepest descent\n", + "\n", + "The basic idea of gradient descent is\n", + "that a function $F(\\mathbf{x})$, \n", + "$\\mathbf{x} \\equiv (x_1,\\cdots,x_n)$, decreases fastest if one goes from $\\bf {x}$ in the\n", + "direction of the negative gradient $-\\nabla F(\\mathbf{x})$.\n", + "\n", + "It can be shown that if" + ] + }, + { + "cell_type": "markdown", + "id": "f406e4a4", + "metadata": {}, + "source": [ + "$$\n", + "\\mathbf{x}_{k+1} = \\mathbf{x}_k - \\gamma_k \\nabla F(\\mathbf{x}_k),\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "9fb06ed3", + "metadata": {}, + "source": [ + "with $\\gamma_k > 0$.\n", + "\n", + "For $\\gamma_k$ small enough, then $F(\\mathbf{x}_{k+1}) \\leq\n", + "F(\\mathbf{x}_k)$. This means that for a sufficiently small $\\gamma_k$\n", + "we are always moving towards smaller function values, i.e a minimum." + ] + }, + { + "cell_type": "markdown", + "id": "dcefbbfb", + "metadata": {}, + "source": [ + "## More on Steepest descent\n", + "\n", + "The previous observation is the basis of the method of steepest\n", + "descent, which is also referred to as just gradient descent (GD). One\n", + "starts with an initial guess $\\mathbf{x}_0$ for a minimum of $F$ and\n", + "computes new approximations according to" + ] + }, + { + "cell_type": "markdown", + "id": "6b2282cf", + "metadata": {}, + "source": [ + "$$\n", + "\\mathbf{x}_{k+1} = \\mathbf{x}_k - \\gamma_k \\nabla F(\\mathbf{x}_k), \\ \\ k \\geq 0.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "7b33a598", + "metadata": {}, + "source": [ + "The parameter $\\gamma_k$ is often referred to as the step length or\n", + "the learning rate within the context of Machine Learning." + ] + }, + { + "cell_type": "markdown", + "id": "4dbfaa03", + "metadata": {}, + "source": [ + "## The ideal\n", + "\n", + "Ideally the sequence $\\{\\mathbf{x}_k \\}_{k=0}$ converges to a global\n", + "minimum of the function $F$. In general we do not know if we are in a\n", + "global or local minimum. In the special case when $F$ is a convex\n", + "function, all local minima are also global minima, so in this case\n", + "gradient descent can converge to the global solution. The advantage of\n", + "this scheme is that it is conceptually simple and straightforward to\n", + "implement. However the method in this form has some severe\n", + "limitations:\n", + "\n", + "In machine learing we are often faced with non-convex high dimensional\n", + "cost functions with many local minima. Since GD is deterministic we\n", + "will get stuck in a local minimum, if the method converges, unless we\n", + "have a very good intial guess. This also implies that the scheme is\n", + "sensitive to the chosen initial condition.\n", + "\n", + "Note that the gradient is a function of $\\mathbf{x} =\n", + "(x_1,\\cdots,x_n)$ which makes it expensive to compute numerically." + ] + }, + { + "cell_type": "markdown", + "id": "b93734d8", + "metadata": {}, + "source": [ + "## The sensitiveness of the gradient descent\n", + "\n", + "The gradient descent method \n", + "is sensitive to the choice of learning rate $\\gamma_k$. This is due\n", + "to the fact that we are only guaranteed that $F(\\mathbf{x}_{k+1}) \\leq\n", + "F(\\mathbf{x}_k)$ for sufficiently small $\\gamma_k$. The problem is to\n", + "determine an optimal learning rate. If the learning rate is chosen too\n", + "small the method will take a long time to converge and if it is too\n", + "large we can experience erratic behavior.\n", + "\n", + "Many of these shortcomings can be alleviated by introducing\n", + "randomness. One such method is that of Stochastic Gradient Descent\n", + "(SGD), see below." + ] + }, + { + "cell_type": "markdown", + "id": "cc1ab56d", + "metadata": {}, + "source": [ + "## Convex functions\n", + "\n", + "Ideally we want our cost/loss function to be convex(concave).\n", + "\n", + "First we give the definition of a convex set: A set $C$ in\n", + "$\\mathbb{R}^n$ is said to be convex if, for all $x$ and $y$ in $C$ and\n", + "all $t \\in (0,1)$ , the point $(1 − t)x + ty$ also belongs to\n", + "C. Geometrically this means that every point on the line segment\n", + "connecting $x$ and $y$ is in $C$ as discussed below.\n", + "\n", + "The convex subsets of $\\mathbb{R}$ are the intervals of\n", + "$\\mathbb{R}$. Examples of convex sets of $\\mathbb{R}^2$ are the\n", + "regular polygons (triangles, rectangles, pentagons, etc...)." + ] + }, + { + "cell_type": "markdown", + "id": "8091cbc6", + "metadata": {}, + "source": [ + "## Convex function\n", + "\n", + "**Convex function**: Let $X \\subset \\mathbb{R}^n$ be a convex set. Assume that the function $f: X \\rightarrow \\mathbb{R}$ is continuous, then $f$ is said to be convex if $$f(tx_1 + (1-t)x_2) \\leq tf(x_1) + (1-t)f(x_2) $$ for all $x_1, x_2 \\in X$ and for all $t \\in [0,1]$. If $\\leq$ is replaced with a strict inequaltiy in the definition, we demand $x_1 \\neq x_2$ and $t\\in(0,1)$ then $f$ is said to be strictly convex. For a single variable function, convexity means that if you draw a straight line connecting $f(x_1)$ and $f(x_2)$, the value of the function on the interval $[x_1,x_2]$ is always below the line as illustrated below." + ] + }, + { + "cell_type": "markdown", + "id": "42354ed2", + "metadata": {}, + "source": [ + "## Conditions on convex functions\n", + "\n", + "In the following we state first and second-order conditions which\n", + "ensures convexity of a function $f$. We write $D_f$ to denote the\n", + "domain of $f$, i.e the subset of $R^n$ where $f$ is defined. For more\n", + "details and proofs we refer to: [S. Boyd and L. Vandenberghe. Convex Optimization. Cambridge University Press](http://stanford.edu/boyd/cvxbook/, 2004).\n", + "\n", + "**First order condition.**\n", + "\n", + "Suppose $f$ is differentiable (i.e $\\nabla f(x)$ is well defined for\n", + "all $x$ in the domain of $f$). Then $f$ is convex if and only if $D_f$\n", + "is a convex set and $$f(y) \\geq f(x) + \\nabla f(x)^T (y-x) $$ holds\n", + "for all $x,y \\in D_f$. This condition means that for a convex function\n", + "the first order Taylor expansion (right hand side above) at any point\n", + "a global under estimator of the function. To convince yourself you can\n", + "make a drawing of $f(x) = x^2+1$ and draw the tangent line to $f(x)$ and\n", + "note that it is always below the graph.\n", + "\n", + "**Second order condition.**\n", + "\n", + "Assume that $f$ is twice\n", + "differentiable, i.e the Hessian matrix exists at each point in\n", + "$D_f$. Then $f$ is convex if and only if $D_f$ is a convex set and its\n", + "Hessian is positive semi-definite for all $x\\in D_f$. For a\n", + "single-variable function this reduces to $f''(x) \\geq 0$. Geometrically this means that $f$ has nonnegative curvature\n", + "everywhere.\n", + "\n", + "This condition is particularly useful since it gives us an procedure for determining if the function under consideration is convex, apart from using the definition." + ] + }, + { + "cell_type": "markdown", + "id": "d60db400", + "metadata": {}, + "source": [ + "## More on convex functions\n", + "\n", + "The next result is of great importance to us and the reason why we are\n", + "going on about convex functions. In machine learning we frequently\n", + "have to minimize a loss/cost function in order to find the best\n", + "parameters for the model we are considering. \n", + "\n", + "Ideally we want the\n", + "global minimum (for high-dimensional models it is hard to know\n", + "if we have local or global minimum). However, if the cost/loss function\n", + "is convex the following result provides invaluable information:\n", + "\n", + "**Any minimum is global for convex functions.**\n", + "\n", + "Consider the problem of finding $x \\in \\mathbb{R}^n$ such that $f(x)$\n", + "is minimal, where $f$ is convex and differentiable. Then, any point\n", + "$x^*$ that satisfies $\\nabla f(x^*) = 0$ is a global minimum.\n", + "\n", + "This result means that if we know that the cost/loss function is convex and we are able to find a minimum, we are guaranteed that it is a global minimum." + ] + }, + { + "cell_type": "markdown", + "id": "0e613f15", + "metadata": {}, + "source": [ + "## Some simple problems\n", + "\n", + "1. Show that $f(x)=x^2$ is convex for $x \\in \\mathbb{R}$ using the definition of convexity. Hint: If you re-write the definition, $f$ is convex if the following holds for all $x,y \\in D_f$ and any $\\lambda \\in [0,1]$ $\\lambda f(x)+(1-\\lambda)f(y)-f(\\lambda x + (1-\\lambda) y ) \\geq 0$.\n", + "\n", + "2. Using the second order condition show that the following functions are convex on the specified domain.\n", + "\n", + " * $f(x) = e^x$ is convex for $x \\in \\mathbb{R}$.\n", + "\n", + " * $g(x) = -\\ln(x)$ is convex for $x \\in (0,\\infty)$.\n", + "\n", + "3. Let $f(x) = x^2$ and $g(x) = e^x$. Show that $f(g(x))$ and $g(f(x))$ is convex for $x \\in \\mathbb{R}$. Also show that if $f(x)$ is any convex function than $h(x) = e^{f(x)}$ is convex.\n", + "\n", + "4. A norm is any function that satisfy the following properties\n", + "\n", + " * $f(\\alpha x) = |\\alpha| f(x)$ for all $\\alpha \\in \\mathbb{R}$.\n", + "\n", + " * $f(x+y) \\leq f(x) + f(y)$\n", + "\n", + " * $f(x) \\leq 0$ for all $x \\in \\mathbb{R}^n$ with equality if and only if $x = 0$\n", + "\n", + "Using the definition of convexity, try to show that a function satisfying the properties above is convex (the third condition is not needed to show this)." + ] + }, + { + "cell_type": "markdown", + "id": "05bffda7", + "metadata": {}, + "source": [ + "## Standard steepest descent\n", + "\n", + "Before we proceed, we would like to discuss the approach called the\n", + "**standard Steepest descent**, which again leads to us having to be able\n", + "to compute a matrix. It belongs to the class of Conjugate Gradient methods (CG).\n", + "\n", + "[The success of the CG method](https://www.cs.cmu.edu/~quake-papers/painless-conjugate-gradient.pdf)\n", + "for finding solutions of non-linear problems is based on the theory\n", + "of conjugate gradients for linear systems of equations. It belongs to\n", + "the class of iterative methods for solving problems from linear\n", + "algebra of the type" + ] + }, + { + "cell_type": "markdown", + "id": "d5e96d65", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{A}\\hat{x} = \\hat{b}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "edb1517e", + "metadata": {}, + "source": [ + "In the iterative process we end up with a problem like" + ] + }, + { + "cell_type": "markdown", + "id": "cf46f9ab", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{r}= \\hat{b}-\\hat{A}\\hat{x},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "42027921", + "metadata": {}, + "source": [ + "where $\\hat{r}$ is the so-called residual or error in the iterative process.\n", + "\n", + "When we have found the exact solution, $\\hat{r}=0$." + ] + }, + { + "cell_type": "markdown", + "id": "ad4b4921", + "metadata": {}, + "source": [ + "## Gradient method\n", + "\n", + "The residual is zero when we reach the minimum of the quadratic equation" + ] + }, + { + "cell_type": "markdown", + "id": "7e26232b", + "metadata": {}, + "source": [ + "$$\n", + "P(\\hat{x})=\\frac{1}{2}\\hat{x}^T\\hat{A}\\hat{x} - \\hat{x}^T\\hat{b},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "249e804e", + "metadata": {}, + "source": [ + "with the constraint that the matrix $\\hat{A}$ is positive definite and\n", + "symmetric. This defines also the Hessian and we want it to be positive definite." + ] + }, + { + "cell_type": "markdown", + "id": "12a1d411", + "metadata": {}, + "source": [ + "## Steepest descent method\n", + "\n", + "We denote the initial guess for $\\hat{x}$ as $\\hat{x}_0$. \n", + "We can assume without loss of generality that" + ] + }, + { + "cell_type": "markdown", + "id": "425ecf31", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{x}_0=0,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "63e4fb8d", + "metadata": {}, + "source": [ + "or consider the system" + ] + }, + { + "cell_type": "markdown", + "id": "de874252", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{A}\\hat{z} = \\hat{b}-\\hat{A}\\hat{x}_0,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "7bf4f529", + "metadata": {}, + "source": [ + "instead." + ] + }, + { + "cell_type": "markdown", + "id": "14dfd7e8", + "metadata": {}, + "source": [ + "## Steepest descent method\n", + "One can show that the solution $\\hat{x}$ is also the unique minimizer of the quadratic form" + ] + }, + { + "cell_type": "markdown", + "id": "0d4369c2", + "metadata": {}, + "source": [ + "$$\n", + "f(\\hat{x}) = \\frac{1}{2}\\hat{x}^T\\hat{A}\\hat{x} - \\hat{x}^T \\hat{x} , \\quad \\hat{x}\\in\\mathbf{R}^n.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "693b4789", + "metadata": {}, + "source": [ + "This suggests taking the first basis vector $\\hat{r}_1$ (see below for definition) \n", + "to be the gradient of $f$ at $\\hat{x}=\\hat{x}_0$, \n", + "which equals" + ] + }, + { + "cell_type": "markdown", + "id": "f4975202", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{A}\\hat{x}_0-\\hat{b},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "1c9715f3", + "metadata": {}, + "source": [ + "and \n", + "$\\hat{x}_0=0$ it is equal $-\\hat{b}$." + ] + }, + { + "cell_type": "markdown", + "id": "ed8fcaa5", + "metadata": {}, + "source": [ + "## Final expressions\n", + "We can compute the residual iteratively as" + ] + }, + { + "cell_type": "markdown", + "id": "595a6c44", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{r}_{k+1}=\\hat{b}-\\hat{A}\\hat{x}_{k+1},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "e02ec1e3", + "metadata": {}, + "source": [ + "which equals" + ] + }, + { + "cell_type": "markdown", + "id": "ee586e88", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{b}-\\hat{A}(\\hat{x}_k+\\alpha_k\\hat{r}_k),\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "b653a9a1", + "metadata": {}, + "source": [ + "or" + ] + }, + { + "cell_type": "markdown", + "id": "4fbb348b", + "metadata": {}, + "source": [ + "$$\n", + "(\\hat{b}-\\hat{A}\\hat{x}_k)-\\alpha_k\\hat{A}\\hat{r}_k,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "bca87614", + "metadata": {}, + "source": [ + "which gives" + ] + }, + { + "cell_type": "markdown", + "id": "4ca73207", + "metadata": {}, + "source": [ + "$$\n", + "\\alpha_k = \\frac{\\hat{r}_k^T\\hat{r}_k}{\\hat{r}_k^T\\hat{A}\\hat{r}_k}\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "b97651c8", + "metadata": {}, + "source": [ + "leading to the iterative scheme" + ] + }, + { + "cell_type": "markdown", + "id": "88a3242e", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{x}_{k+1}=\\hat{x}_k-\\alpha_k\\hat{r}_{k},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "2d77985f", + "metadata": {}, + "source": [ + "## Our simple $2\\times 2$ example\n", + "\n", + "Last week we introduced the simple two-dimensional function" + ] + }, + { + "cell_type": "markdown", + "id": "d6bf1899", + "metadata": {}, + "source": [ + "$$\n", + "f(x_1,x_2)=x_1^2+x_1x_2+10x_2^2-5x_1-3x_2,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "294f0746", + "metadata": {}, + "source": [ + "which is of the form (in terms of vectors and matrices)" + ] + }, + { + "cell_type": "markdown", + "id": "aa812aa8", + "metadata": {}, + "source": [ + "$$\n", + "f(\\boldsymbol{x})=\\frac{1}{2}\\boldsymbol{x}^T\\boldsymbol{A}\\boldsymbol{x}-\\boldsymbol{b}^T\\boldsymbol{x},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "c9cd000a", + "metadata": {}, + "source": [ + "where we have" + ] + }, + { + "cell_type": "markdown", + "id": "62c2d155", + "metadata": {}, + "source": [ + "$$\n", + "\\boldsymbol{x}=\\begin{bmatrix} x_1 \\\\ x_2\\end{bmatrix},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "14b04d34", + "metadata": {}, + "source": [ + "$$\n", + "\\boldsymbol{b}=\\begin{bmatrix} 5 \\\\ 3\\end{bmatrix},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "12e18a6f", + "metadata": {}, + "source": [ + "and" + ] + }, + { + "cell_type": "markdown", + "id": "5554c203", + "metadata": {}, + "source": [ + "$$\n", + "\\boldsymbol{A}=\\begin{bmatrix} 2 & 1\\\\ 1& 20\\end{bmatrix}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "37c1f654", + "metadata": {}, + "source": [ + "## Derivatives and more\n", + "\n", + "Optimizing the above equation, that is" + ] + }, + { + "cell_type": "markdown", + "id": "fe4cfa4d", + "metadata": {}, + "source": [ + "$$\n", + "\\nabla f = 0 = \\boldsymbol{A}\\boldsymbol{x}-\\boldsymbol{b},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "f5733b60", + "metadata": {}, + "source": [ + "which leads to a simple matrix-inversion problem" + ] + }, + { + "cell_type": "markdown", + "id": "f6b5934b", + "metadata": {}, + "source": [ + "$$\n", + "\\boldsymbol{x}=\\boldsymbol{A}^{-1}\\boldsymbol{b}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "52ca4f24", + "metadata": {}, + "source": [ + "This problem is easy to solve since we can calculate the inverse. Alternatively, we can solve the two coupled equations with two unknowns" + ] + }, + { + "cell_type": "markdown", + "id": "83ff488d", + "metadata": {}, + "source": [ + "$$\n", + "\\frac{\\partial f}{\\partial x_1}=2x_1+x_2-5=0,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "5b33ecc7", + "metadata": {}, + "source": [ + "and" + ] + }, + { + "cell_type": "markdown", + "id": "589431f4", + "metadata": {}, + "source": [ + "$$\n", + "\\frac{\\partial f}{\\partial x_2}=x_1+20x_2-3=0,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "6dbc5e2c", + "metadata": {}, + "source": [ + "with solutions $x_1=97/39$ and $x_2=1/39$." + ] + }, + { + "cell_type": "markdown", + "id": "7cf4b6cb", + "metadata": {}, + "source": [ + "## Simple codes for steepest descent and conjugate gradient using a $2\\times 2$ matrix, in c++, Python code to come" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "5093cd2d", + "metadata": {}, + "outputs": [], + "source": [ + "#include \n", + "#include \n", + "#include \n", + "#include \n", + "#include \"vectormatrixclass.h\"\n", + "using namespace std;\n", + "// Main function begins here\n", + "int main(int argc, char * argv[]){\n", + " int dim = 2;\n", + " Vector x(dim),xsd(dim), b(dim),x0(dim);\n", + " Matrix A(dim,dim);\n", + "\n", + " // Set our initial guess\n", + " x0(0) = x0(1) = 0;\n", + " // Set the matrix\n", + " A(0,0) = 2; A(1,0) = 1; A(0,1) = 1; A(1,1) = 20;\n", + " b(0) = 5; b(1) = 3;\n", + " cout << \"The Matrix A that we are using: \" << endl;\n", + " A.Print();\n", + " cout << endl;\n", + " xsd = SteepestDescent(A,b,x0);\n", + " cout << \"The approximate solution using Steepest Descent is: \" << endl;\n", + " xsd.Print();\n", + " cout << endl;\n", + "}" + ] + }, + { + "cell_type": "markdown", + "id": "64917123", + "metadata": {}, + "source": [ + "## The routine for the steepest descent method" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6bcf9a1e", + "metadata": {}, + "outputs": [], + "source": [ + "Vector SteepestDescent(Matrix A, Vector b, Vector x0){\n", + " int IterMax, i;\n", + " int dim = x0.Dimension();\n", + " const double tolerance = 1.0e-14;\n", + " Vector x(dim),f(dim),z(dim);\n", + " double c,alpha,d;\n", + " IterMax = 30;\n", + " x = x0;\n", + " r = A*x-b;\n", + " i = 0;\n", + " while (i <= IterMax){\n", + " z = A*r;\n", + " c = dot(r,r);\n", + " alpha = c/dot(r,z);\n", + " x = x - alpha*r;\n", + " r = A*x-b;\n", + " if(sqrt(dot(r,r)) < tolerance) break;\n", + " i++;\n", + " }\n", + " return x;\n", + "}" + ] + }, + { + "cell_type": "markdown", + "id": "f9824f2e", + "metadata": {}, + "source": [ + "## Steepest descent example" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "f6c193d4", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "gca() got an unexpected keyword argument 'projection'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[3], line 18\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39marray(\u001b[38;5;241m2\u001b[39m\u001b[38;5;241m*\u001b[39m[x[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m+\u001b[39mx[\u001b[38;5;241m1\u001b[39m]\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m5.0\u001b[39m, x[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m+\u001b[39m\u001b[38;5;241m20\u001b[39m\u001b[38;5;241m*\u001b[39mx[\u001b[38;5;241m1\u001b[39m]]\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m3.0\u001b[39m)\n\u001b[1;32m 17\u001b[0m fig \u001b[38;5;241m=\u001b[39m pt\u001b[38;5;241m.\u001b[39mfigure()\n\u001b[0;32m---> 18\u001b[0m ax \u001b[38;5;241m=\u001b[39m \u001b[43mfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgca\u001b[49m\u001b[43m(\u001b[49m\u001b[43mprojection\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m3d\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 20\u001b[0m xmesh, ymesh \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mmgrid[\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m2\u001b[39m:\u001b[38;5;241m3\u001b[39m:\u001b[38;5;241m0\u001b[39mj,\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m2\u001b[39m:\u001b[38;5;241m3\u001b[39m:\u001b[38;5;241m0\u001b[39mj]\n\u001b[1;32m 21\u001b[0m fmesh \u001b[38;5;241m=\u001b[39m f(np\u001b[38;5;241m.\u001b[39marray([xmesh, ymesh]))\n", + "\u001b[0;31mTypeError\u001b[0m: gca() got an unexpected keyword argument 'projection'" + ] + }, + { + "data": { + "text/plain": [ + "
    " + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%matplotlib inline\n", + "\n", + "import numpy as np\n", + "import numpy.linalg as la\n", + "\n", + "import scipy.optimize as sopt\n", + "\n", + "import matplotlib.pyplot as pt\n", + "from mpl_toolkits.mplot3d import axes3d\n", + "\n", + "def f(x):\n", + " return x[0]**2 + 10.0*x[1]**2+x[0]*x[1]-5.0*x[0]-3*x[2]\n", + "\n", + "def df(x):\n", + " return np.array(2*[x[0]+x[1]-5.0, x[0]+20*x[1]]-3.0)\n", + "\n", + "fig = pt.figure()\n", + "ax = fig.gca(projection=\"3d\")\n", + "\n", + "xmesh, ymesh = np.mgrid[-2:3:0j,-2:3:0j]\n", + "fmesh = f(np.array([xmesh, ymesh]))\n", + "ax.plot_surface(xmesh, ymesh, fmesh)" + ] + }, + { + "cell_type": "markdown", + "id": "f7c40b47", + "metadata": {}, + "source": [ + "And then as countor plot" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "faaef1c5", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'xmesh' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[2], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m pt\u001b[38;5;241m.\u001b[39maxis(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mequal\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m pt\u001b[38;5;241m.\u001b[39mcontour(\u001b[43mxmesh\u001b[49m, ymesh, fmesh)\n\u001b[1;32m 3\u001b[0m guesses \u001b[38;5;241m=\u001b[39m [np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m3.0\u001b[39m, \u001b[38;5;241m0.05\u001b[39m])]\n", + "\u001b[0;31mNameError\u001b[0m: name 'xmesh' is not defined" + ] + }, + { + "data": { + "image/png": "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", + "text/plain": [ + "
    " + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "pt.axis(\"equal\")\n", + "pt.contour(xmesh, ymesh, fmesh)\n", + "guesses = [np.array([3.0, 0.05])]" + ] + }, + { + "cell_type": "markdown", + "id": "cc812942", + "metadata": {}, + "source": [ + "Find guesses" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d0ec2880", + "metadata": {}, + "outputs": [], + "source": [ + "x = guesses[-1]\n", + "s = -df(x)" + ] + }, + { + "cell_type": "markdown", + "id": "5b051e83", + "metadata": {}, + "source": [ + "Run it!" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "3c08b684", + "metadata": {}, + "outputs": [], + "source": [ + "def f1d(alpha):\n", + " return f(x + alpha*s)\n", + "\n", + "alpha_opt = sopt.golden(f1d)\n", + "next_guess = x + alpha_opt * s\n", + "guesses.append(next_guess)\n", + "print(next_guess)" + ] + }, + { + "cell_type": "markdown", + "id": "cbcaf40b", + "metadata": {}, + "source": [ + "What happened?" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "7f186b8a", + "metadata": {}, + "outputs": [], + "source": [ + "pt.axis(\"equal\")\n", + "pt.contour(xmesh, ymesh, fmesh, 50)\n", + "it_array = np.array(guesses)\n", + "pt.plot(it_array.T[0], it_array.T[1], \"x-\")" + ] + }, + { + "cell_type": "markdown", + "id": "5fff7daf", + "metadata": {}, + "source": [ + "## Conjugate gradient method\n", + "In the CG method we define so-called conjugate directions and two vectors \n", + "$\\hat{s}$ and $\\hat{t}$\n", + "are said to be\n", + "conjugate if" + ] + }, + { + "cell_type": "markdown", + "id": "a9d17d2d", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{s}^T\\hat{A}\\hat{t}= 0.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "67ce3688", + "metadata": {}, + "source": [ + "The philosophy of the CG method is to perform searches in various conjugate directions\n", + "of our vectors $\\hat{x}_i$ obeying the above criterion, namely" + ] + }, + { + "cell_type": "markdown", + "id": "a0fbafc1", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{x}_i^T\\hat{A}\\hat{x}_j= 0.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "c52db4ba", + "metadata": {}, + "source": [ + "Two vectors are conjugate if they are orthogonal with respect to \n", + "this inner product. Being conjugate is a symmetric relation: if $\\hat{s}$ is conjugate to $\\hat{t}$, then $\\hat{t}$ is conjugate to $\\hat{s}$." + ] + }, + { + "cell_type": "markdown", + "id": "01dd8f76", + "metadata": {}, + "source": [ + "## Conjugate gradient method\n", + "An example is given by the eigenvectors of the matrix" + ] + }, + { + "cell_type": "markdown", + "id": "69b68e6a", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{v}_i^T\\hat{A}\\hat{v}_j= \\lambda\\hat{v}_i^T\\hat{v}_j,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "28c861ca", + "metadata": {}, + "source": [ + "which is zero unless $i=j$." + ] + }, + { + "cell_type": "markdown", + "id": "a6ec3aed", + "metadata": {}, + "source": [ + "## Conjugate gradient method\n", + "Assume now that we have a symmetric positive-definite matrix $\\hat{A}$ of size\n", + "$n\\times n$. At each iteration $i+1$ we obtain the conjugate direction of a vector" + ] + }, + { + "cell_type": "markdown", + "id": "10fee31a", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{x}_{i+1}=\\hat{x}_{i}+\\alpha_i\\hat{p}_{i}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "eeb65674", + "metadata": {}, + "source": [ + "We assume that $\\hat{p}_{i}$ is a sequence of $n$ mutually conjugate directions. \n", + "Then the $\\hat{p}_{i}$ form a basis of $R^n$ and we can expand the solution \n", + "$ \\hat{A}\\hat{x} = \\hat{b}$ in this basis, namely" + ] + }, + { + "cell_type": "markdown", + "id": "acfb574c", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{x} = \\sum^{n}_{i=1} \\alpha_i \\hat{p}_i.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "cad3ff45", + "metadata": {}, + "source": [ + "## Conjugate gradient method\n", + "The coefficients are given by" + ] + }, + { + "cell_type": "markdown", + "id": "c91cb637", + "metadata": {}, + "source": [ + "$$\n", + "\\mathbf{A}\\mathbf{x} = \\sum^{n}_{i=1} \\alpha_i \\mathbf{A} \\mathbf{p}_i = \\mathbf{b}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "56866152", + "metadata": {}, + "source": [ + "Multiplying with $\\hat{p}_k^T$ from the left gives" + ] + }, + { + "cell_type": "markdown", + "id": "dc885f5c", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{p}_k^T \\hat{A}\\hat{x} = \\sum^{n}_{i=1} \\alpha_i\\hat{p}_k^T \\hat{A}\\hat{p}_i= \\hat{p}_k^T \\hat{b},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "ecbda348", + "metadata": {}, + "source": [ + "and we can define the coefficients $\\alpha_k$ as" + ] + }, + { + "cell_type": "markdown", + "id": "1cb56c1e", + "metadata": {}, + "source": [ + "$$\n", + "\\alpha_k = \\frac{\\hat{p}_k^T \\hat{b}}{\\hat{p}_k^T \\hat{A} \\hat{p}_k}\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "0e113c26", + "metadata": {}, + "source": [ + "## Conjugate gradient method and iterations\n", + "\n", + "If we choose the conjugate vectors $\\hat{p}_k$ carefully, \n", + "then we may not need all of them to obtain a good approximation to the solution \n", + "$\\hat{x}$. \n", + "We want to regard the conjugate gradient method as an iterative method. \n", + "This will us to solve systems where $n$ is so large that the direct \n", + "method would take too much time.\n", + "\n", + "We denote the initial guess for $\\hat{x}$ as $\\hat{x}_0$. \n", + "We can assume without loss of generality that" + ] + }, + { + "cell_type": "markdown", + "id": "2a417239", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{x}_0=0,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "f8eca051", + "metadata": {}, + "source": [ + "or consider the system" + ] + }, + { + "cell_type": "markdown", + "id": "f301cd17", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{A}\\hat{z} = \\hat{b}-\\hat{A}\\hat{x}_0,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "3ce80544", + "metadata": {}, + "source": [ + "instead." + ] + }, + { + "cell_type": "markdown", + "id": "06d430ae", + "metadata": {}, + "source": [ + "## Conjugate gradient method\n", + "One can show that the solution $\\hat{x}$ is also the unique minimizer of the quadratic form" + ] + }, + { + "cell_type": "markdown", + "id": "df4c262c", + "metadata": {}, + "source": [ + "$$\n", + "f(\\hat{x}) = \\frac{1}{2}\\hat{x}^T\\hat{A}\\hat{x} - \\hat{x}^T \\hat{x} , \\quad \\hat{x}\\in\\mathbf{R}^n.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "70ca1a9d", + "metadata": {}, + "source": [ + "This suggests taking the first basis vector $\\hat{p}_1$ \n", + "to be the gradient of $f$ at $\\hat{x}=\\hat{x}_0$, \n", + "which equals" + ] + }, + { + "cell_type": "markdown", + "id": "2a80836b", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{A}\\hat{x}_0-\\hat{b},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "4546d9d3", + "metadata": {}, + "source": [ + "and \n", + "$\\hat{x}_0=0$ it is equal $-\\hat{b}$.\n", + "The other vectors in the basis will be conjugate to the gradient, \n", + "hence the name conjugate gradient method." + ] + }, + { + "cell_type": "markdown", + "id": "18e13ae9", + "metadata": {}, + "source": [ + "## Conjugate gradient method\n", + "Let $\\hat{r}_k$ be the residual at the $k$-th step:" + ] + }, + { + "cell_type": "markdown", + "id": "877633e0", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{r}_k=\\hat{b}-\\hat{A}\\hat{x}_k.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "b5a1975d", + "metadata": {}, + "source": [ + "Note that $\\hat{r}_k$ is the negative gradient of $f$ at \n", + "$\\hat{x}=\\hat{x}_k$, \n", + "so the gradient descent method would be to move in the direction $\\hat{r}_k$. \n", + "Here, we insist that the directions $\\hat{p}_k$ are conjugate to each other, \n", + "so we take the direction closest to the gradient $\\hat{r}_k$ \n", + "under the conjugacy constraint. \n", + "This gives the following expression" + ] + }, + { + "cell_type": "markdown", + "id": "d4a13896", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{p}_{k+1}=\\hat{r}_k-\\frac{\\hat{p}_k^T \\hat{A}\\hat{r}_k}{\\hat{p}_k^T\\hat{A}\\hat{p}_k} \\hat{p}_k.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "2dbb6628", + "metadata": {}, + "source": [ + "## Conjugate gradient method\n", + "We can also compute the residual iteratively as" + ] + }, + { + "cell_type": "markdown", + "id": "85875f49", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{r}_{k+1}=\\hat{b}-\\hat{A}\\hat{x}_{k+1},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "9efe856c", + "metadata": {}, + "source": [ + "which equals" + ] + }, + { + "cell_type": "markdown", + "id": "470484e3", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{b}-\\hat{A}(\\hat{x}_k+\\alpha_k\\hat{p}_k),\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "049c6212", + "metadata": {}, + "source": [ + "or" + ] + }, + { + "cell_type": "markdown", + "id": "09e38b0d", + "metadata": {}, + "source": [ + "$$\n", + "(\\hat{b}-\\hat{A}\\hat{x}_k)-\\alpha_k\\hat{A}\\hat{p}_k,\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "ffa86d35", + "metadata": {}, + "source": [ + "which gives" + ] + }, + { + "cell_type": "markdown", + "id": "72841aae", + "metadata": {}, + "source": [ + "$$\n", + "\\hat{r}_{k+1}=\\hat{r}_k-\\hat{A}\\hat{p}_{k},\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "88f3cf38", + "metadata": {}, + "source": [ + "## Simple implementation of the Conjugate gradient algorithm" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "213b6a91", + "metadata": {}, + "outputs": [], + "source": [ + " Vector ConjugateGradient(Matrix A, Vector b, Vector x0){\n", + " int dim = x0.Dimension();\n", + " const double tolerance = 1.0e-14;\n", + " Vector x(dim),r(dim),v(dim),z(dim);\n", + " double c,t,d;\n", + "\n", + " x = x0;\n", + " r = b - A*x;\n", + " v = r;\n", + " c = dot(r,r);\n", + " int i = 0; IterMax = dim;\n", + " while(i <= IterMax){\n", + " z = A*v;\n", + " t = c/dot(v,z);\n", + " x = x + t*v;\n", + " r = r - t*z;\n", + " d = dot(r,r);\n", + " if(sqrt(d) < tolerance)\n", + " break;\n", + " v = r + (d/c)*v;\n", + " c = d; i++;\n", + " }\n", + " return x;\n", + "}" + ] + }, + { + "cell_type": "markdown", + "id": "d2847fc8", + "metadata": {}, + "source": [ + "## Broyden–Fletcher–Goldfarb–Shanno algorithm\n", + "The optimization problem is to minimize $f(\\mathbf {x} )$ where $\\mathbf {x}$ is a vector in $R^{n}$, and $f$ is a differentiable scalar function. There are no constraints on the values that $\\mathbf {x}$ can take.\n", + "\n", + "The algorithm begins at an initial estimate for the optimal value $\\mathbf {x}_{0}$ and proceeds iteratively to get a better estimate at each stage.\n", + "\n", + "The search direction $p_k$ at stage $k$ is given by the solution of the analogue of the Newton equation" + ] + }, + { + "cell_type": "markdown", + "id": "46f17a0b", + "metadata": {}, + "source": [ + "$$\n", + "B_{k}\\mathbf {p} _{k}=-\\nabla f(\\mathbf {x}_{k}),\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "480f6bf1", + "metadata": {}, + "source": [ + "where $B_{k}$ is an approximation to the Hessian matrix, which is\n", + "updated iteratively at each stage, and $\\nabla f(\\mathbf {x} _{k})$\n", + "is the gradient of the function\n", + "evaluated at $x_k$. \n", + "A line search in the direction $p_k$ is then used to\n", + "find the next point $x_{k+1}$ by minimising" + ] + }, + { + "cell_type": "markdown", + "id": "45a18714", + "metadata": {}, + "source": [ + "$$\n", + "f(\\mathbf {x}_{k}+\\alpha \\mathbf {p}_{k}),\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "2e64b382", + "metadata": {}, + "source": [ + "over the scalar $\\alpha > 0$." + ] + }, + { + "cell_type": "markdown", + "id": "47d2904d", + "metadata": {}, + "source": [ + "## Stochastic Gradient Descent\n", + "\n", + "Stochastic gradient descent (SGD) and variants thereof address some of\n", + "the shortcomings of the Gradient descent method discussed above.\n", + "\n", + "The underlying idea of SGD comes from the observation that a given \n", + "function, which we want to minimize, can almost always be written as a\n", + "sum over $n$ data points $\\{\\mathbf{x}_i\\}_{i=1}^n$," + ] + }, + { + "cell_type": "markdown", + "id": "a6549f0b", + "metadata": {}, + "source": [ + "$$\n", + "C(\\mathbf{\\beta}) = \\sum_{i=1}^n c_i(\\mathbf{x}_i,\n", + "\\mathbf{\\beta}).\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "e0679ec8", + "metadata": {}, + "source": [ + "## Computation of gradients\n", + "\n", + "This in turn means that the gradient can be\n", + "computed as a sum over $i$-gradients" + ] + }, + { + "cell_type": "markdown", + "id": "138d5b06", + "metadata": {}, + "source": [ + "$$\n", + "\\nabla_\\beta C(\\mathbf{\\beta}) = \\sum_i^n \\nabla_\\beta c_i(\\mathbf{x}_i,\n", + "\\mathbf{\\beta}).\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "058943d0", + "metadata": {}, + "source": [ + "Stochasticity/randomness is introduced by only taking the\n", + "gradient on a subset of the data called minibatches. If there are $n$\n", + "data points and the size of each minibatch is $M$, there will be $n/M$\n", + "minibatches. We denote these minibatches by $B_k$ where\n", + "$k=1,\\cdots,n/M$." + ] + }, + { + "cell_type": "markdown", + "id": "66cb46b8", + "metadata": {}, + "source": [ + "## SGD example\n", + "As an example, suppose we have $10$ data points $(\\mathbf{x}_1,\\cdots, \\mathbf{x}_{10})$ \n", + "and we choose to have $M=5$ minibathces,\n", + "then each minibatch contains two data points. In particular we have\n", + "$B_1 = (\\mathbf{x}_1,\\mathbf{x}_2), \\cdots, B_5 =\n", + "(\\mathbf{x}_9,\\mathbf{x}_{10})$. Note that if you choose $M=1$ you\n", + "have only a single batch with all data points and on the other extreme,\n", + "you may choose $M=n$ resulting in a minibatch for each datapoint, i.e\n", + "$B_k = \\mathbf{x}_k$.\n", + "\n", + "The idea is now to approximate the gradient by replacing the sum over\n", + "all data points with a sum over the data points in one the minibatches\n", + "picked at random in each gradient descent step" + ] + }, + { + "cell_type": "markdown", + "id": "37f5c873", + "metadata": {}, + "source": [ + "$$\n", + "\\nabla_{\\beta}\n", + "C(\\mathbf{\\beta}) = \\sum_{i=1}^n \\nabla_\\beta c_i(\\mathbf{x}_i,\n", + "\\mathbf{\\beta}) \\rightarrow \\sum_{i \\in B_k}^n \\nabla_\\beta\n", + "c_i(\\mathbf{x}_i, \\mathbf{\\beta}).\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "28417f9f", + "metadata": {}, + "source": [ + "## The gradient step\n", + "\n", + "Thus a gradient descent step now looks like" + ] + }, + { + "cell_type": "markdown", + "id": "1371ee83", + "metadata": {}, + "source": [ + "$$\n", + "\\beta_{j+1} = \\beta_j - \\gamma_j \\sum_{i \\in B_k}^n \\nabla_\\beta c_i(\\mathbf{x}_i,\n", + "\\mathbf{\\beta})\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "id": "c57a595a", + "metadata": {}, + "source": [ + "where $k$ is picked at random with equal\n", + "probability from $[1,n/M]$. An iteration over the number of\n", + "minibathces (n/M) is commonly referred to as an epoch. Thus it is\n", + "typical to choose a number of epochs and for each epoch iterate over\n", + "the number of minibatches, as exemplified in the code below." + ] + }, + { + "cell_type": "markdown", + "id": "bb4a5d25", + "metadata": {}, + "source": [ + "## Simple example code" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "af5d1798", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np \n", + "\n", + "n = 100 #100 datapoints \n", + "M = 5 #size of each minibatch\n", + "m = int(n/M) #number of minibatches\n", + "n_epochs = 10 #number of epochs\n", + "\n", + "j = 0\n", + "for epoch in range(1,n_epochs+1):\n", + " for i in range(m):\n", + " k = np.random.randint(m) #Pick the k-th minibatch at random\n", + " #Compute the gradient using the data in minibatch Bk\n", + " #Compute new suggestion for \n", + " j += 1" + ] + }, + { + "cell_type": "markdown", + "id": "b0545474", + "metadata": {}, + "source": [ + "Taking the gradient only on a subset of the data has two important\n", + "benefits. First, it introduces randomness which decreases the chance\n", + "that our opmization scheme gets stuck in a local minima. Second, if\n", + "the size of the minibatches are small relative to the number of\n", + "datapoints ($M < n$), the computation of the gradient is much\n", + "cheaper since we sum over the datapoints in the $k-th$ minibatch and not\n", + "all $n$ datapoints." + ] + }, + { + "cell_type": "markdown", + "id": "c8d6d0cb", + "metadata": {}, + "source": [ + "## When do we stop?\n", + "\n", + "A natural question is when do we stop the search for a new minimum?\n", + "One possibility is to compute the full gradient after a given number\n", + "of epochs and check if the norm of the gradient is smaller than some\n", + "threshold and stop if true. However, the condition that the gradient\n", + "is zero is valid also for local minima, so this would only tell us\n", + "that we are close to a local/global minimum. However, we could also\n", + "evaluate the cost function at this point, store the result and\n", + "continue the search. If the test kicks in at a later stage we can\n", + "compare the values of the cost function and keep the $\\beta$ that\n", + "gave the lowest value." + ] + }, + { + "cell_type": "markdown", + "id": "b333b841", + "metadata": {}, + "source": [ + "## Slightly different approach\n", + "\n", + "Another approach is to let the step length $\\gamma_j$ depend on the\n", + "number of epochs in such a way that it becomes very small after a\n", + "reasonable time such that we do not move at all.\n", + "\n", + "As an example, let $e = 0,1,2,3,\\cdots$ denote the current epoch and let $t_0, t_1 > 0$ be two fixed numbers. Furthermore, let $t = e \\cdot m + i$ where $m$ is the number of minibatches and $i=0,\\cdots,m-1$. Then the function $$\\gamma_j(t; t_0, t_1) = \\frac{t_0}{t+t_1} $$ goes to zero as the number of epochs gets large. I.e. we start with a step length $\\gamma_j (0; t_0, t_1) = t_0/t_1$ which decays in *time* $t$.\n", + "\n", + "In this way we can fix the number of epochs, compute $\\beta$ and\n", + "evaluate the cost function at the end. Repeating the computation will\n", + "give a different result since the scheme is random by design. Then we\n", + "pick the final $\\beta$ that gives the lowest value of the cost\n", + "function." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "1c55be00", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np \n", + "\n", + "def step_length(t,t0,t1):\n", + " return t0/(t+t1)\n", + "\n", + "n = 100 #100 datapoints \n", + "M = 5 #size of each minibatch\n", + "m = int(n/M) #number of minibatches\n", + "n_epochs = 500 #number of epochs\n", + "t0 = 1.0\n", + "t1 = 10\n", + "\n", + "gamma_j = t0/t1\n", + "j = 0\n", + "for epoch in range(1,n_epochs+1):\n", + " for i in range(m):\n", + " k = np.random.randint(m) #Pick the k-th minibatch at random\n", + " #Compute the gradient using the data in minibatch Bk\n", + " #Compute new suggestion for beta\n", + " t = epoch*m+i\n", + " gamma_j = step_length(t,t0,t1)\n", + " j += 1\n", + "\n", + "print(\"gamma_j after %d epochs: %g\" % (n_epochs,gamma_j))" + ] + }, + { + "cell_type": "markdown", + "id": "009e6326", + "metadata": {}, + "source": [ + "## Program for stochastic gradient" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "67b4dc09", + "metadata": {}, + "outputs": [], + "source": [ + "# Importing various packages\n", + "from math import exp, sqrt\n", + "from random import random, seed\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "from sklearn.linear_model import SGDRegressor\n", + "\n", + "x = 2*np.random.rand(100,1)\n", + "y = 4+3*x+np.random.randn(100,1)\n", + "\n", + "xb = np.c_[np.ones((100,1)), x]\n", + "theta_linreg = np.linalg.inv(xb.T.dot(xb)).dot(xb.T).dot(y)\n", + "print(\"Own inversion\")\n", + "print(theta_linreg)\n", + "sgdreg = SGDRegressor(n_iter = 50, penalty=None, eta0=0.1)\n", + "sgdreg.fit(x,y.ravel())\n", + "print(\"sgdreg from scikit\")\n", + "print(sgdreg.intercept_, sgdreg.coef_)\n", + "\n", + "\n", + "theta = np.random.randn(2,1)\n", + "\n", + "eta = 0.1\n", + "Niterations = 1000\n", + "m = 100\n", + "\n", + "for iter in range(Niterations):\n", + " gradients = 2.0/m*xb.T.dot(xb.dot(theta)-y)\n", + " theta -= eta*gradients\n", + "print(\"theta frm own gd\")\n", + "print(theta)\n", + "\n", + "xnew = np.array([[0],[2]])\n", + "xbnew = np.c_[np.ones((2,1)), xnew]\n", + "ypredict = xbnew.dot(theta)\n", + "ypredict2 = xbnew.dot(theta_linreg)\n", + "\n", + "\n", + "n_epochs = 50\n", + "t0, t1 = 5, 50\n", + "m = 100\n", + "def learning_schedule(t):\n", + " return t0/(t+t1)\n", + "\n", + "theta = np.random.randn(2,1)\n", + "\n", + "for epoch in range(n_epochs):\n", + " for i in range(m):\n", + " random_index = np.random.randint(m)\n", + " xi = xb[random_index:random_index+1]\n", + " yi = y[random_index:random_index+1]\n", + " gradients = 2 * xi.T.dot(xi.dot(theta)-yi)\n", + " eta = learning_schedule(epoch*m+i)\n", + " theta = theta - eta*gradients\n", + "print(\"theta from own sdg\")\n", + "print(theta)\n", + "\n", + "\n", + "plt.plot(xnew, ypredict, \"r-\")\n", + "plt.plot(xnew, ypredict2, \"b-\")\n", + "plt.plot(x, y ,'ro')\n", + "plt.axis([0,2.0,0, 15.0])\n", + "plt.xlabel(r'$x$')\n", + "plt.ylabel(r'$y$')\n", + "plt.title(r'Random numbers ')\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "id": "5de3c7a3", + "metadata": {}, + "source": [ + "## Using gradient descent methods, limitations\n", + "\n", + "* **Gradient descent (GD) finds local minima of our function**. Since the GD algorithm is deterministic, if it converges, it will converge to a local minimum of our energy function. Because in ML we are often dealing with extremely rugged landscapes with many local minima, this can lead to poor performance.\n", + "\n", + "* **GD is sensitive to initial conditions**. One consequence of the local nature of GD is that initial conditions matter. Depending on where one starts, one will end up at a different local minima. Therefore, it is very important to think about how one initializes the training process. This is true for GD as well as more complicated variants of GD.\n", + "\n", + "* **Gradients are computationally expensive to calculate for large datasets**. In many cases in statistics and ML, the energy function is a sum of terms, with one term for each data point. For example, in linear regression, $E \\propto \\sum_{i=1}^n (y_i - \\mathbf{w}^T\\cdot\\mathbf{x}_i)^2$; for logistic regression, the square error is replaced by the cross entropy. To calculate the gradient we have to sum over *all* $n$ data points. Doing this at every GD step becomes extremely computationally expensive. An ingenious solution to this, is to calculate the gradients using small subsets of the data called \"mini batches\". This has the added benefit of introducing stochasticity into our algorithm.\n", + "\n", + "* **GD is very sensitive to choices of learning rates**. GD is extremely sensitive to the choice of learning rates. If the learning rate is very small, the training process take an extremely long time. For larger learning rates, GD can diverge and give poor results. Furthermore, depending on what the local landscape looks like, we have to modify the learning rates to ensure convergence. Ideally, we would *adaptively* choose the learning rates to match the landscape.\n", + "\n", + "* **GD treats all directions in parameter space uniformly.** Another major drawback of GD is that unlike Newton's method, the learning rate for GD is the same in all directions in parameter space. For this reason, the maximum learning rate is set by the behavior of the steepest direction and this can significantly slow down training. Ideally, we would like to take large steps in flat directions and small steps in steep directions. Since we are exploring rugged landscapes where curvatures change, this requires us to keep track of not only the gradient but second derivatives. The ideal scenario would be to calculate the Hessian but this proves to be too computationally expensive. \n", + "\n", + "* GD can take exponential time to escape saddle points, even with random initialization. As we mentioned, GD is extremely sensitive to initial condition since it determines the particular local minimum GD would eventually reach. However, even with a good initialization scheme, through the introduction of randomness, GD can still take exponential time to escape saddle points." + ] + }, + { + "cell_type": "markdown", + "id": "80f0f66a", + "metadata": {}, + "source": [ + "## Codes from numerical recipes\n", + "You can however use codes we have adapted from the text [Numerical Recipes in C++](http://www.nr.com/), see chapter 10.7. \n", + "Here we present a program, which you also can find at the webpage of the course we use the functions **dfpmin** and **lnsrch**. This is a variant of the Broyden et al algorithm discussed in the previous slide.\n", + "\n", + "* The program uses the harmonic oscillator in one dimensions as example.\n", + "\n", + "* The program does not use armadillo to handle vectors and matrices, but employs rather my own vector-matrix class. These auxiliary functions, and the main program *model.cpp* can all be found under the [program link here](https://github.com/CompPhysics/ComputationalPhysics2/tree/gh-pages/doc/pub/cg/programs/c%2B%2B).\n", + "\n", + "Below we show only excerpts from the main program. For the full program, see the above link." + ] + }, + { + "cell_type": "markdown", + "id": "f38903db", + "metadata": {}, + "source": [ + "## Finding the minimum of the harmonic oscillator model in one dimension" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "67377884", + "metadata": {}, + "outputs": [], + "source": [ + "// Main function begins here\n", + "int main()\n", + "{\n", + " int n, iter;\n", + " double gtol, fret;\n", + " double alpha;\n", + " n = 1;\n", + "// reserve space in memory for vectors containing the variational\n", + "// parameters\n", + " Vector g(n), p(n);\n", + " cout << \"Read in guess for alpha\" << endl;\n", + " cin >> alpha;\n", + " gtol = 1.0e-5;\n", + "// now call dfmin and compute the minimum\n", + " p(0) = alpha;\n", + " dfpmin(p, n, gtol, &iter, &fret, Efunction, dEfunction);\n", + " cout << \"Value of energy minimum = \" << fret << endl;\n", + " cout << \"Number of iterations = \" << iter << endl;\n", + " cout << \"Value of alpha at minimum = \" << p(0) << endl;\n", + " return 0;\n", + "} // end of main program" + ] + }, + { + "cell_type": "markdown", + "id": "4e7447d8", + "metadata": {}, + "source": [ + "## Functions to observe\n", + "The functions **Efunction** and **dEfunction** compute the expectation value of the energy and its derivative.\n", + "They use the the quasi-Newton method of [Broyden, Fletcher, Goldfarb, and Shanno (BFGS)](https://www.springer.com/it/book/9780387303031)\n", + "It uses the first derivatives only. The BFGS algorithm has proven good performance even for non-smooth optimizations. \n", + "These functions need to be changed when you want to your own derivatives." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "15dc6a71", + "metadata": {}, + "outputs": [], + "source": [ + "// this function defines the expectation value of the local energy\n", + "double Efunction(Vector &x)\n", + "{\n", + " double value = x(0)*x(0)*0.5+1.0/(8*x(0)*x(0));\n", + " return value;\n", + "} // end of function to evaluate\n", + "\n", + "// this function defines the derivative of the energy \n", + "void dEfunction(Vector &x, Vector &g)\n", + "{\n", + " g(0) = x(0)-1.0/(4*x(0)*x(0)*x(0));\n", + "} // end of function to evaluate" + ] + }, + { + "cell_type": "markdown", + "id": "73b9f44a", + "metadata": {}, + "source": [ + "You need to change these functions in order to compute the local energy for your system. I used 1000\n", + "cycles per call to get a new value of $\\langle E_L[\\alpha]\\rangle$.\n", + "When I compute the local energy I also compute its derivative.\n", + "After roughly 10-20 iterations I got a converged result in terms of $\\alpha$." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/doc/src/week6/week6.do.txt b/doc/src/week6/week6.do.txt index 0ceea043..c858f048 100644 --- a/doc/src/week6/week6.do.txt +++ b/doc/src/week6/week6.do.txt @@ -1,9 +1,9 @@ TITLE: Week 8 February 19-23: Gradient Methods AUTHOR: Morten Hjorth-Jensen {copyright, 1999-present|CC BY-NC} Email morten.hjorth-jensen@fys.uio.no at Department of Physics and Center fo Computing in Science Education, University of Oslo, Oslo, Norway & Department of Physics and Astronomy and Facility for Rare Isotope Beams, Michigan State University, East Lansing, Michigan, USA -DATE: today +DATE: February 23, 2024 !split -===== Overview of week 8 ===== +===== Overview ===== !bblock Topics * Gradient methods: o Semi-Newton methods (Broyden's algorithm) @@ -29,7 +29,7 @@ o "Handwritten notes":"https://github.com/CompPhysics/ComputationalPhysics2/blob !split ===== Brief reminder on Newton-Raphson's method ===== -Let us quickly remind ourselves how we derive the above method. +Let us quickly remind ourselves on how we derive the above method. Perhaps the most celebrated of all one-dimensional root-finding routines is Newton's method, also called the Newton-Raphson @@ -440,7 +440,66 @@ leading to the iterative scheme !split -===== Code examples for steepest descent ===== +===== Our simple $2\times 2$ example ===== + +Last week we introduced the simple two-dimensional function +!bt +\[ +f(x_1,x_2)=x_1^2+x_1x_2+10x_2^2-5x_1-3x_2, +\] +!et +which is of the form (in terms of vectors and matrices) +!bt +\[ +f(\bm{x})=\frac{1}{2}\bm{x}^T\bm{A}\bm{x}-\bm{b}^T\bm{x}, +\] +!et +where we have +!bt +\[ +\bm{x}=\begin{bmatrix} x_1 \\ x_2\end{bmatrix}, +\] +!et +!bt +\[ +\bm{b}=\begin{bmatrix} 5 \\ 3\end{bmatrix}, +\] +!et +and +!bt +\[ +\bm{A}=\begin{bmatrix} 2 & 1\\ 1& 20\end{bmatrix}. +\] +!et + +!split +===== Derivatives and more ===== + +Optimizing the above equation, that is +!bt +\[ +\nabla f = 0 = \bm{A}\bm{x}-\bm{b}, +\] +!et +which leads to a simple matrix-inversion problem +!bt +\[ +\bm{x}=\bm{A}^{-1}\bm{b}. +\] +!et +This problem is easy to solve since we can calculate the inverse. Alternatively, we can solve the two coupled equations with two unknowns +!bt +\[ +\frac{\partial f}{\partial x_1}=2x_1+x_2-5=0, +\] +!et +and +!bt +\[ +\frac{\partial f}{\partial x_2}=x_1+20x_2-3=0, +\] +!et +with solutions $x_1=97/39$ and $x_2=1/39$. !split ===== Simple codes for steepest descent and conjugate gradient using a $2\times 2$ matrix, in c++, Python code to come ===== @@ -461,8 +520,8 @@ int main(int argc, char * argv[]){ // Set our initial guess x0(0) = x0(1) = 0; // Set the matrix - A(0,0) = 3; A(1,0) = 2; A(0,1) = 2; A(1,1) = 6; - b(0) = 2; b(1) = -8; + A(0,0) = 2; A(1,0) = 1; A(0,1) = 1; A(1,1) = 20; + b(0) = 5; b(1) = 3; cout << "The Matrix A that we are using: " << endl; A.Print(); cout << endl; @@ -516,15 +575,15 @@ import matplotlib.pyplot as pt from mpl_toolkits.mplot3d import axes3d def f(x): - return 0.5*x[0]**2 + 2.5*x[1]**2 + return x[0]**2 + 10.0*x[1]**2+x[0]*x[1]-5.0*x[0]-3*x[2] def df(x): - return np.array([x[0], 5*x[1]]) + return np.array(2*[x[0]+x[1]-5.0, x[0]+20*x[1]]-3.0) fig = pt.figure() ax = fig.gca(projection="3d") -xmesh, ymesh = np.mgrid[-2:2:50j,-2:2:50j] +xmesh, ymesh = np.mgrid[-2:3:00j,-2:3:00j] fmesh = f(np.array([xmesh, ymesh])) ax.plot_surface(xmesh, ymesh, fmesh) !ec @@ -532,7 +591,7 @@ And then as countor plot !bc pycod pt.axis("equal") pt.contour(xmesh, ymesh, fmesh) -guesses = [np.array([2, 2./5])] +guesses = [np.array([3.0, 0.05])] !ec Find guesses !bc pycod