感知器实验

import matplotlib.pyplot as plt
import sys
 
x1 = np.array([0, 0, 1, 1])
x2 = np.array([0, 1, 0, 1])

y = np.array([0, 0, 0, 1])

y1 = np.array([0, 1, 1, 1])
w1 = 0.6
w2 = 0.6
b = -0.7
alpho = 0.001
e0 = 0
j = 0
 

xx = []
yy = []
zz = []
 

def fx(x1, x2):
    return w1 * x1 + w2 * x2 + b
 

def hy(xx1, xx2):
    if (fx(xx1, xx2) > 0):
        return 1
    else:
        return 0
 
while (j < 200000):
    b = b - (fx(x1[j % 4], x2[j % 4]) - y[j % 4]) * alpho
    w1 = w1 - (fx(x1[j % 4], x2[j % 4]) - y[j % 4]) * x1[j % 4] * alpho
    w2 = w2 - (fx(x1[j % 4], x2[j % 4]) - y[j % 4]) * x2[j % 4] * alpho
    xx.append(b)
    yy.append(w1)
    zz.append(w2)

    if (abs(fx(x1[j % 4], x2[j % 4]) - y[j % 4]) < e0 and abs(
     (fx(x1[j % 4], x2[j % 4]) - y[j % 4]) * x1[j % 4]) < e0 and abs(
            (fx(x1[j % 4], x2[j % 4]) - y[j % 4]) * x2[j % 4]) < e0):
        print("true")
        print(fx(x1[j % 4], x2[j % 4]) - y[j % 4])
        print(abs((fx(x1[j % 4], x2[j % 4]) - y[j % 4]) * x1[j % 4]))
        print(abs((fx(x1[j % 4], x2[j % 4]) - y[j % 4]) * x2[j % 4]))
        print(b, w1, w2)
    j = j + 1
 
plt.plot(xx,"*")
plt.plot(yy,",")
plt.plot(zz,".")
plt.show()
print("ok")
print((hy(x1[0], x2[0]) - y[0]))
print(b, w1, w2)
 

感知器实验_第1张图片

 

你可能感兴趣的:(python)