Skip to main content
 首页 » 编程设计

python之神经网络中的线性函数正在产生很大的输出值

2024年10月01日6xxx_UU

我正在对 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 作为输出。除了恒等函数,我还尝试了 leaky ReLU,但输出仍然是 INF。我还尝试了不同的学习率、隐藏层中的神经元数量、隐藏层数量、初始权重值、迭代次数等。 那么,我如何使用具有反向传播误差的神经网络执行回归?

请您参考如下方法:

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