0
$\begingroup$

I try to build a NN classifier on the well-known MNIST image database with Sklearn's Grid Search according the following:

model = KerasClassifier(build_fn=create_model, verbose=1)
param_grid = dict(batch_size=[10, 50, 100, 250], nb_epoch=[10, 50, 100])
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X_train, y_train)

create_model is a function that builds the Neural Network Model. The fitting (last row) gives a long error message:

JoblibTypeError                           Traceback (most recent call last)
<ipython-input-109-bcb5d85cabad> in <module>()
      2 param_grid = dict(optimizer=['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam'])
      3 grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=10)
----> 4 grid_result = grid.fit(X_train, Y_train)

/home/buda/anaconda2/lib/python2.7/site-packages/sklearn/grid_search.pyc in fit(self, X, y)
    802 
    803         """
--> 804         return self._fit(X, y, ParameterGrid(self.param_grid))
    805 
    806 

/home/buda/anaconda2/lib/python2.7/site-packages/sklearn/grid_search.pyc in _fit(self, X, y, parameter_iterable)
    551                                     self.fit_params, return_parameters=True,
    552                                     error_score=self.error_score)
--> 553                 for parameters in parameter_iterable
    554                 for train, test in cv)
    555 

/home/buda/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in __call__(self, iterable)
    808                 # consumption.
    809                 self._iterating = False
--> 810             self.retrieve()
    811             # Make sure that we get a last message telling us we are done
    812             elapsed_time = time.time() - self._start_time

/home/buda/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in retrieve(self)
    755                     # a working pool as they expect.
    756                     self._initialize_pool()
--> 757                 raise exception
    758 
    759     def __call__(self, iterable):

[very many rows here]

...........................................................................
    /home/buda/anaconda2/lib/python2.7/site-packages/keras/wrappers/scikit_learn.py in fit(self=<keras.wrappers.scikit_learn.KerasClassifier object>, X=memmap([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ....,  0.,  0., ...,  0.,  0.,  0.]], dtype=float32), y=memmap([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ....,  0.,  0., ...,  0.,  1.,  0.]], dtype=float32), **kwargs={})
        132             self.model = self.__call__(**self.filter_sk_params(self.__call__))
        133         elif not isinstance(self.build_fn, types.FunctionType):
        134             self.model = self.build_fn(
        135                 **self.filter_sk_params(self.build_fn.__call__))
        136         else:
    --> 137             self.model = self.build_fn(**self.filter_sk_params(self.build_fn))
            self.model = undefined
            self.build_fn = <function create_model>
            self.filter_sk_params = <bound method KerasClassifier.filter_sk_params o...as.wrappers.scikit_learn.KerasClassifier object>>
        138 
        139         loss_name = self.model.loss
        140         if hasattr(loss_name, '__name__'):
        141             loss_name = loss_name.__name__

    ...........................................................................
    /home/buda/Projects/Kaggle/Nerve/<ipython-input-62-14daa2be96d1> in create_model(optimizer='SGD')
          1 
          2 np.random.seed(100)
          3 
          4 def create_model(optimizer="SGD"):
    ----> 5     model=Sequential()
          6     model.add(Dense(input_dim=784, init="uniform", activation="relu"))
          7     model.add(Dense(output_dim=10, activation="sigmoid"))
          8 
          9     model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
         10     return model

    TypeError: __init__() takes at least 2 arguments (4 given)

What can be the error here?

The full code is the following:

(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train=X_train.reshape(X_train.shape[0], X_train.shape[1]*X_train.shape[2]).astype("float32")
X_test=X_test.reshape(X_test.shape[0], X_test.shape[1]*X_test.shape[2]).astype("float32")

Y_train=np_utils.to_categorical(y_train,10).astype("float32")
Y_test=np_utils.to_categorical(y_test,10).astype("float32")

np.random.seed(100)

def create_model(optimizer="SGD"):
    model=Sequential()
    model.add(Dense(input_dim=784, init="uniform", activation="relu"))
    model.add(Dense(output_dim=10, activation="sigmoid"))

    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    return model

model = KerasClassifier(build_fn=create_model, nb_epoch=50, batch_size=10, verbose=0)
param_grid = dict(optimizer=['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam'])
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=10)
grid_result = grid.fit(X_train, Y_train, param_grid)

With n_jobs=1 in GridSearchCV:

TypeError                                 Traceback (most recent call last)
<ipython-input-110-538ac4724f14> in <module>()
      2 param_grid = dict(optimizer=['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam'])
      3 grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=1, cv=10)
----> 4 grid_result = grid.fit(X_train, Y_train)

/home/buda/anaconda2/lib/python2.7/site-packages/sklearn/grid_search.pyc in fit(self, X, y)
    802 
    803         """
