Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Attribute not found

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
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 :thinking:, it is one of the initializing attributes
 
Hiya,
Thank you for sharing your code and error details! The error message you're encountering:

AttributeError: 'Node' object has no attribute 'nextNode'

is essentially Python's way of telling us that it's trying to access an attribute named nextNode from an instance of Node, but it can't find it. Let's try to troubleshoot:

  1. Initialization Check:Ensure that every Node is initialized properly and that nextNode is either another Node instance or None. The issue might occur if somewhere nextNode is not properly set.
  2. Correct Node Passing:When you create the network instance using n1 = Network(x1), make sure x1 and any subsequent nodes are connected as you expect them to be. Each node should point to the next one, forming a linked list, and the last node in the sequence should have nextNode set to None.
  3. Debugging:Consider adding some print statements in your calcOutput method, to verify that finalNode is indeed an instance of Node and to understand how your while loop is traversing through the nodes:
    python
Code:
 def calcOutput(self):
    finalNode = self.nodes
while finalNode.nextNode is not None:
print(f"Current node value: {finalNode.value}")  # Debugging line
        finalNode = finalNode.nextNode
return 1-pow(math.e,-(finalNode.value))

This can help you trace through the execution and see where it might be going wrong.
Safeguarding Your Code:To make your code a bit more robust and avoid these kinds of runtime errors, you might consider adding some checks before accessing attributes. For instance:
Code:
while getattr(finalNode, 'nextNode', None) is not None:
This will check if nextNode exists before trying to access it, preventing the AttributeError.
Lastly, you might find it helpful to break your problem down into smaller, more manageable pieces and test each piece independently. Creating unit tests for your classes/methods might be beneficial to ensure that every part is working as expected. When everything works in isolation, start combining them back together gradually and test at each stage.

I hope this helps! If you're still having issues, please let me know, and we can dig deeper together. Happy coding!
 
Thanks a lot for the advice!You know what its really weird because it doesnt hit a error in
Code:
changeVal()
which uses the nextNode attribute yet it does in
Code:
calcOutput()
which means the error is somewhere in the iteration I guess.
 
I made the function recursive:

Code:
def calcOutput(self,node):

        if node.nextNode is None:

            return 1-pow(math.e,-(node.value))

        else:

            self.calcOutput(node.nextNode)

but it still doesnt work.Then I checked the line of code and it is indeed the line of the while instruction.I ran some tests and the link between the output and the input is not broken yet for some reason the function doesnt exit the while loop when finalNode (now in the case of
Code:
returnOutputNode()
becomes y1 and I would like too know why this is happening.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom