Learner
Learner

Reputation: 837

AttributeError: object x has no attribute y

Out of curiosity I tried to write a basic neural network myself, but I ran into an error when I tried to initialize the object.

class NeuralNet(object):

    def __init__(self,layers,activication = "tanh"):

        if activication == "sigmoid":
            self.activication = sigmoid
            self.activication_deriv = sigmoid_derivative

        elif activication == "tanh":
            self.activication = tanh
            self.activication_deriv = tanh_deriv

        """initializing weights with random values
           between -0.25 and 0.25 and also adding bias unit"""
        self.weigths = []
        for i in range(1,len(layers)-1):
            self.weights.append((2*np.random((layers[i - 1] + 1, layers[i] + 1))-1)*0.25)
        self.weights.append((2*np.random.random((layers[i] + 1, layers[i + 1]))-1)*0.25)

This is how I tried to initialize it:

nn = NeuralNet([1,2,1],"tanh")

The error:

traceback (most recent call last):
  File "/home/a/Documents/LiClipse Workspace/machine_learning/src/sf.py", line 180, in <module>
    single_model(train_set,labels)
  File "/home/a/Documents/LiClipse Workspace/machine_learning/src/sf.py", line 130, in single_model
    nn = NeuralNet([1,2,1],"tanh")
  File "/home/a/Documents/LiClipse Workspace/machine_learning/src/neuralnetwork.py", line 41, in __init__
    self.weights.append((2*np.random((layers[i - 1] + 1, layers[i] + 1))-1)*0.25)
AttributeError: 'NeuralNet' object has no attribute 'weights'

As you can see my Object doesn't have the attribute weights. I am new to object orientation in python, so I'm not sure what I'm doing wrong here. I tried to look into other neural network implementations, and into similar stackoverflow questions, but I was unable to derive the solution from them.

Upvotes: 0

Views: 575

Answers (1)

Maciej Gol
Maciej Gol

Reputation: 15864

self.weigths = []
self.weights.append(...)

Note 'gths' and 'ghts'.

Upvotes: 4

Related Questions