--> 804         return self._fit(X, y, ParameterGrid(self.param_grid))
    805 
    806 

/home/buda/anaconda2/lib/python2.7/site-packages/sklearn/grid_search.pyc in _fit(self, X, y, parameter_iterable)
    551                                     self.fit_params, return_parameters=True,
    552                                     error_score=self.error_score)
--> 553                 for parameters in parameter_iterable
    554                 for train, test in cv)
    555 

/home/buda/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in __call__(self, iterable)
    798             # was dispatched. In particular this covers the edge
    799             # case of Parallel used with an exhausted iterator.
--> 800             while self.dispatch_one_batch(iterator):
    801                 self._iterating = True
    802             else:

/home/buda/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in dispatch_one_batch(self, iterator)
    656                 return False
    657             else:
--> 658                 self._dispatch(tasks)
    659                 return True
    660 

/home/buda/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in _dispatch(self, batch)
    564 
    565         if self._pool is None:
--> 566             job = ImmediateComputeBatch(batch)
    567             self._jobs.append(job)
    568             self.n_dispatched_batches += 1

/home/buda/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in __init__(self, batch)
    178         # Don't delay the application, to avoid keeping the input
    179         # arguments in memory
--> 180         self.results = batch()
    181 
    182     def get(self):

/home/buda/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.pyc in __call__(self)
     70 
     71     def __call__(self):
---> 72         return [func(*args, **kwargs) for func, args, kwargs in self.items]
     73 
     74     def __len__(self):

/home/buda/anaconda2/lib/python2.7/site-packages/sklearn/cross_validation.pyc in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, error_score)
   1529             estimator.fit(X_train, **fit_params)
   1530         else:
-> 1531             estimator.fit(X_train, y_train, **fit_params)
   1532 
   1533     except Exception as e:

/home/buda/anaconda2/lib/python2.7/site-packages/keras/wrappers/scikit_learn.pyc in fit(self, X, y, **kwargs)
    135                 **self.filter_sk_params(self.build_fn.__call__))
    136         else:
--> 137             self.model = self.build_fn(**self.filter_sk_params(self.build_fn))
    138 
    139         loss_name = self.model.loss

<ipython-input-62-14daa2be96d1> in create_model(optimizer)
      3 def create_model(optimizer="SGD"):
      4     model=Sequential()
----> 5     model.add(Dense(input_dim=784, init="uniform", activation="relu"))
      6     model.add(Dense(output_dim=10, activation="sigmoid"))
      7 

TypeError: __init__() takes at least 2 arguments (4 given)
$\endgroup$
1
  • $\begingroup$ Can you please put the whole error here so that more insight can be taken out of it ? $\endgroup$ – enterML Aug 29 '16 at 12:18
1
$\begingroup$

The last line in your code is the result of above error. This is because the 'fit' method takes only two arguments i.e the data and the labels. In order to get rid of the above error, modify your code as following:

grid_result = grid.fit(X_train,Y_train)

After that you can perform various operations on your classifier such as :

best_model = grid_result.best_estimator_.model
best_param = grid_result.best_params_
$\endgroup$
8
  • $\begingroup$ Thanks, but that version doesn't work either: TypeError: __init__() takes at least 2 arguments (4 given) $\endgroup$ – Hendrik Aug 30 '16 at 7:41
  • $\begingroup$ Then please upload the whole error. I cannot simply think of anything useful from the above line. A proper full error as thrown by your code might give me some insight. $\endgroup$ – enterML Aug 30 '16 at 7:47
  • $\begingroup$ Sorry, I cannot publish here the full error message because it far more lengthy than the allowed text limit. $\endgroup$ – Hendrik Aug 30 '16 at 8:12
  • $\begingroup$ Done, I have citated more error lines. $\endgroup$ – Hendrik Aug 30 '16 at 13:18
  • $\begingroup$ Can you please run the code again after setting n_jobs=1 ? $\endgroup$ – enterML Aug 30 '16 at 14:20
0
$\begingroup$

model.fit() accepts only two parameters. Remove the third one in the last line of your code.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.