question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KerasClassifier "can't pickle _thread.RLock objects" message when predicting

See original GitHub issue

I’m able to fit a model that includes KerasClassifier as a model in the ensemble. However, at prediction time I get the following error. I’ve tried changing the backend as well as the number of jobs but to no avail. Any ideas?

`--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-9-74578adea9e4> in <module>() ----> 1 preds = ensemble.predict_proba(X[294:])

D:\Continuum\anaconda3\lib\site-packages\mlens\ensemble\base.py in predict_proba(self, X, **kwargs) 633 “”" 634 kwargs.pop(‘proba’, None) –> 635 return self.predict(X, proba=True, **kwargs) 636 637 def _build_layer(self, estimators, indexer, preprocessing, **kwargs):

D:\Continuum\anaconda3\lib\site-packages\mlens\ensemble\base.py in predict(self, X, **kwargs) 613 return 614 X, _ = check_inputs(X, check_level=self.array_check) –> 615 return self._backend.predict(X, **kwargs) 616 617 def predict_proba(self, X, **kwargs):

D:\Continuum\anaconda3\lib\site-packages\mlens\ensemble\base.py in predict(self, X, **kwargs) 199 predictions from final layer. 200 “”" –> 201 if not self.fitted: 202 NotFittedError(“Instance not fitted.”) 203

D:\Continuum\anaconda3\lib\site-packages\mlens\parallel\base.py in fitted(self) 359 if not self.stack or not self._check_static_params(): 360 return False –> 361 return all([g.fitted for g in self.stack]) 362 363 @property

D:\Continuum\anaconda3\lib\site-packages\mlens\parallel\base.py in <listcomp>(.0) 359 if not self.stack or not self._check_static_params(): 360 return False –> 361 return all([g.fitted for g in self.stack]) 362 363 @property

D:\Continuum\anaconda3\lib\site-packages\mlens\parallel\base.py in fitted(self) 359 if not self.stack or not self._check_static_params(): 360 return False –> 361 return all([g.fitted for g in self.stack]) 362 363 @property

D:\Continuum\anaconda3\lib\site-packages\mlens\parallel\base.py in <listcomp>(.0) 359 if not self.stack or not self._check_static_params(): 360 return False –> 361 return all([g.fitted for g in self.stack]) 362 363 @property

D:\Continuum\anaconda3\lib\site-packages\mlens\parallel\handles.py in fitted(self) 256 if not self._check_static_params(): 257 return False –> 258 return all([o.fitted for o in self.learners + self.transformers]) 259 260 def get_params(self, deep=True):

D:\Continuum\anaconda3\lib\site-packages\mlens\parallel\handles.py in <listcomp>(.0) 256 if not self._check_static_params(): 257 return False –> 258 return all([o.fitted for o in self.learners + self.transformers]) 259 260 def get_params(self, deep=True):

D:\Continuum\anaconda3\lib\site-packages\mlens\parallel\learner.py in fitted(self) 743 # Check estimator param overlap 744 fitted = self.learner + self.sublearners –> 745 fitted_params = fitted[0].estimator.get_params(deep=True) 746 model_estimator_params = self.estimator.get_params(deep=True) 747

D:\Continuum\anaconda3\lib\site-packages\mlens\parallel\learner.py in estimator(self) 64 def estimator(self): 65 “”“Deep copy of estimator”“” —> 66 return deepcopy(self._estimator) 67 68 @estimator.setter

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 178 y = x 179 else: –> 180 y = _reconstruct(x, memo, *rv) 181 182 # If is its own copy, don’t memoize.

D:\Continuum\anaconda3\lib\copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy) 278 if state is not None: 279 if deep: –> 280 state = deepcopy(state, memo) 281 if hasattr(y, ‘setstate’): 282 y.setstate(state)

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 148 copier = _deepcopy_dispatch.get(cls) 149 if copier: –> 150 y = copier(x, memo) 151 else: 152 try:

D:\Continuum\anaconda3\lib\copy.py in _deepcopy_dict(x, memo, deepcopy) 238 memo[id(x)] = y 239 for key, value in x.items(): –> 240 y[deepcopy(key, memo)] = deepcopy(value, memo) 241 return y 242 d[dict] = _deepcopy_dict

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 178 y = x 179 else: –> 180 y = _reconstruct(x, memo, *rv) 181 182 # If is its own copy, don’t memoize.

D:\Continuum\anaconda3\lib\copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy) 278 if state is not None: 279 if deep: –> 280 state = deepcopy(state, memo) 281 if hasattr(y, ‘setstate’): 282 y.setstate(state)

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 148 copier = _deepcopy_dispatch.get(cls) 149 if copier: –> 150 y = copier(x, memo) 151 else: 152 try:

D:\Continuum\anaconda3\lib\copy.py in _deepcopy_dict(x, memo, deepcopy) 238 memo[id(x)] = y 239 for key, value in x.items(): –> 240 y[deepcopy(key, memo)] = deepcopy(value, memo) 241 return y 242 d[dict] = _deepcopy_dict

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 178 y = x 179 else: –> 180 y = _reconstruct(x, memo, *rv) 181 182 # If is its own copy, don’t memoize.

D:\Continuum\anaconda3\lib\copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy) 278 if state is not None: 279 if deep: –> 280 state = deepcopy(state, memo) 281 if hasattr(y, ‘setstate’): 282 y.setstate(state)

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 148 copier = _deepcopy_dispatch.get(cls) 149 if copier: –> 150 y = copier(x, memo) 151 else: 152 try:

D:\Continuum\anaconda3\lib\copy.py in _deepcopy_dict(x, memo, deepcopy) 238 memo[id(x)] = y 239 for key, value in x.items(): –> 240 y[deepcopy(key, memo)] = deepcopy(value, memo) 241 return y 242 d[dict] = _deepcopy_dict

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 148 copier = _deepcopy_dispatch.get(cls) 149 if copier: –> 150 y = copier(x, memo) 151 else: 152 try:

D:\Continuum\anaconda3\lib\copy.py in _deepcopy_list(x, memo, deepcopy) 213 append = y.append 214 for a in x: –> 215 append(deepcopy(a, memo)) 216 return y 217 d[list] = _deepcopy_list

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 178 y = x 179 else: –> 180 y = _reconstruct(x, memo, *rv) 181 182 # If is its own copy, don’t memoize.

D:\Continuum\anaconda3\lib\copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy) 278 if state is not None: 279 if deep: –> 280 state = deepcopy(state, memo) 281 if hasattr(y, ‘setstate’): 282 y.setstate(state)

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 148 copier = _deepcopy_dispatch.get(cls) 149 if copier: –> 150 y = copier(x, memo) 151 else: 152 try:

D:\Continuum\anaconda3\lib\copy.py in _deepcopy_dict(x, memo, deepcopy) 238 memo[id(x)] = y 239 for key, value in x.items(): –> 240 y[deepcopy(key, memo)] = deepcopy(value, memo) 241 return y 242 d[dict] = _deepcopy_dict

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 178 y = x 179 else: –> 180 y = _reconstruct(x, memo, *rv) 181 182 # If is its own copy, don’t memoize.

D:\Continuum\anaconda3\lib\copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy) 278 if state is not None: 279 if deep: –> 280 state = deepcopy(state, memo) 281 if hasattr(y, ‘setstate’): 282 y.setstate(state)

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 148 copier = _deepcopy_dispatch.get(cls) 149 if copier: –> 150 y = copier(x, memo) 151 else: 152 try:

D:\Continuum\anaconda3\lib\copy.py in _deepcopy_dict(x, memo, deepcopy) 238 memo[id(x)] = y 239 for key, value in x.items(): –> 240 y[deepcopy(key, memo)] = deepcopy(value, memo) 241 return y 242 d[dict] = _deepcopy_dict

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 178 y = x 179 else: –> 180 y = _reconstruct(x, memo, *rv) 181 182 # If is its own copy, don’t memoize.

D:\Continuum\anaconda3\lib\copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy) 278 if state is not None: 279 if deep: –> 280 state = deepcopy(state, memo) 281 if hasattr(y, ‘setstate’): 282 y.setstate(state)

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 148 copier = _deepcopy_dispatch.get(cls) 149 if copier: –> 150 y = copier(x, memo) 151 else: 152 try:

D:\Continuum\anaconda3\lib\copy.py in _deepcopy_dict(x, memo, deepcopy) 238 memo[id(x)] = y 239 for key, value in x.items(): –> 240 y[deepcopy(key, memo)] = deepcopy(value, memo) 241 return y 242 d[dict] = _deepcopy_dict

D:\Continuum\anaconda3\lib\copy.py in deepcopy(x, memo, _nil) 167 reductor = getattr(x, “reduce_ex”, None) 168 if reductor: –> 169 rv = reductor(4) 170 else: 171 reductor = getattr(x, “reduce”, None)

TypeError: can’t pickle _thread.RLock objects`

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
onacramecommented, Aug 10, 2018

Just one thing to note. It’s doesn’t really impact me as I’m using a small dataset but when I use the KerasClassifier in the ensemble I can only get it to work with setting n_jobs=1. If I set to -1 it won’t work.

0reactions
onacramecommented, Aug 14, 2018

Gotcha, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keras: TypeError: can't pickle _thread.lock objects with ...
This means you implement a class that inherits from either KerasClassifier or KerasRegressor . The __call__ method of the present class will ...
Read more >
Keras 2, TypeError: can't pickle _thread.lock objects
I am using Keras to create an ANN and do a grid search on the network. I have encountered the following error while...
Read more >
could not serialize object: typeerror: cannot pickle '_thread. ...
In your case, I suspect the error is perhaps caused by variable "lock". Open side panel. Pyspark: PicklingError: Could not serialize object:.
Read more >
How to Grid Search Hyperparameters for Deep Learning ...
By default, the grid search will only use one thread. ... Personally, I guess “cannot pickle object class” means the neuron network cannot...
Read more >
Pandas : TypeError: can't pickle _thread._local objects when ...
Pandas : TypeError: can't pickle _thread._local objects when using dask on pandas DataFrame [ Beautify Your Computer ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found