调用循环内的函数时发生NameError

时间:2017-11-01 00:40:29

标签: python visual-studio function loops

我目前是python中的新开发人员,我正在尝试使用我在互联网上找到的与空间入侵者游戏一起工作的代码进行简单的反向传播ANN。

我正在使用Visual Studio Code 2015进行调试,因此我可以理解代码是如何流动的。

我有一个名为spaceinvaders.py的文件,在这个文件中,所有代码都写了很多类,可以创建精灵,声音和所有其他游戏内容。

为了实现ANN,我只是粘贴代码并尝试运行它,但每当我尝试从2个特定函数调用时,我得到“NameError未被用户代码处理 - 全局名称” activate_neuron “未定义”,但功能已定义

我搜索了很多寻找类似问题,但没有想法如何解决它(主要是因为我是一个较新的Python开发人员)。

这是完整的代码,以及在函数“ forward_propagate ”中调用函数“ activate_neuron ”时的错误。

[Other Space Invaders Classes]
    class BackPropagation (object):

        def inicializar_rede_neural(n_inputs, n_hidden, n_outputs):
            network = list()
            #inicializa os pesos da input para a hidden layer
            hidden_layer = [{'weights':[random() for i in range(n_inputs + 1)]} for i in range(n_hidden)]
            network.append(hidden_layer)
            #inicializa os pesos da hidden para a output layer
            output_layer = [{'weights':[random() for i in range(n_hidden + 1)]} for i in range(n_outputs)]
            network.append(output_layer)
            return network

        #Faz a ativação do neurônio
        def activate_neuron(weights, inputs):
            activation = weights[-1]
            for i in range(len(weights)-1):
                activation += weights[i] * inputs[i]
            return activation

        # Transfer neuron activation (função sigmoide)
        def transferir(activation):
            return 1.0 / (1.0 + exp(-activation))

        # Realiza o foward propagate
        def forward_propagate(network, row):
            inputs = row
            for layer in network:
                new_inputs = []
                for neuron in layer:
                    activation = activate_neuron(neuron['weights'], inputs) >Here's the error<
                    neuron['output'] = transferir(activation)
                    new_inputs.append(neuron['output'])
                inputs = new_inputs
            return inputs

        np.random.seed(1)
        network = inicializar_rede_neural(3, 2, 1)
        for layer in network:
            print(layer)
        row = [1, 1, 0, None]
        output = forward_propagate(network, row)
        print(output)

此外,我认为我将尝试调用下一个名为“ transferir ”的函数时出现相同的错误类型。

你们有什么想法解决它? 谢谢!

1 个答案:

答案 0 :(得分:0)

如果你是python的新手,那么我就能理解为什么你错过了这个。代码必须是

activation = self.activate_neuron(neuron['weights'], inputs)

activation = activate_neuron(neuron['weights'], inputs)

每当调用在同一个类中创建的函数时,您需要使用self.function()而不仅仅是function()。这个怪癖主要是为了蟒蛇,但你会看到很多

编辑:整个班级重做

[Other Space Invaders Classes]
    class BackPropagation(object):

        def inicializar_rede_neural(self, n_inputs, n_hidden, n_outputs):
            network = []
            #inicializa os pesos da input para a hidden layer
            hidden_layer = [{'weights':[random() for i in range(n_inputs + 1)]} for i in range(n_hidden)]
            network.append(hidden_layer)
            #inicializa os pesos da hidden para a output layer
            output_layer = [{'weights':[random() for i in range(n_hidden + 1)]} for i in range(n_outputs)]
            network.append(output_layer)
            return network

        #Faz a ativação do neurônio
        def activate_neuron(self, weights, inputs):
            activation = weights[-1]
            for i in range(len(weights)-1):
                activation += weights[i] * inputs[i]
            return activation

        # Transfer neuron activation (self, função sigmoide)
        def transferir(self, activation):
            return 1.0 / (1.0 + exp(-activation))

        # Realiza o foward propagate
        def forward_propagate(self, network, row):
            inputs = row
            for layer in network:
                new_inputs = []
                for neuron in layer:
                    activation = self.activate_neuron(neuron['weights'], inputs) #>Here's the error<
                    neuron['output'] = self.transferir(activation)
                    new_inputs.append(neuron['output'])
                inputs = new_inputs
            return inputs

    np.random.seed(1)
    backprop = BackPropagation()
    network = backprop.inicializar_rede_neural(3, 2, 1)
    for layer in network:
        print(layer)
    row = [1, 1, 0, None]
    output = backprop.forward_propagate(network, row)
    print(output)