Python Forum
Loop Value Stays the Same
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop Value Stays the Same
#1

Hello,
I have this code after the modification of W I want to obtain the new value of E.I use the for loop but the value stay the same of the first iteration.
Please who have any idea about that.


import numpy as np

Yd =0.4
lamda=10
delt =0.1
x1=0.8
x2=0.2
w01=w02=w03=w11=w12=w13=w21=w22=w23=0
W= np.array ([[w01],[w02],[w03],[w11],[w12],[w13],[w21],[w22],[w23]])

for i  in range (100):
    
 y1 = ( w01+ w11*x1+w21*x2)
y2=(w02+w12*x1+w22*x2)
Z = ((np.array([[w03,w13,w23]])).dot (np.array([[1],[y1],[y2]])))
E=(Z-Yd)

print(E)

dw01=w13
dw02=w23
dw03=1
dw11=w13*x1
dw12=w23*x1
dw13=w01+w11*x1+w21*x2
dw21=w13*x2
dw22=w23*x2
dw23=w02+w12*x1+w22*x2

F=  (np.array([[dw01,dw02,dw03,dw11,dw12,dw13,dw21,dw22,dw23]]))
FT = (np.transpose(F))
cte= (- FT.dot(F.dot(FT) ** (-1))*lamda*E)
W=W+delt*cte
Reply
#2
The only thing that changes is "i" and you don't use it anywhere under the loop. Please don't use i, l, or O as single digit variable names as they can look like numbers depending on the font.
Reply
#3
Please how I can use "i" under the loop?

I'm a new user of python and I don't find a solution for that
Reply
#4
So you want a new value of E after modifying W. Since you don't modify W until line 33, I guess you would need to indent everything from line 14 to line 33. The loop only works for the code that is indented under it.

BTW, I say "I guess" because your code is completely unintelligible, so I have no clue what it is doing. I would strongly suggest that you learn to comment your code and use descriptive variable names.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
The loop instantiates a variable which can be using like any other variable. It can be named whatever you'd prefer. Remember that it is local to the loop so it can only be used within the loop:

for index in range(10):
    x = index * 3
    y = index + 10
    print(y ** x)
Reply
#6
hello,
When I try to run this code I obtain this error:
Error:
Traceback (most recent call last): File "C:\Users\USER\Desktop\2008.py", line 57, in <module> NN.train( X,W, yd) File "C:\Users\USER\Desktop\2008.py", line 48, in train self.backprop() File "C:\Users\USER\Desktop\2008.py", line 43, in backprop W= W+0.1*cte UnboundLocalError: local variable 'W' referenced before assignment
how can help me please

import numpy as np 
      

x0=1
x1=0.8
x2=0.2
X=np.array(([x0,x1,x2]), dtype=float)
yd=np.array(([0.4]), dtype=float)
w1=w2=w3=w4=wb1=wb2=w5=w6=wb3=0
W = np.array([[w1],[w2],[w3],[w4],[wb1],[wb2],[w5],[w6],[wb3]])


# Class definition
class NeuralNetwork:
    def __init__(self, X,W,yd):
        self.X = X
        self.W= W
        self.yd = yd
        self.z =  np. zeros(yd.shape)
        
    def feedforward(self):
        self.y1 = wb1+w1*x1+w2*x2
        self.y2 = wb2+w3*x1+w4*x2
        self.z = wb3+w5*self.y1+w6*self.y2
        return self.z
        
    def backprop(self):
        dw1=w5*x1
        dw2=w5*x2
        dw3=w6*x1
        dw4=w6*x2
        dwb1=w5
        dwb2=w6
        dw5=wb1+w1*x1+w2*x2
        dw6=wb2+w3*x1+w4*x2
        dwb3=1
        
        F=(np.array([[dw1,dw2,dw3,dw4,dwb1,dwb2,dw5,dw6,dwb3]]))
  
        FT = np.transpose(F)       
        cte= - FT.dot(F.dot(FT) ** (-1))*10*( NN.feedforward()-yd)
        W= W+0.1*cte
       

    def train(self, X ,W,yd):
        self.feedforward()
        self.backprop()
        

NN = NeuralNetwork( X,W ,yd)
for i in range(100): # trains the NN 100 times
    #if i % 100 ==0: 
        print ("loss: \n" + str( NN.feedforward()-yd))
        print ("\n")
  
NN.train( X,W, yd)
   
Reply
#7
As the error states, you did not define W. You did define self.W in your __init__, but that does not allow you to reference W anywhere in the class. You need to use self.W to refer to the self.W you created in __init__ (and self.X, self.yd, self.z, self.y1, and so on).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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