Python Forum
construction of Neural Network for solving Differential equations
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
construction of Neural Network for solving Differential equations
#1
hello there,

i m trying to construct a code for a neural network to solve differential equations
here is the code, can one plz take a look of the errors. will be grateful

import numpy as np
from autograd import grad, elementwise_grad

N = 5
x = np.linspace(0, 1, N)

n_neurons = 5

w_h = np.random.randn(n_neurons, N)
w_o = np.random.randn(n_neurons, 1)
w = [w_h, w_o]

def sigmoid(z):
    return 1/(1+np.exp(-z))

def NN(x, w):
    a = np.dot(w_h, x)
    a1 = sigmoid(a)
    b = np.dot(a1, w_o)
    b1 = sigmoid(b)
    return b1 

def gt(x, w):
    g0 = 1
    return g0+x*NN(x, w)

def g(x, gt):
    return x**3 + 2.*x + x**2 * ((1. + 3.*x**2) / (1. + x + x**3))-gt(x, w)*(x + (1. + 3.*x**2) / (1. + x + x**3))

def d_segmoid(z):
    return sigmoid(z)*(1-sigmoid(z))

def d_NN(x, w):
    k = 1
    a11 = np.dot(w_o.T, w_h.T**k)
    return np.dot(a11, d_segmoid(x))


def loss_function(x, w):
    
    err = (d_NN(x, w)-g(x, gt))**2
    cost_sum = np.sum(err)
    
    return cost_sum/ np.size(err)

def optimalw(x, lmb):
    lmb = 0.001
    for i in range(1000):
        loss_function_grad = grad(loss_function,0)
    
        w[0] = w[0]-lmb*loss_function_grad[0]
        w[1] = w[1]-lmb*loss_function_grad[1]
    
    return w
Error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-26-9381da78f871> in <module> 8 9 return w ---> 10 optimalw(x, lmb) <ipython-input-26-9381da78f871> in optimalw(x, lmb) 4 loss_function_grad = grad(loss_function,0) 5 ----> 6 w[0] = w[0]-lmb*loss_function_grad[0] 7 w[1] = w[1]-lmb*loss_function_grad[1] 8 TypeError: 'function' object is not subscriptable
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Neural network and data analysis from clients survey result pthon3 2 1,899 Mar-17-2022, 02:21 AM
Last Post: jefsummers
  Multilayer Perceptron Neural Network erdemath 3 2,333 Aug-09-2021, 11:07 PM
Last Post: jefsummers
  Neural Network importance weights / coefficients jkaustin 1 2,061 Nov-10-2020, 07:44 PM
Last Post: jefsummers
  Get error message in a GAN neural network tutorial jdude50 0 1,675 Oct-22-2020, 11:11 PM
Last Post: jdude50
  Explain Me Neural Network Ai's Harshil 2 2,011 Oct-22-2020, 04:50 AM
Last Post: Harshil
  plot differential equation sympy dan_adi 0 2,990 Oct-13-2020, 10:44 AM
Last Post: dan_adi
  Neural Network mensagem error Dalpi 2 2,868 May-20-2020, 04:03 PM
Last Post: Dalpi
  coding neural network programmer19890620 4 3,434 Feb-27-2020, 04:26 AM
Last Post: programmer19890620
  Why does this simple neural network not learn? PythonIsGreat 1 2,104 Aug-30-2019, 05:49 PM
Last Post: ThomasL
  Need Help solving second order differential equations SkewedZone 2 3,109 Jun-25-2019, 12:14 PM
Last Post: SkewedZone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020