【问题标题】:Can one test example belong to more than one class?一个测试示例可以属于多个类吗?
【发布时间】: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


    【解决方案1】:

    我认为 - 由于训练数据是随机生成的,因此将使用模型在该实例中拥有的数据(当前)执行训练,因此相同的输入可能在不同的运行过程中被分类到任一类中。

    (希望我很清楚,没有让你更困惑!:) 如果我误解了你的问题,请纠正我)

    【讨论】:

    • 对。但我得到的是相同的输入分类在两个类中,在同一次运行中。
    • 哦,好的,您可以尝试重新运行您的代码吗?因为我得到了不同的输出[[223. 85.]] 0.0 Not Class 1 # 1.0 Class 2
    • 是的,这种情况并不总是发生。我尝试使用相同的输入大约 5 次;我没想到它会同时被分类到两个类中,但是我想知道由于随机输出,相同的输入是否可以在不同的运行中被分类到两个类中。事实证明情况更糟。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 2012-09-30
    相关资源
    最近更新 更多