0
votes

I try to pass a Keras model (as a function) to KerasClassifier wrapper from scikit_learn, and then use GridSearchCV to create some settings, and finally fit the train and test datasets (both are numpy array)

I then, with the same python script, got different exceptions, some of them are:

_1.

Traceback (most recent call last): File "mnist_flat_imac.py", line 63, in grid_result = validator.fit(train_images, train_labels) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/site-packages/sklearn/model_selection/_search.py", line 626, in fit base_estimator = clone(self.estimator) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/site-packages/sklearn/base.py", line 62, in clone new_object_params[name] = clone(param, safe=False) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/site-packages/sklearn/base.py", line 53, in clone

snipped here

in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/copy.py", line 174, in deepcopy rv = reductor(4) TypeError: can't pickle SwigPyObject objects Exception ignored in: > Traceback (most recent call last): File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/site-packages/tensorflow/python/framework/c_api_util.py", line 52, in __del__ c_api.TF_DeleteGraph(self.graph) AttributeError: 'ScopedTFGraph' object has no attribute 'graph'

_2.

Traceback (most recent call last): File "mnist_flat_imac.py", line 63, in grid_result = validator.fit(train_images, train_labels) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/site-packages/sklearn/model_selection/_search.py", line 626, in fit base_estimator = clone(self.estimator) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/site-packages/sklearn/base.py", line 62, in clone new_object_params[name] = clone(param, safe=False) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/site-packages/sklearn/base.py", line 53, in clone return copy.deepcopy(estimator) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/copy.py", line 182, in deepcopy y = _reconstruct(x, rv, 1, memo) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/copy.py", line 297, in _reconstruct

snipped here

in deepcopy y = _reconstruct(x, rv, 1, memo) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/copy.py", line 297, in _reconstruct state = deepcopy(state, memo) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/copy.py", line 155, in deepcopy y = copier(x, memo) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/copy.py", line 243, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/copy.py", line 174, in deepcopy rv = reductor(4) TypeError: can't pickle SwigPyObject objects

_3.

Traceback (most recent call last): File "mnist_flat_imac.py", line 63, in grid_result = validator.fit(train_images, train_labels) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/site-packages/sklearn/model_selection/_search.py", line 626, in fit base_estimator = clone(self.estimator) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/site-packages/sklearn/base.py", line 62, in clone new_object_params[name] = clone(param, safe=False) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/site-packages/sklearn/base.py", line 53, in clone

snipped here

in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/copy.py", line 182, in deepcopy y = _reconstruct(x, rv, 1, memo) File "/home/longnv/PYTHON_ENV/DataScience/lib/python3.5/copy.py", line 306, in _reconstruct y.dict.update(state) AttributeError: 'NoneType' object has no attribute 'update'

Why did it output different errors with the same python script? And how can I fix this?

Thank you so much!

P.S.

  • python: 3.5
  • tensorflow: 1.10.1
  • pandas: 0.23.4
  • Ubuntu: 4.4.0-124-generic
1

1 Answers

0
votes

Found it.

It should be: clf = KerasClassifier(build_fn=get_model)

Instead of: clf = KerasClassifier(build_fn=get_model())