## Red neuronal multicapa de avance con función de limitación estricta

``````import numpy as np
from matplotlib import pyplot as plt
import random
plt.rcParams['font.sans-serif'] = ['SimHei']  # 显示中文标签
plt.rcParams['axes.unicode_minus'] = False

def yxf(x):
if x >= 0:
return 1
else:
return 0

def juzheng(a, b):
sum = 0
for i in range(len(a)):
sum = sum + a[i]*b[i]
return sum

x0_r = []
x1_r = []
x0_r1 = []
x1_r1 = []
x0_r2 = []
x1_r2 = []
label1 = []
label2 = []
label3 = []
for i in range(800):
x0 = 2*random.random()
x1 = 4*random.random()-1
x0_r.append(x0)
x1_r.append(x1)
if 1.5*x0 - x1 - 1 <= 0:
label1.append(1)
elif 1.5*x0 - x1 - 1 > 0:
label1.append(0)
for i in range(800):
x0 = x0_r[i]
x1 = x1_r[i]
if (-1)*x0 - x1 + 4 >= 0:
label2.append(1)
elif (-1)*x0 - x1 + 4 < 0:
label2.append(0)
for i in range(800):
x0 = x0_r[i]
x1 = x1_r[i]
if 4*x0 - x1 - 1 >= 0:
label3.append(1)
elif 4*x0 - x1 - 1 < 0:
label3.append(0)

err = 1
k = 0

w1 = [1+random.random() for i in range(3)]
w2 = [1+random.random() for i in range(3)]
w3 = [1+random.random() for i in range(3)]
w0 = [0.1, 0.1, 0.1]
label0 = []

for i in range(len(label3)):
if label1[i] == 1 and label2[i] == 1 and label3[i] == 1:
label0.append(1)
else:
label0.append(0)
for i in range(len(label1)):
if label0[i] == 1:
plt.plot(x0_r[i], x1_r[i], 'k*')
else:
plt.plot(x0_r[i], x1_r[i], 'bs')
plt.show()
while err > 0:
tj = []
for i in range(len(label1)):
X1 = [x0_r[i], x1_r[i], 1]
# 第一层第一个神经元
I1 = juzheng(X1, w1)
y1 = yxf(I1)
dui1 = label1[i] - y1
w1[0] = w1[0] + 0.005 * dui1 * X1[0]
w1[1] = w1[1] + 0.1 * dui1 * X1[1]
w1[2] = w1[2] + 0.1 * dui1 * X1[2]
# 第一层第二个神经元
I2 = juzheng(X1, w2)
y2 = yxf(I2)
dui2 = label2[i] - y2
w2[0] = w2[0] + 0.005 * dui2 * X1[0]
w2[1] = w2[1] + 0.1 * dui2 * X1[1]
w2[2] = w2[2] + 0.1 * dui2 * X1[2]
# 第一层第三个神经元
I3 = juzheng(X1, w3)
y3 = yxf(I3)
dui3 = label3[i] - y3
w3[0] = w3[0] + 0.005 * dui3 * X1[0]
w3[1] = w3[1] + 0.1 * dui3 * X1[1]
w3[2] = w3[2] + 0.1 * dui3 * X1[2]
# 第二层
Y0 = [y1, y2, y3]
I0 = juzheng(Y0, w0)
y0 = yxf(I0-2.9999)
dui0 = label0[i] - y0
w0[0] = w0[0] + 0.05 * dui0 * Y0[0]
w0[1] = w0[1] + 0.05 * dui0 * Y0[1]
w0[2] = w0[2] + 0.05 * dui0 * Y0[2]
tj.append(abs(dui0))
err = sum(tj)
k = k+1
print('第{}次迭代不满足条件的点有{}个'.format(k, err))

xx = np.linspace(-1, 4, 100)
yab = -w1[2] / w1[1] - w1[0] / w1[1] * xx
yac = -w2[2] / w2[1] - w2[0] / w2[1] * xx
ybc = -w3[2] / w3[1] - w3[0] / w3[1] * xx
print('w1:', w1)
print('w3:', w2)
print('w3:', w3)
print('w0:', w0)
plt.figure()
plt.plot(xx, yab, label='AB')
plt.plot(xx, yac, label='BC')
plt.plot(xx, ybc, label='AC')
plt.legend()
plt.xlim([0, 2])
plt.ylim([-2, 4])
y0_r = []
y1_r = []
y2_r = []
y3_r = []
for i in range(2000):
x0 = 2*random.random()
x1 = 4*random.random()-1
X1 = [x0, x1, 1]
y1 = yxf(juzheng(X1, w1))
y2 = yxf(juzheng(X1, w2))
y3 = yxf(juzheng(X1, w3))
Y = [y1, y2, y3]
y0 = yxf(juzheng(Y, w0)-2.9999)
if y0:
plt.plot(x0, x1, 'k*')
else:
plt.plot(x0, x1, 'gs')
y0_r.append(y0)
y1_r.append(y1)
y2_r.append(y2)
y3_r.append(y3)
plt.show()

``````