0
class neuralNetwork:
    def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
        self.inodes = input_nodes
        self.hnodes = hidden_nodes
        self.onodes = output_nodes
        self.lr = learning_rate
        pass

    def train():
        pass

    def query():
        pass

input_nodes = 3
hidden_nodes = 3
output_nodes = 3

learning_rate = 0.3
n = neuralNetwork(input_nodes,hidden_nodes,output_nodes, learning_rate)

import numpy


self.wih = (numpy.random.rand(self.hnodes, self.inodes) - 0.5)
self.who = (numpy.random.rand(self.onodes, self.hnodes) - 0.5)



---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-20-8474973d8450> in <module>()
----> 1 self.wih = (numpy.random.rand(self.hnodes, self.inodes) - 0.5)
  2 self.who = (numpy.random.rand(self.onodes, self.hnodes) - 0.5)
NameError: name 'self' is not defined

Why is self not defined?

so how can I fix this error... I overlooked it several times, but still can't find any Solutions to it. Even though it is explained like this in a tutorial. Help would be appreciated.

Josh K
  • 28,364
  • 20
  • 86
  • 132
  • 1
    Because you never defined `self`... – juanpa.arrivillaga Sep 21 '17 at 18:28
  • 1
    "self" is only defined within the neural network functions you've defined (which, by the way, should be indented inside the "class" definition). You need to replace "self" with "n" because it's your neural network class, I think? – Max Sep 21 '17 at 18:28
  • how do i define self then? – kamazibel Sep 21 '17 at 18:34
  • The way you *define any other variable*. But I think the most important thing for you to understand is what `self` inside your methods *actually is*. IOW, learn the basics of OOP in Python. – juanpa.arrivillaga Sep 21 '17 at 19:21

1 Answers1

2

self is passed into the call to an instance method. You're looking to address the instance itself.

nnet = neuralNetwork(input_nodes,hidden_nodes,output_nodes, learning_rate)
nnet.wih = (numpy.random.rand(nnet.hnodes, nnet.inodes) - 0.5)
nnet.who = (numpy.random.rand(nnet.onodes, nnet.hnodes) - 0.5)

Briefly talking about how self works, it's not magical. Taking an example class:

class Foo(object):
    def __init__(self, name):
        self.name = name

    def speak(self):
        return "Hello {}".format(self.name)

self is passed into speak when it's invoked as a bound function. One could call it this instead, but self is the Python convention.

Josh K
  • 28,364
  • 20
  • 86
  • 132
  • @kamazibel Fixed, I hadn't updated how I declared that variable. – Josh K Sep 21 '17 at 18:37
  • TypeError: __init__() missing 4 required positional arguments: 'input_nodes', 'hidden_nodes', 'output_nodes', and 'learning_rate' – kamazibel Sep 21 '17 at 18:40
  • @kamazibel Copied your arguments into the declaration. Are you meaning to assign `wih` and `who` to the `neuralNetwork`? – Josh K Sep 21 '17 at 18:42
  • @kamazibel You need to initialize your parameters (`input_nodes`, `hidden_nodes`, `output_nodes` and `learning_rate`) as you did in your question. Then use @joshk 's code above. – tdube Sep 21 '17 at 18:44