Backpropagation

Now that the feedforward can be done, the next step is to decide how the parameters should change such that they minimize the cost function.

Recall that the chosen cost function for this problem is $$ c(x, P) = \sum_i \big(g_t'(x_i, P) - ( -\gamma g_t(x_i, P) \big)^2 $$

In order to minimize it, an optimization method must be chosen.

Here, gradient descent with a constant step size has been chosen.

Before looking at the gradient descent method, let us set up the cost function along with the right ride of the ODE and trial solution.

# The trial solution using the deep neural network:
def g_trial(x,params, g0 = 10):
    return g0 + x*neural_network(params,x)

# The right side of the ODE:
def g(x, g_trial, gamma = 2):
    return -gamma*g_trial

# The cost function:
def cost_function(P, x):
    
    # Evaluate the trial function with the current parameters P
    g_t = g_trial(x,P)
    
    # Find the derivative w.r.t x of the neural network
    d_net_out = elementwise_grad(neural_network,1)(P,x) 
    
    # Find the derivative w.r.t x of the trial function
    d_g_t = elementwise_grad(g_trial,0)(x,P)  
    
    # The right side of the ODE 
    func = g(x, g_t)

    err_sqr = (d_g_t - func)**2
    cost_sum = np.sum(err_sqr)
    
    return cost_sum