import numpy as np
import matplotlib.pyplot as plt
#此示例神经网络的样本输入输出分别是二进制和十进制0-8
#输入样本
X=np.array([
[0,0,0],
[0,0,1],
[0,1,0],
[0,1,1],
[1,0,0],
[1,0,1],
[1,1,0],
[1,1,1]
])
#输出样本
Y=np.array([
[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7]
])
#构造神经网络 1个输入层3个神经元 1个输出层1个神经元 激活函数为ReLU
W=np.random.rand(1,3)
print("初始模型随机权重:",W)
def relu(x):
x[x<0]=0
return x
def relu_derivative(x):
return (x>0).astype(int)
learning_rate=0.1
train_times=100
costs=[]
for i in range(train_times):
for j in range(len(X)):
y=relu(W.dot(X[j]))
cost=Y[j]-y
cost2=cost**2
costs.append(cost2)
gradient=-np.dot(cost*relu_derivative(W.dot(X[j])), X[j].reshape(1,3))