【发布时间】:2019-12-19 04:13:34
【问题描述】:
我正在尝试从头开始为单层感知器编写代码,即不使用除 numpy 之外的任何库。我使用的激活函数是sigmoid。
我有一个大小为 50、2 个属性的训练集,为了模拟多个类,我有两个输出节点而不是 1 ,所以我的输入矩阵的形式为 1x2 ,权重矩阵的形式为 2x2输出矩阵为 1x2。
我已使用正常梯度下降进行优化。我训练了连接到第一个输出的两个权重,另外两个独立地连接到第二个输出的权重,即,因为第一个输出的权重是根据我的训练输出的第一个元素训练的,第二个输出的权重也是如此输出,附加到它的权重是根据我的训练输出的第二个元素进行训练的。
训练集是从正态分布随机生成的,第一个属性来自具有均值 100 和 S.D 75 的正态分布,而第二个输出来自正态分布。平均为 400 和 S.D. 75. 类是交替分配的,即第一个训练样例属于 1 类,下一个属于 0 类,下一个属于 1 类,依此类推。
我的问题是,对于我在随机生成的训练集上重复提供的某些输入,有时输入被分为两个类。从数学的角度来看,这可能吗?可能是因为我选择的训练集是随机的吗?我认为是这种情况,但我想确定在数学上它确实是可能的,并且这不是我的代码或逻辑中的错误。
我的代码是:
import numpy as np
def sigmoid(x):
#"Numerically-stable sigmoid function."
if x >= 0:
z = np.exp(-x)
return 1 / (1 + z)
else:
z = np.exp(x)
return z / (1 + z)
print("Number of input nodes is 2\n")
print("Number of output nodes is 2\n")
print("Stochastic Gradient descent used to avoid summing up and iterating till minima\n")
print("Activation function : Sigmoid\n")
#print("There are two attri
T_entries=[]
A_entries=[]
#design the input matrix X , the weight matrix W
Rx=1
Cx=2 #Input vector is of size 1x2 , Weight is of size 2x2
W=np.array([[1.0,1.0],[1.0,1.0]]) #assigning initial value to weights
B=np.array([0.0,0.0]) #Assigning initial values to biases
#Training set
Rt=50
Ct=2 #10 training set. Classify between male and female . Attributes : Height>5'5" = 1 else 0 ; Weight > 60 =1 else 0
#print("Enter training set\n")
#print("Enter the attribute values in space seperate entries\n")
#Attributes
#T_entries=list(map(float, input().split()))
i=0
while i<50:
pick=np.random.normal(100, 75,1)
T_entries.append(pick)
pick=np.random.normal(400, 75,1)
T_entries.append(pick)
i=i+1
T= np.array(T_entries).reshape(Rt, Ct)
#Annotation
#print("Enter annotations for training set\n")
Ra=50
Ca=2 #Annotations : 0 for male , 1 for female
#A_entries=list(map(float,input().split()))
i=0
pick=0
while i<50:
pick=1-pick
A_entries.append(pick)
A_entries.append(1-pick)
i=i+1
A=np.array(A_entries).reshape(Ra,Ca)
print("The random training set generated is\n")
i=0
while i < Ra:
print(str(T[i][0])+" "+str(T[i][1])+" "+str(A[i][0])+" "+str(A[i][1])+"\n")
i=i+1
print("Training....")
l_rate=0.05 #learning rate
i=0
while i<50:
print("****************************************************************************************\n")
print("Iteration: "+str(i)+"\n")
x1=T[i][0]
x2=T[i][1]
y1=A[i][0]
y2=A[i][1]
print("The training example is: " +str(x1)+ " " +str(x2) +"\n")
print("Corresponding annotations are : " +str(y1) +" " +str(y2) +"\n")
T_i=np.array([x1,x2]) #Getting the attributes
A_i=np.array([y1,y2]) #Corresponding annotation
f_x=np.matmul(T_i,W)+B #Compute the output to calculate the error
print("Ultimate output for the training exmaple is :\n")
print(f_x)
calc=sigmoid(f_x[0])
#print(calc)
real=A_i[0]
#print(real)
print("Calculated value of y1 is " +str(calc) +" while the real value is " +str(real) +"\n")
# w1 and w2 are responsible for out_1 and w3 and w4 for out_2
err_out1=calc-real
print("Error in y1 is "+str(err_out1)+"\n")
W[0][0]=W[0][0]- l_rate*(2*err_out1*T_i[0]) #w1 modified 2*error*x1
W[1][0]=W[1][0]- l_rate*(2*err_out1*T_i[1]) #w2 modified 2*error*x2
B[0]=B[0]- l_rate*(2*err_out1) #b1 modified
print("The weight matrix is \n")
#print(W)
calc=sigmoid(f_x[1])
#print(calc)
real=A_i[1]
#print(real)
print("Calculated value of y2 is " +str(calc) +" while the real value is " +str(real) +"\n")
err_out2=calc-real
print("Error in y2 is "+str(err_out2)+"\n")
W[0][1]=W[0][1]-l_rate*(2*err_out2*T_i[0])
W[1][1]=W[1][1]-l_rate*(2*err_out2*T_i[1])
B[1]=B[1]-l_rate*(2*err_out2)
print("The weight matrix is \n")
print(W)
i=i+1
#Testing our model
#print (W)
print("Enter x1 and x2")
Test_entries=list(map(float, input().split()))
Test=np.array(Test_entries).reshape(Rx,Cx)
print("Entered Test data is\n")
print(Test)
f=np.matmul(Test,W)+B
print(sigmoid(f[0][0]))
if sigmoid(f[0][0]) > 0.5 :
print ("Class 1")
else:
print("Not Class 1")
print("\n")
print(sigmoid(f[0][1]))
if sigmoid(f[0][1]) > 0.5:
print("Class 2")
else:
print("Not Class 2")
此测试示例的输出:
Entered Test data is
[[223. 85.]]
1.0
Class 1
1.0
Class 2`
【问题讨论】:
标签: python-3.x neural-network perceptron