Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Neural network
#1
Hello ,

I have this code of neural network:

 import numpy as np

#X = np.array(([1 , 0],),)
#yd = np.array(([1],), )
#xPredicted = np.array(([1 , 1]),)

for i in range (1,200):
  for j in range (1,200):
   x1 = -10 +(0.1)*(i-1) 
   x2 = -10 +(0.1)*(j-1)
   X= np.array(([x1, x2],),)
   #print(X)
   yd = np.array(([x1**2 + x2 +3],),)
   #print(yd)
xPredicted = np.array(([1,1]), )

class Neural_Network(object):
  def __init__(self):
     #parameters
    self.inputSize = 2
    self.outputSize = 1
    self.hiddenSize = 2
 
    #weights
    self.W1 =  np.random.randn(self.inputSize, self.hiddenSize) # (2x2) weight matrix from input to hidden layer
    self.W2 =  np.random.randn(self.hiddenSize, self.outputSize) # (2x1) weight matrix from hidden to output layer
 
  def forward(self, X):
     
    #forward propagation through our network
     
    self.H1 = np.dot(X, self.W1) # dot product of X (input) and  the first set of 3x3 weights
    self.Y1 = self.sigmoid(self.H1) # the first activation function
    self.H2 = np.dot(self.Y1, self.W2) # dot product of the  hidden layer and the second set of 3x1 weights
    Z = self.sigmoid(self.H2) # final activation function
    return Z

  def sigmoid(self, s):
    # activation function
    return 1/(1+np.exp(-s))

  def sigmoidPrime(self, s):
    #derivative of sigmoid
    return s * (1 - s)

  def backward(self, X, yd, Z):
      
    # backward propagate through the network
    self.E_error = yd- Z  # error in output
    self.Z_delta = self.E_error *self.sigmoidPrime(Z) # applying derivative of sigmoid to error

    self.Y1_error = self.Z_delta.dot(self.W2.T) # Y1 error: how much our hidden layer weights contributed to output error
    self.Y1_delta = self.Y1_error*self.sigmoidPrime(self.Y1) # applying derivative of sigmoid to Y2 error

    
    self.W1 += X.T.dot(self.Y1_delta) # adjusting the set (hidden --> input) weights
    self.W2 += self.Y1.T.dot(self.Z_delta) # adjusting the set (hidden --> output) weights

  def train(self, X, Z):
    Z = self.forward(X)
    self.backward(X, yd, Z)


  def predict(self):
    print ("Predicted data based on trained weights: ");
    print ("Input (scaled): \n" + str(xPredicted));
    print ("Output: \n" + str(self.forward(xPredicted)));

NN = Neural_Network()
for k in range(100): # trains the NN 1,000 times
  print ("# " + str(k) + "\n")
  print ("Input (scaled): \n" + str(X))
  print ("Actual Output: \n" + str(yd))
  print ("Predicted Output: \n" + str(NN.forward(X)))
  print ("Loss: \n" + str((yd - NN.forward(X)))) 
  print ("\n")
  NN.train(X, yd)
  NN.predict()
I want to test the code for different values of X and Y for this recent I used the for loop :

for i in range (1,200):
  for j in range (1,200):
   x1 = -10 +(0.1)*(i-1) 
   x2 = -10 +(0.1)*(j-1)
   X= np.array(([x1, x2],),)
   #print(X)
   yd = np.array(([x1**2 + x2 +3],),)
   #print(yd)
My problem I don't obtain all the possibility of X and Y I have 200 possibility and one I run the code I just obtain one possibility .

Output:
# 0 Input (scaled): [[9.8 9.8]] Actual Output: [[108.84]] Predicted Output: [[0.56752375]] Loss: [[108.27247625]] Predicted data based on trained weights: Input (scaled): [1 1] Output: [1.] # 1 Input (scaled): [[9.8 9.8]] Actual Output: [[108.84]] Predicted Output: [[1.]] Loss: [[107.84]] Predicted data based on trained weights: Input (scaled): [1 1] Output: [1.] # 2 Input (scaled): [[9.8 9.8]] Actual Output: [[108.84]] Predicted Output: [[1.]] Loss: [[107.84]] Predicted data based on trained weights: Input (scaled): [1 1] Output: [1.] # 3 Input (scaled): [[9.8 9.8]] Actual Output: [[108.84]] Predicted Output: [[1.]] Loss: [[107.84]] Predicted data based on trained weights: Input (scaled): [1 1] Output: [1.]
Please who can help me.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Neural network and data analysis from clients survey result pthon3 2 1,864 Mar-17-2022, 02:21 AM
Last Post: jefsummers
  Multilayer Perceptron Neural Network erdemath 3 2,282 Aug-09-2021, 11:07 PM
Last Post: jefsummers
  Neural Network importance weights / coefficients jkaustin 1 2,028 Nov-10-2020, 07:44 PM
Last Post: jefsummers
  Get error message in a GAN neural network tutorial jdude50 0 1,648 Oct-22-2020, 11:11 PM
Last Post: jdude50
  Explain Me Neural Network Ai's Harshil 2 1,973 Oct-22-2020, 04:50 AM
Last Post: Harshil
  construction of Neural Network for solving Differential equations arshad 0 1,608 Jun-04-2020, 09:20 AM
Last Post: arshad
  Neural Network mensagem error Dalpi 2 2,826 May-20-2020, 04:03 PM
Last Post: Dalpi
  coding neural network programmer19890620 4 3,391 Feb-27-2020, 04:26 AM
Last Post: programmer19890620
  Why does this simple neural network not learn? PythonIsGreat 1 2,082 Aug-30-2019, 05:49 PM
Last Post: ThomasL
  First neural network: Problem with the weight factos 11112294 0 2,142 Jan-12-2019, 09:11 PM
Last Post: 11112294

Forum Jump:

User Panel Messages

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