NameError:名称'self'未在神经网络示例中定义

时间:2017-11-22 18:02:59

标签: python neural-network

我直接从Tariq Rashid的“制作你自己的神经网络”中运行以下代码。它说“NameError:name'self'未定义”。但它是定义的。代码直接来自本书。

import numpy

#neural network class definition
class neuralNetwork:

    #initialize the neural network
    def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
        # set number of nodes in each input, hidden, output layer
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes

        #learning rate
        self.lr = learningrate
        pass

    #train the neural network
    def train():
        pass

    #query the neural network
    def query():
        pass

#number of input, hidden and output nodes
input_nodes = 3
hidden_nodes = 3
output_nodes = 3

#learning rate is 0.3
learning_rate = 0.3

#create instance of neural network
n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)

numpy.random.rand(3, 3)-0.5

#link weight matrices, wih and who
#weights inside the arrays are w_i_j, where link is from node i to node j in 
the next layer
#w11 w21
#w12 w22 etc
self.wih = (numpy.random.rand(self.hnodes, self.inodes) - 0.5)
self.who = (numpy.random.rand(self.onodes, self.hnodes) - 0.5)

1 个答案:

答案 0 :(得分:2)

<ul> <li>Get to know the business</li> <li>Get to know people (stakeholders, key players, cross-functional partners, etc.)</li> <li>Learn how the team's priorities impact our mission</li> <li>Get to know your projects, the team's projects, who's involved, and your onboarding goals</li> </ul>仅在self函数内定义。您在顶级的代码没有定义,因此错误。

代码是否属于__init__()类中的方法 - 那些空的neuralNetworktrain()方法看起来很可疑。

请注意,您需要将query()作为方法的参数。 (例如:self

看起来你错过了这本书要求你做的事情。我假设你正在尝试this,而且代码确实存在于方法中。