# GeneralizingEstimator with incremental learning /	.partial\_fit

**URL:** <https://mne.discourse.group/t/generalizingestimator-with-incremental-learning-partial-fit/2281>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [August 5, 2020, 4:38pm UTC](https://mne.discourse.group/t/generalizingestimator-with-incremental-learning-partial-fit/2281 "2020-08-05T16:38:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [August 5, 2020, 4:38pm UTC](https://mne.discourse.group/t/generalizingestimator-with-incremental-learning-partial-fit/2281/1 "2020-08-05T16:38:35Z")

</div>

External Email - Use Caution

Hi!

I would need to try decoding with incremental learning (EEG data).  
I was planning to use logistic regression by means of the SGDClassifier  
\<[https://scikit-learn.org/stable/modules/generated/sklearn.linear\_model.SGDClassifier.html](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html)\>  
.  
I would then need to call .partial\_fit to make my estimator learn on each  
of my training sets.  
However:

'GeneralizingEstimator' object has no attribute 'partial\_fit'

Same issue for SlidingEstimator.  
Is there a way to work around this limitation?

Thank you so so much in advance!

Giulia Gennari  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20200805/2ceed796/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20200805/2ceed796/attachment.html)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [August 5, 2020, 5:17pm UTC](https://mne.discourse.group/t/generalizingestimator-with-incremental-learning-partial-fit/2281/2 "2020-08-05T17:17:24Z")

</div>

External Email - Use Caution

Hi Giulia,

I think you should be able to change the method:

model = sklearn.linear\_model.SGDClassifier()  
model.fit = model.partial\_fit  
slider = mne.decoding.SlidingEstimator(model)  
for X, y in train\_batches:  
&nbsp;&nbsp;&nbsp;&nbsp;slider.fit(X, y)

Best

JR

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [August 6, 2020, 1:54pm UTC](https://mne.discourse.group/t/generalizingestimator-with-incremental-learning-partial-fit/2281/3 "2020-08-06T13:54:37Z")

</div>

External Email - Use Caution

Dear Jean-R?mi,

Thank you for the nice suggestion!

Just to make sure that this is working (I apologize for my ignorance):

When I run:  
model = SGDClassifier(loss='log', class\_weight='balanced')  
model.fit = model.partial\_fit  
slider1 = SlidingEstimator(model, scoring='roc\_auc')  
slider1.fit(X\_train, y\_train)

or

clf = make\_pipeline(Vectorizer(), StandardScaler(), model)  
slider2 = SlidingEstimator(clf, scoring='roc\_auc')  
slider2.fit(X\_train, y\_train)

I do not get any error, while I would expect:

ValueError: class\_weight 'balanced' is not supported for partial\_fit.  
In order to use 'balanced' weights, use  
compute\_class\_weight('balanced', classes, y). Pass the resulting  
weights as the class\_weight parameter.

Since this is what I get with:  
model.fit(X\_train[:,:,single\_time\_point], y\_train)

Is there a good reason for that? E.g. class weights are computed internally  
beforehand by SlidingEstimator?

Thank you again!

Giulia

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [August 6, 2020, 2:11pm UTC](https://mne.discourse.group/t/generalizingestimator-with-incremental-learning-partial-fit/2281/4 "2020-08-06T14:11:26Z")

</div>

External Email - Use Caution

Hi Giula,

good catch, I had forgotten that we're cloning the estimator for each time  
sample; you'll thus need to do this:

class MyModel(SGDClassifier):  
&nbsp;&nbsp;&nbsp;&nbsp;def fit(self, X, y):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super().partial\_fit(X, y)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self

model = MyModel(loss='log', class\_weight='balanced')  
slider = SlidingEstimator(model, scoring='roc\_auc')

Hope that helps

JR

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [August 7, 2020, 12:32pm UTC](https://mne.discourse.group/t/generalizingestimator-with-incremental-learning-partial-fit/2281/5 "2020-08-07T12:32:38Z")

</div>

External Email - Use Caution

Dear Jean-R?mi and dear Alex,

\*Thank you!\*

A solution based on this:  
class MyModel(SGDClassifier):  
&nbsp;&nbsp;&nbsp;&nbsp;def fit(self, X, y):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super().partial\_fit(X, y)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self

..works fine!  
Except for the crucial fact that parallel processing (n\_jobs\>1) seems not  
feasible.  
This is what I get when I try to score the slider (apologies for the  
ugliness, I copy-paste everything since it might be meaningful to catch  
what is wrong):

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [August 7, 2020, 12:55pm UTC](https://mne.discourse.group/t/generalizingestimator-with-incremental-learning-partial-fit/2281/6 "2020-08-07T12:55:59Z")

</div>

External Email - Use Caution

please create an issue on github and include a script to replicate the  
crash by just  
copy pasting. I'll then have a look.

Alex  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20200807/5c00fcb6/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20200807/5c00fcb6/attachment.html)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [August 7, 2020, 12:57pm UTC](https://mne.discourse.group/t/generalizingestimator-with-incremental-learning-partial-fit/2281/7 "2020-08-07T12:57:03Z")

</div>

External Email - Use Caution

Hi Giula,

In the long run, for batch optimization of parallel tasks (here each slided  
time sample), I would encourage you to have a look at pytorch; sklearn is  
not really optimal for this because it can't make use of gpu.

In the meantime, here is a solution to your problem: simply put your new  
class in a separate script e.g.

# in mymodel.py  
import numpy as np  
from sklearn.linear\_model import SGDClassifier

class MyModel(SGDClassifier):  
&nbsp;&nbsp;&nbsp;&nbsp;def fit(self, X, y):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if not hasattr(self, 'classes\_'):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.classes\_ = np.unique(y)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super().partial\_fit(X, y, self.classes\_)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self

# main script  
import numpy as np  
from mne.decoding import SlidingEstimator  
from mymodel import MyModel  
model = MyModel()  
slider = SlidingEstimator(model, scoring='roc\_auc', n\_jobs=2)

X = np.random.randn(100, 10, 3)  
y = np.random.randint(0, 2, 100)  
slider.fit(X, y)  
slider.score(X, y)

hope that helps

JR

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [August 20, 2020, 11:19am UTC](https://mne.discourse.group/t/generalizingestimator-with-incremental-learning-partial-fit/2281/8 "2020-08-20T11:19:34Z")

</div>

External Email - Use Caution

Dear Jean-R?mi,

Thank you for the suggestion and, above all, thank you so much for your  
help and assistance!  
My scripts have been working just fine while I would have never been able  
to implement my current analysis without your prompts.

All the best,

Giulia
