Pass coefficients to model in scikit learn
See original GitHub issueDescription
I would like to train a network on a dataset and then continue training on further data, i.e., to adjust the weights of my model with the help of some data and then use these weights for further training on more data.
I thought that this would be possible with the warm_start parameter in a network model.
Steps/Code to Reproduce
from sklearn.datasets import load_boston
from sklearn import linear_model
X, y = load_boston(return_X_y=True)
X_1, X_2, y_1, y_2 = X[:len(X)/2], X[len(X)/2:], y[:len(y)/2], y[len(y)/2:]
model_1 = linear_model.ElasticNet(alpha = 0.01, warm_start=True)
in_weights = model_1.coef_
print 'Initialization : \nin_weights', in_weights
model_1.fit(X_1, y_1)
weights_1 = model_1.coef_
print '\nFirst fit:\n in_weights', in_weights, '\n \nweights_1 \n', weights_1
model_1.fit(X_2, y_2)
weights_2 = model_1.coef_
print '\n Second Fit: \nin_weights \n', in_weights, '\n \nweights_1 \n', weights_1, '\n \nweights_2 \n', weights_2
Expected Results
I expected the warm start parameter to set the weights to zero with every initialization and in case of several calls to fit use the weights of the previous fit call as starting point for the next fit if the warm_start parameter is set to True.
Actual Results
However, comparing the two options (warm_start = True/False
) in practice (try in code above) yields identical results for weights and consequently for predictions. The only difference seems to be, that weights are updated retrospectively if assigned to variables in between if warm_start = True
.
The following code illustrates this with a simple scikit-learn dataset and the ElasticNet as model. weights_1 changes values after the second fit such that weights_1 == weights_2
for warm_start = True
(as described above), whereas weights_1
keeps the values of the first fit in case of warm_start = False
(set warm_start to False in a second run for comparison).
So apparently the warm_start option is not the right approach to solve my problem. What possibilities are there then to pass coefficients, learned from a first fit to a model and continue the training with the passed coefficients as starting point?
The models I want to use this with are the MLPRegressor and the ElastcNet.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
I feel that your assumptions on “warm_start” are wrong: warm_start only says that the optimization will start from the previous coefficients, but it does not say that it will end in a position controlled by these previous coefficients. Indeed, the model is fully fit on the data given, and is not influenced by the previous data.
If you want the model to be influenced by the previous data, you are looking for settings that are closer to something like on-line learning, and hence the partial_fit method of objects that support on-line learning.
AFAIK, ElasticNet is only randomised if selection=‘random’. This could be better documented, perhaps.
On 2 July 2017 at 01:21, MarenMa notifications@github.com wrote: