簡體   English   中英

某些數據集的線性回歸不收斂

[英]Linear regression not converging for some datasets

我在 Python 中創建了多元線性回歸的簡單實現,它按預期工作。 但是對於一些特定的數據集,成本 function 無論我使用多少次迭代或任何學習率都不會減少。

例如-對於

X = [[6.2],[6.5],[5.48],[6.54],[7.18],[7.93]](單自變量)

Y = [26.3, 26.65, 25.03, 26.01, 27.9, 30.47]

即使在學習率為 0.04 的 100000 次迭代之后,成本 function 也不會低於 0.24。

我的實現有什么問題嗎?

import numpy as np

'''
Linear Regression algorithm:-
feature_count - No. of independent variables.

Attributes:-
learning_rate - Set or change the learning rate of the algorithm.

Methods-
1- predict - Predict values based on provided features X.
2- cost - Find cost corresponding to features X and outputs Y.
3- train - Train the algorithm using features X and outputs Y.

Arguments of Methods-
X - Features. This should always be a 2D array, even if there is only one row.
Y - Outputs. This should always be a 1D array.
'''
class LinearRegressor:
    def __init__(self, feature_count: int, learning_rate: float):
        self.alpha = learning_rate
        self.theta = np.zeros(feature_count + 1)
        
    def predict(self, X):
        X = np.insert(X, 0, 1, axis = 1)
        return np.dot(X, self.theta)
    
    def cost(self, X, Y):
        theta = self.theta
        X = np.insert(X, 0, 1, axis = 1)
        m = X.shape[0]
        E = np.dot(X, theta) - Y
        return (1/m) * np.dot(E, E)
        
    def train(self, X, Y, iterations: int):
        dot = np.dot
        tp = np.transpose
        
        X = np.insert(X, 0, 1, axis = 1)
        alpha = self.alpha
        theta = self.theta
        m = X.shape[0]
        
        for _ in range(0, iterations):
            theta = theta - (alpha/m) * dot( tp(X), dot(X, theta)-Y )
            
        self.theta = theta
        
    @property
    def learning_rate(self):
        return self.alpha
    
    @learning_rate.setter
    def learning_rate(self, value):
        self.alpha = value
from ml_algorithms import LinearRegressor

X = [[6.2],[6.5],[5.48],[6.54],[7.18],[7.93]]
Y = [26.3, 26.65, 25.03, 26.01, 27.9, 30.47]

lr = LinearRegressor(1, 0.04)
lr.train(X, Y, 100000)
print("Theta values after training- ", lr.theta)
print("Predicting training data-    ", lr.predict(X))
print("Cost- ", lr.cost(X, Y))

輸出-

Theta values after training-  [12.54939547  2.18588067]

Predicting training data-     [26.10185564 26.75761984 24.52802155 26.84505507 28.2440187  29.8834292 ]

Cost-  0.24375945142471633

您的實施似乎很好。 在您的設置中,您只有 2 個可學習參數,您的 model 可以調整這些參數以降低損失。 您達到了一個限制,在該限制下,對參數值的任何進一步調整(考慮到計算權重更新的精度)都會增加損失。

計算的精度控制着您可以將更新應用到參數的粒度。 增加它可以讓您對權重應用更細粒度的更新。

為了測試這個想法,您可以提高可學習參數的精度。 你的新__init__ function 看起來像這樣:

def __init__(self, feature_count: int, learning_rate: float):
    self.alpha = learning_rate
    self.theta = np.zeros(feature_count + 1, dtype=np.longdouble)

運行與之前完全相同的代碼,我們得到的損失值為0.2437594514247159799 ,低於您當前的損失值。

損失的減少是無關緊要的,但我們正在接近在不改變實現本身的情況下所能達到的極限。 要進一步減少損失,請考慮更改您的實現。 也許使用具有更高學習能力的建模技術。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM