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 question,warm_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]]))
你可以看到中間的兩條線都不同,但差別不大。它所做的就是使用最後一個模型的參數作爲用新數據重新訓練新模型的起點。這聽起來像你想要做的就是保存數據,並重新訓練它與每次添加數據時的舊數據和新數據相結合。
問題:你不能將新數據添加到原始數據並重新訓練整個數據集?作爲旁註,我會檢查以下鏈接:http://scikit-learn.org/stable/modules/scaling_strategies.html。然後我會考慮在神經網絡中經常使用的小批量策略(您需要自己實現梯度下降),但邏輯迴歸將非常容易(請查看https://udata.science/2017/08/31/蟒蛇的實現 - 的 - 邏輯迴歸 - 從劃傷/)。但是,對於這種策略,您也需要在整個數據集上進行少量傳遞...... – Umberto
由於數據量巨大且具有當前資源,需要使用新舊數據再次訓練模型效率不高,因此需要超過24小時的訓練時間該模型, –