代码之家  ›  专栏  ›  技术社区  ›  Harsh kumar

神经网络中的线性函数产生较大的输出值

  •  2
  • Harsh kumar  · 技术社区  · 7 年前

    我正在对iris数据集进行回归,以预测其类型。我使用相同的数据和相同的神经网络成功地进行了分类。对于分类,我使用tanh作为所有层中的激活函数。但对于回归,我在隐藏层使用tanh函数,在输出层使用单位函数。

    import numpy as np
    
    class BackPropagation:
        weight =[]
        output =[]
        layers =0
        eta = 0.1
    
        def __init__(self, x):
            self.layers = len(x)
            for i in range(self.layers-2):
            w = np.random.randn(x[i]+1,x[i+1]+1)
            self.weight.append(w)
            w = w = np.random.randn(x[-2]+1,x[-1])
            self.weight.append(w)
    
        def tanh(self,x):
            return np.tanh(x)
    
        def deriv_tanh(self,x):
            return 1.0-(x**2)
    
        def linear(self,x):
            return x
    
        def deriv_linear(self,x):
            return 1
    
    
        def training(self,in_data,target,epoch=100):
            bias = np.atleast_2d(np.ones(in_data.shape[0])*(-1)).T
            in_data = np.hstack((in_data,bias))
            print("Training Starts ......")
    
            while epoch!=0:
                epoch-=1
                self.output=[]
                self.output.append(in_data)
                # FORWARD PHASE
                for j in range(self.layers-2):
                    y_in = np.dot(self.output[j],self.weight[j])
                    y_out = self.tanh(y_in)
                    self.output.append(y_out)
    
                y_in = np.dot(self.output[-1],self.weight[-1])
                y_out = self.linear(y_in)
                self.output.append(y_out)
                print("Weight Is")
                for i in self.weight:
                    print(i)       
    
                # BACKWARD PHASE
                error = self.output[-1]-target
                print("ERROR IS")
                print(np.mean(0.5*error*error))
                delta=[]
                delta_o = error * self.deriv_linear(self.output[-1])
                delta.append(delta_o)
    
                for k in reversed(range(self.layers-2)):
                    delta_h = np.dot(delta[-1],self.weight[k+1].T) * self.deriv_tanh(self.output[k+1])
                    delta.append(delta_h)
                delta.reverse()
    
                # WEIGHT UPDATE
    
                for i in range(self.layers-1):
                    self.weight[i] -= (self.eta * np.dot(self.output[i].T, delta[i]))
    
            print("Training complete !")
            print("ACCURACY IS")
            acc = (1.0-(0.5*error*error))*100
            print(np.mean(acc))
    
        def recall(self,in_data):
            in_data = np.atleast_2d(in_data)
            bias = np.atleast_2d(np.ones(in_data.shape[0])*(-1)).T
            in_data = np.hstack((in_data,bias))
            y_out = in_data.copy()
            for i in range(self.layers-2):
                y_in = np.dot(y_out,self.weight[i])
                y_out = self.tanh(y_in).copy()
            y_in = np.dot(y_out,self.weight[-1])
            y_out = self.linear(y_in).copy()
            return y_out
    # MAIN
    data = np.loadtxt("iris.txt",delimiter=",")
    obj = BackPropagation([4,2,1])
    in_data = data[:rows,:cols].copy()
    target = data[:rows,cols:].copy()
    obj.training(in_data,target)
    print("ANSWER IS")
    print(obj.recall(in_data))  
    

    数据集是这样的。这里,前4列是特征,最后一列包含目标值。数据集中有150条这样的记录。

    5.1,3.5,1.4,0.2,0
    4.9,3.0,1.4,0.2,0
    5.0,3.6,1.4,0.2,0
    5.4,3.9,1.7,0.4,0
    4.6,3.4,1.4,0.3,0
    7.0,3.2,4.7,1.4,1
    6.4,3.2,4.5,1.5,1
    6.9,3.1,4.9,1.5,1
    5.5,2.3,4.0,1.3,1
    6.3,3.3,6.0,2.5,2
    5.8,2.7,5.1,1.9,2
    7.1,3.0,5.9,2.1,2
    6.3,2.9,5.6,1.8,2
    

    在每个历元之后,预测值都呈指数增长。并且,在50个时代内,代码将INF或-INF作为输出。我还尝试了不同的学习速率、隐藏层中的神经元数量、隐藏层数量、初始权重值、迭代次数等。 那么,如何使用误差反向传播的神经网络进行回归呢?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Bastian    7 年前

    使用均方误差函数进行回归任务。对于分类任务,通常使用softmax层作为输出,并优化交叉输入成本函数。