gpt4 book ai didi

python - 使用多层感知器的 XOR 分类对于所有输入输出 1

转载 作者:行者123 更新时间:2023-11-30 09:00:11 25 4
gpt4 key购买 nike

我使用具有 1 个隐藏层(2 个神经元)和 1 个输出神经元的神经网络来解决 XOR 问题。

这是我正在使用的代码。它包含主运行文件 xor.py,该文件创建 model.py 中定义的模型。每个神经元由neuron.py中的Neuron类定义

异或.py

from model import Model
import numpy as np

inputs = [[0,0], [0,1], [1,0], [1,1]]
outputs = [0, 1, 1, 0]

m = Model()

m.train(inputs, outputs)

for i in inputs:
p = m.predict(i)
print str(i) + ' => ' + str(p)

模型.py

from neuron import HiddenNeuron, OutputNeuron
import numpy as np

class Model(object):

def __init__(self):
self.hidden = [HiddenNeuron(2) for i in range(2)]
self.output = OutputNeuron(2)

def predict(self, input):
temp = []
for x in range(2):
self.hidden[x].forward(input)
temp.append(self.hidden[x].out)
self.output.forward(temp)
return self.output.out

def train(self, inputs, targets):
it = 0
i = 0
size = len(inputs)
while it < 4:
if i == size:
i = 0
feature = inputs[i]
print '\n\nFeature : ' + str(feature) + '\n'
print 'Output weights : ' + str(self.output.weights)
print 'Hidden 1 weights : ' + str(self.hidden[0].weights)
print 'Hidden 2 weights : ' + str(self.hidden[1].weights)
temp = []
for x in range(2):
self.hidden[x].forward(feature)
temp.append(self.hidden[x].out)
self.output.forward(temp)
self.output.backward(targets[i])
deltas = []
deltas.append(self.output.error)
weights = []
weights.append([self.output.weights[0]])
weights.append([self.output.weights[1]])
for x in range(2):
self.hidden[x].backward(deltas, weights[x])
for x in range(2):
self.hidden[x].update(feature)
self.output.update(temp)
it += 1
i += 1

神经元

import numpy as np
from random import uniform

class Neuron(object):

def activation(self, fx):
return 1/(1 + np.exp(-fx))

def __init__(self, dim, lrate):
self.dim = dim
self.weights = np.empty([dim])
self.weights = [uniform(0,1) for x in range(dim)]
self.bias = uniform(0, 1)
self.lrate = lrate
self.out = None
self.error = None

def update(self, input):
j = 0
for i in input:
delta = self.lrate * self.error
self.weights[j] -= (delta*i)
self.bias += delta
j+=1

def forward(self, input):
j = 0
sum = self.bias
for f in input:
sum += f * self.weights[j]
j+=1
self.out = self.activation(sum)

def backward(self):
pass

class OutputNeuron(Neuron):

def __init__(self, dim, lrate=0.2):
super(OutputNeuron, self).__init__(dim, lrate)

def backward(self, target):
self.error = self.out * (1 - self.out) * (self.out - target)


class HiddenNeuron(Neuron):

def __init__(self, dim, lrate=0.2):
super(HiddenNeuron, self).__init__(dim, lrate)

def backward(self, deltas, weights):
sum = 0
size = len(deltas)
for x in range(size):
sum += deltas[x] * weights[x]
self.error = self.out * (1 - self.out) * sum

最终输出为

[0, 0] => 0.999999991272
[0, 1] => 0.999999970788
[1, 0] => 0.999999952345
[1, 1] => 0.999715564446

最佳答案

我认为错误出在 Neuron.py 函数 update() 中。如果您将 self.bias += delta 更改为 self.bias -= delta ,它应该可以工作,至少对我来说是这样。否则,您将修改偏差以上升到误差表面上的最大值。

下面您可以看到 100000 个训练周期后的输出。

[0, 0] => 0.0174550173543
[0, 1] => 0.983899954593
[1, 0] => 0.983895388655
[1, 1] => 0.0164172288168

关于python - 使用多层感知器的 XOR 分类对于所有输入输出 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43390463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com