在封闭范围内赋值之前引用的自由变量“ ....”

时间:2019-05-17 18:39:02

标签: python machine-learning scope neural-network perceptron

在实施二维AND感知器时,我遇到了错误

  在封闭范围内赋值之前引用了

自由变量'...'

我在网上查找了该邮件,在大多数情况下,我都知道为什么会出现此错误。但就我而言,我不知道为什么这个错误会消失。我对python很陌生,如果您能指出我的错误,我将不胜感激!:)这是我的代码:

import numpy as np

b = 0 #bias
weights = np.array([0,0]) #weight
alpha = 0.5 #learning rate
input_data = np.array([([0,0], 0),([0,1],0),([1,0],0),([1,1], 1)])  #data points that we want to separate
d = np.array([input_data[i][1] for i in range(len(input_data))]) #d will not change
x = np.array([input_data[i][0] for i in range(len(input_data))]) #x is the positions of the data points. They do not change either.


def perceptron():
    #repeat until the input error is zero
    while True:
        output = np.array([np.dot(weights, x[i]) + b for i in range(len(x))]) #y will be updated for each round
        y = np.array([1 if output[i]>0 else 0 for i in range(len(output))])
        for i in range(len(x)):
            weights = weights + alpha * (d[i] - y[i]) * x[i]
            b = b + alpha * (d[i]- y[i])
        if np.array_equal(d,y):
            break
    return weights, b  

错误是:

  

在封闭范围内赋值之前引用的自由变量“权重”

但是在定义perceptron()之前,我已经在每个第一个开始声明了“权重”。

0 个答案:

没有答案