单层神经网络(感知器)的python实现
程序员文章站
2024-03-14 11:32:58
...
构造一个单层网络,**函数是sigmoid,损失函数是均方根。
参数w0和b0,输入X,输入层L0,输出层L1,预测值Y,lr(n)学习速率
python如下:
import numpy as np
# sigmoid function
def sig(x):
return (1/(1+np.exp(-x)))
def deridig(y):
return y*(1-y)
# input dataset
X = np.array([ [0,0,1],
[0,1,1],
[1,0,1],
[1,1,1] ])
# output dataset
y = np.array([[0,0,1,1]]).T
#
np.random.seed(1)
# initialize weights randomly with mean 0
w0 = 2*np.random.random((3,1)) - 1
b0=1
lr=1
print ('w000',w0)
for iter in range(10000):
# forward propagation
l0 = X
l1 = sig(np.dot(l0,w0)+b0)
#print ('l1',l1)
# how much did we miss?
E_error=(y-l1)*(y-l1)
# update weights
w0 += 1*np.dot(l0.T,2*(y-l1) *deridig(l1))
b0 += 1*2*(y-l1) *deridig(l1)
#print ('w0',w0)
print ("Output After Training:")
print (l1)
print ("w0 After Training:")
print (w0)
print ("b0 After Training:")
print (b0)
循环10000次后的结果:
Output After Training:
[[ 0.00411501]
[ 0.00384688]
[ 0.99675082]
[ 0.99643784]]
w0 After Training:
[[ 5.05661648]
[-0.2814999 ]
[-2.84714308]]
b0 After Training:
[[-2.64189825]
[-2.42804605]
[ 3.51667531]
[ 3.70589715]]
上一篇: cmake
下一篇: c11右值与移动语义