3

我正在嘗試使用反向傳播實現多層感知器,但我仍然不能教他XOR,我也經常會遇到數學範圍錯誤。我看了書和谷歌學習規則和誤差反向傳播方法,但我仍然不知道哪裏是我的錯誤蟒蛇 - 多層感知器,反向傳播,無法學習XOR

def logsig(net): 
    return 1/(1+math.exp(-net)) 

def perceptron(coef = 0.5, iterations = 10000): 
    inputs = [[0,0],[0,1],[1,0],[1,1]] 
    desiredOuts = [0,1,1,0] 
    bias = -1 
    [input.append(bias) for input in inputs] 
    weights_h1 = [random.random() for e in range(len(inputs[0]))] 
    weights_h2 = [random.random() for e in range(len(inputs[0]))] 
    weights_out = [random.random() for e in range(3)] 
    for itteration in range(iterations): 
     out = [] 
     for input, desiredOut in zip(inputs, desiredOuts): 
       #1st hiden neuron 
      net_h1 = sum(x * w for x, w in zip(input, weights_h1)) 
      aktivation_h1 = logsig(net_h1) 
       #2st hiden neuron 
      net_h2 = sum(x * w for x, w in zip(input, weights_h2)) 
      aktivation_h2 = logsig(net_h2) 
       #output neuron 
      input_out = [aktivation_h1, aktivation_h2, bias] 
      net_out = sum(x * w for x, w in zip(input_out, weights_out)) 
      aktivation_out = logsig(net_out)    
       #error propagation   
      error_out = (desiredOut - aktivation_out) * aktivation_out * (1- aktivation_out) 
      error_h1 = aktivation_h1 * (1-aktivation_h1) * weights_out[0] * error_out 
      error_h2 = aktivation_h2 * (1-aktivation_h2) * weights_out[1] * error_out 
       #learning    
      weights_out = [w + x * coef * error_out for w, x in zip(weights_out, input_out)] 
      weights_h1 = [w + x * coef * error_out for w, x in zip(weights_h1, input)] 
      weights_h2 = [w + x * coef * error_out for w, x in zip(weights_h2, input)]    
      out.append(aktivation_out) 
    formatedOutput = ["%.2f" % e for e in out] 
    return formatedOutput 

回答

2

我發現的唯一的事情是,你要更新weights_h1weights_h2error_out代替error_h1error_h2。換句話說:

weights_h1 = [w + x * coef * error_h1 for w, x in zip(weights_h1, input)] 
weights_h2 = [w + x * coef * error_h2 for w, x in zip(weights_h2, input)] 
+0

是的,就是這樣。非常感謝 – user2173836 2013-03-15 12:48:04

0

數學距離誤差很可能從math.exp(-net)計算的到來,對於淨大的負號。