import random
import numpy as np
def sigmod_Z(X, W_raw):
x_r = np.dot(X, W_raw.T)
result = 1 / (1 + np.exp(-x_r))
return result
def deta(sg, X, t):
dt0 = (sg - t) * sg * (1 - sg)
dt1 = (sg - t) * sg * (1 - sg) * X[1]
dt2 = (sg - t) * sg * (1 - sg) * X[2]
dt = [dt0, dt1, dt2]
return dt
def y_pre(Xinput, W):
sigmod2 = np.dot(Xinput, W.T)
result = 1 / (1 + np.exp(-sigmod2))
return result
Error = 0.001
AF = 0.001
train_data = np.array([[1, 1, 1], [1, 0, 0], [0, 1, 0], [0, 0, 0]])
pre = []
rel = []
j = random.randint(0, 2)
x1 = train_data[j][0]
x2 = train_data[j][1]
t = train_data[j][2]
X = np.array(([1, x1, x2]))
w0 = 1
w1 = 1
w2 = 1
W_raw = np.array(([w0, w1, w2]))
print("输入训练的参数X:", X)
dt0 = 0.1
dt1 = 0.1
dt2 = 0.1
while dt0 >= Error or dt1 >= Error or dt2 >= Error:
w0 = w0 - AF * dt0
w1 = w1 - AF * dt1
w2 = w2 - AF * dt2
W = np.array(([w0, w1, w2]))
print("----------------------W:", W)
X = np.array(([1, x1, x2]))
sg = sigmod_Z(X, W)
dt = deta(sg, X, t)
dt0 = dt[0]
dt1 = dt[1]
dt2 = dt[2]
print(dt)
W = np.array([w0, w1, w2])
X_input = np.array([0, 1, 0])
y_pre = y_pre(X_input, W)
print("W:", W)
print("X_input", X_input)
print("y_pre:", y_pre)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ef5c67eb94999e606d0ccbf888660fd1.png)