线性回归成本增加

14
我为了训练目的,在Python中实现了一个线性回归。问题是成本不断上升而不是下降。我使用的数据是Airfoil Self-Noise数据集。数据可以在此处找到:这里 我按照以下方式导入数据:
import pandas as pd

def features():

    features = pd.read_csv("data/airfoil_self_noise/airfoil_self_noise.dat.txt", sep="\t", header=None)

    X = features.iloc[:, 0:5]
    Y = features.iloc[:, 5]

    return X.values, Y.values.reshape(Y.shape[0], 1)

我对线性回归的代码如下:

import numpy as np
import random

class linearRegression():

    def __init__(self, learning_rate=0.01, max_iter=20):
        """
        Initialize the hyperparameters of the linear regression.

        :param learning_rate: the learning rate
        :param max_iter: the max numer of iteration to perform
        """

        self.lr = learning_rate
        self.max_iter = max_iter
        self.m = None
        self.weights = None
        self.bias = None

    def fit(self, X, Y):
        """
        Run gradient descent algorithm

        :param X: the inputs
        :param Y: the outputs
        :return:
        """

        self.m = X.shape[0]
        self.weights = np.random.normal(0, 0.1, (X.shape[1], 1))
        self.bias = random.normalvariate(0, 0.1)

        for iter in range(0, self.max_iter):

            A = self.__forward(X)
            dw, db = self.__backward(A, X, Y)

            J = (1/(2 * self.m)) * np.sum(np.power((A - Y), 2))

            print("at iteration %s cost is %s" % (iter, J))

            self.weights = self.weights - self.lr * dw
            self.bias = self.bias - self.lr * db

    def predict(self, X):
        """
        Make prediction on the inputs

        :param X: the inputs
        :return:
        """

        Y_pred = self.__forward(X)

        return Y_pred

    def __forward(self, X):
        """
        Compute the linear function on the inputs

        :param X: the inputs
        :return:
            A: the activation
        """

        A = np.dot(X, self.weights) + self.bias

        return A

    def __backward(self, A, X, Y):
        """

        :param A: the activation
        :param X: the inputs
        :param Y: the outputs
        :return:
            dw: the gradient for the weights
            db: the gradient for the bias
        """

        dw = (1 / self.m) * np.dot(X.T, (A - Y))
        db = (1 / self.m) * np.sum(A - Y)

        return dw, db

然后我按照以下方式实例化linearRegression类:

X, Y = features()
model = linearRegression()
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42)
model.fit(X_train, y_train)

我试图找出成本增加的原因,但到目前为止还没有找到原因。如果有人能指点我正确的方向,我将不胜感激。


3
请发布创建类实例和调用函数的完整代码,以便他人能够重现错误或问题。 - Sheldore
2
我编辑了我的帖子。 - Vetouz
当您在数据上使用其他程序包时,您会得到什么样的结果?手动执行几次迭代后,您会得到什么结果? - Mohammad Athar
很可能是你的数据。当我将features()中的行更改为X = features.iloc[:, 1:2](而不是使用前四列)时,您的成本开始下降。即使我使用sklearn,也无法获得比原始数据更好的分数。尝试构建一个人工数据集,您知道它会与线性回归很好地配合-看看您能得到什么样的结果。 - Mohammad Athar
3个回答

6

通常,如果您选择了大的学习率,您可能会遇到类似的问题。我已经试图检查您的代码,并且我的观察结果如下:

  • 您的代价函数J看起来没问题。
  • 但是,在反向传播函数中,您似乎是将实际结果从猜测中减去。这样做可能会得到负权值,由于您正在减去学习率和速率的乘积,因此您最终会得到增加的代价函数结果。

截至第二点 - 这可能会导致振荡问题或者大动量问题(由于学习率过大)?看起来问题的性质就像你所提到的。但是,如果我们只是改变学习率,就能解决你提到的问题吗?或者只是将成本函数改为MAE也可以吗? - undefined

3

你的学习速率过高。当我运行与你代码相同但学习速率为1e-7而不是0.01时,成本会可靠地下降。


0
一般情况下,当成本增加时,学习率过高。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接