Velpus Captiosus
Well-Known Coder
Hello!Another very strange error occurs in the interpreter when trying to run my program in Python.With the help of some members here I corrected my code , now a different error pops up
The error according to the interpreter is in the function
It prints this message:
What is going on?The Node class HAS a attribute nextNode , it is one of the initializing attributes
The error according to the interpreter is in the function
Code:
def calcOutput(self):
Python:
import math
class Node:
def __init__(self,value,nextNode):
self.value = 0
self.b = 0.5
self.prevNodes = []
if nextNode is not None:
self.nextNode = nextNode
self.nextNode.prevNodes.append(self)
def changeB(self,error):
if self.b+error>0:
self.b+=error
else:
print("Weight cannot be < 0")
def printVal(self):
print(self.value,"\n")
def changeVal(self,inp):
self.value = inp
self.nextNode.value += float(self.value)* self.b
class Network:
def __init__(self,nodes):
self.nodes = nodes
def calcOutput(self):
finalNode = self.nodes
while finalNode.nextNode is not None:
finalNode = finalNode.nextNode
return 1-pow(math.e,-(finalNode.value))
def returnOutputNode(self):
finalNode = self.nodes
while finalNode.nextNode is not None:
finalNode = finalNode.nextNode
return finalNode
def digitize(self,val):
if val<0.5:
val = 0
else:
val = 1
return val
def correctBiases(self,node,error):
print(node.value)
node.changeB(error)
if len(node.prevNodes) != 0:
for nd in node.prevNodes:
self.correctBiases(nd,error)
def checkAnswer(self):
y = self.digitize(self.calcOutput())
print(y,"\n")
x = float(input("What is the correct output?\n"))
if x != y:
z = (self.digitize(self.calcOutput())-x)/10
print(z,'\n')
self.correctBiases(self.returnOutputNode(),z)
y1 = Node(0,None)
x1 = Node(0,y1)
x2 = Node(0,y1)
n1 = Network(x1)
while True:
num1 = input("Give me a bit:")
if num1 == 'e':
break
num2 = float(input("Give me another bit:"))
x1.changeVal(float(num1))
x2.changeVal(num2)
print(x1.value," ",x1.b,"\n")
print(x2.value," ",x2.b,"\n")
print(y1.value,"\n")
n1.checkAnswer()
It prints this message:
Code:
Give me a bit:1
Give me another bit:0
1.0 0.5
0.0 0.5
0.5
Traceback (most recent call last):
File "C:\Python\neuralnetwork1.py", line 146, in <module>
n1.checkAnswer()
File "C:\Python\neuralnetwork1.py", line 98, in checkAnswer
y = self.digitize(self.calcOutput())
File "C:\Python\neuralnetwork1.py", line 55, in calcOutput
while finalNode.nextNode is not None:
AttributeError: 'Node' object has no attribute 'nextNode'
What is going on?The Node class HAS a attribute nextNode , it is one of the initializing attributes