2017-09-18 210 views
0

如何在sklearn python中重新訓練我現有的機器學習模型?如何用新數據重新訓練sklearn中的邏輯迴歸模型

我有成千上萬的記錄,我用它訓練了我的模型,並使用pickle傾銷爲.pkl文件。 在第一次訓練模型時,我在創建邏輯迴歸對象時使用了warmStart = True參數。

示例代碼:

log_regression_model = linear_model.LogisticRegression(warm_start = True) 
log_regression_model.fit(X, Y) 
# Saved this model as .pkl file on filesystem like pickle.dump(model,open('model.pkl', wb)) 

我想保持這種最新的新數據,我將每天獲得。從文件系統加載它

#open the model from filesystem 
log_regression_model = pickle.load(open('model.pkl','rb')) 
log_regression_model.fit(X, Y) # New X, Y here is data of last 24 hours only. Few hundreds records only. 

但是,當我重新訓練模型: 對於我打開現有的模型文件,並獲得過去24小時內的新數據,並訓練它again./

示例代碼,它似乎刪除了現有的模型,它是用記錄創建的千個,並創建了最近24小時記錄的幾個新記錄(數千個記錄的模型在文件系統上的大小爲3MB,而新的重新訓練模型只有67KB)

我嘗試過使用warmStart選項。 如何重新訓練我的LogisticRegression模型?

+0

問題:你不能將新數據添加到原始數據並重新訓練整個數據集?作爲旁註,我會檢查以下鏈接:http://scikit-learn.org/stable/modules/scaling_strategies.html。然後我會考慮在神經網絡中經常使用的小批量策略(您需要自己實現梯度下降),但邏輯迴歸將非常容易(請查看https://udata.science/2017/08/31/蟒蛇的實現 - 的 - 邏輯迴歸 - 從劃傷/)。但是,對於這種策略,您也需要在整個數據集上進行少量傳遞...... – Umberto

+0

由於數據量巨大且具有當前資源,需要使用新舊數據再次訓練模型效率不高,因此需要超過24小時的訓練時間該模型, –

回答

0

LogicsticRegression對象的大小與使用多少個樣本來訓練它無關。

from sklearn.linear_model import LogisticRegression 
import pickle 
import sys 

np.random.seed(0) 
X, y = np.random.randn(100000, 1), np.random.randint(2, size=(100000,)) 
log_regression_model = LogisticRegression(warm_start=True) 
log_regression_model.fit(X, y) 
print(sys.getsizeof(pickle.dumps(log_regression_model))) 

np.random.seed(0) 
X, y = np.random.randn(100, 1), np.random.randint(2, size=(100,)) 
log_regression_model = LogisticRegression(warm_start=True) 
log_regression_model.fit(X, y) 
print(sys.getsizeof(pickle.dumps(log_regression_model))) 

結果

1230 
1233 

你可能會節省錯誤的模型對象。確保你保存了log_regression_model。

pickle.dump(log_regression_model, open('model.pkl', 'wb')) 

隨着模型大小等不同,而事實上,LogisticRegression的對象不與不同數量的訓練樣本的改變它們的大小,它看起來像不同的代碼被用於生成保存的模型和這個新的「再培訓「模式。

所有這一切說,它也像warm_start沒有做任何事情在這裏:

np.random.seed(0) 
X, y = np.random.randn(200, 1), np.random.randint(2, size=(200,)) 

log_regression_model = LogisticRegression(warm_start=True) 
log_regression_model.fit(X[:100], y[:100]) 
print(log_regression_model.intercept_, log_regression_model.coef_) 

log_regression_model.fit(X[100:], y[100:]) 
print(log_regression_model.intercept_, log_regression_model.coef_) 

log_regression_model = LogisticRegression(warm_start=False) 
log_regression_model.fit(X[100:], y[100:]) 
print(log_regression_model.intercept_, log_regression_model.coef_) 

log_regression_model = LogisticRegression(warm_start=False) 
log_regression_model.fit(X, y) 
print(log_regression_model.intercept_, log_regression_model.coef_) 

給出:

(array([ 0.01846266]), array([[-0.32172516]])) 
(array([ 0.17253402]), array([[ 0.33734497]])) 
(array([ 0.17253402]), array([[ 0.33734497]])) 
(array([ 0.09707612]), array([[ 0.01501025]])) 

基於this other questionwarm_start會產生一定的影響,如果你使用另一種解算器(例如LogisticRegression(warm_start=True, solver='sag')),但它仍然不會像在添加新數據的整個數據集上重新訓練一樣。例如,上述四個輸出成爲:

(array([ 0.01915884]), array([[-0.32176053]])) 
(array([ 0.17973458]), array([[ 0.33708208]])) 
(array([ 0.17968324]), array([[ 0.33707362]])) 
(array([ 0.09903978]), array([[ 0.01488605]])) 

你可以看到中間的兩條線都不同,但差別不大。它所做的就是使用最後一個模型的參數作爲用新數據重新訓練新模型的起點。這聽起來像你想要做的就是保存數據,並重新訓練它與每次添加數據時的舊數據和新數據相結合。

0

當您在訓練有素的模型上使用fit時,您基本上會丟棄所有先前的信息。

Scikit-learn有一些模型有partial_fit方法可用於增量訓練,如in documentation

我不記得是否有可能重新培訓Logistic迴歸在sklearn,但sklearn具有SGDClassifierloss=log運行Logistic迴歸與隨機梯度下降優化,具有partial_fit方法。

相關問題