对于xor有些了解的小伙伴都知道,
一个神经元只能做一条线(也就是所谓的感知机),
(网上爬的一张感知机便于大家看一下)
上图也就解释了为什么一个神经元不能实现XOR,所以这里用了两个神经元
下面是结构图
由于没有找到更简单的图片,于是就临时凑合的画了一个丑陋的结构图(以后有时间会重新编辑)
神经网络结构大致是上面的样子,input层用来输入矩阵,层与层之间的每条线表示一个权重,总共四个,
hide层有两个激活函数,最后由output输出结果(也是在output层进行反向处理)
源代码:
from numpy import exp, array, random, dot
class NeuralNetwork():
def __init__(self):
# 生成随机数种子
random.seed(0)
self.weights1 = 2 * random.random((2, 2)) - 1
random.seed(1)
self.weights2 = 2 * random.random((2, 1)) - 1
# sigmoid 函数
# 正规化操作,使得每个元素都是0~1
def sigmoid(self, x):
return 1 / (1 + exp(-x))
# sigmoid 函数求导
# sigmoid 函数梯度
# 表示我们对当前权重的置信程度
def dsigmoid(self, x):
return x * (1 - x)
# 神经网络——乘积+激活
def activation(self, inputs,weights):
# 把输入传递给神经网络
return self.sigmoid(dot(inputs, weights))
# 神经网络
def train(self, training_set_inputs, training_set_outputs, number_of_training):
for iteration in range(number_of_training):
# 训练集导入神经网络
in_hide = dot(training_set_inputs, self.weights1) # 3*2
out_hide = self.sigmoid(in_hide) # 3*2
in_output = dot(out_hide, self.weights2) #3*1
output = self.sigmoid(in_output) #3*1
# derivative to the weights of the second layer
d_weight2 = dot(out_hide.T, (training_set_outputs - output)*self.dsigmoid(output)) # 2*1
d_weight1 = dot(training_set_inputs.T, dot((training_set_outputs - output)*self.dsigmoid(output),self.weights2.T)*self.dsigmoid(out_hide)) #2*2
self.weights2 += 0.2*d_weight2
self.weights1 += 0.2*d_weight1
neuralnetwork = NeuralNetwork()
print("训练前的权重1")
print(neuralnetwork.weights1)
print("训练前的权重2")
print(neuralnetwork.weights2)
# 训练集,四个样本,3个输入,1个输出
training_set_inputs = array([[0, 0],
[1, 0],
[0, 1],
[1, 1]])
training_set_outputs = array([[0, 1, 1, 0]]).T
# 训练神经网络
# 10000次训练
neuralnetwork.train(training_set_inputs, training_set_outputs, 100000)
print("训练后的权重1")
print(neuralnetwork.weights1)
print("训练后的权重2")
print(neuralnetwork.weights2)
# 新数据测试
predict1 = neuralnetwork.activation([1,1], neuralnetwork.weights1)
predict2 = neuralnetwork.activation(predict1, neuralnetwork.weights2)
print("预测[1,1]")
print(predict2)
predict1 = neuralnetwork.activation([1,0], neuralnetwork.weights1)
predict2 = neuralnetwork.activation(predict1, neuralnetwork.weights2)
print("预测[1,0]")
print(predict2)
predict1 = neuralnetwork.activation([0,1], neuralnetwork.weights1)
predict2 = neuralnetwork.activation(predict1, neuralnetwork.weights2)
print("预测[0,1]")
print(predict2)
predict1 = neuralnetwork.activation([0,0], neuralnetwork.weights1)
predict2 = neuralnetwork.activation(predict1, neuralnetwork.weights2)
print("预测[0,0]")
print(predict2)
print("step:0.2")
暑假自学的时候写的代码,今天心血来潮,整理出来上传了一波,可能有点知识点遗忘了,写的有点不对,请各位多指教