Keras: Ошибка получения «невозможно выбрать объекты _thread.RLock» при использовании широких и глубоких NN с рандомизированным поиском - PullRequest
1 голос
/ 25 октября 2019

Я создаю модель Wide-and-Deep NN и применяю к ней рандомизированный поиск с помощью Keras. Однако я не могу выполнить функцию fit(), потому что я получаю следующую ошибку:

невозможно выбрать объекты _thread.RLock

СейчасЯ исследовал это, и наиболее распространенными причинами являются использование лямбда-слоя или многопроцессорность, обе вещи, которые я не использую в своем коде ... Я был бы признателен за любую помощь, которую вы можете мне предоставить.

Здесьмой код

Создать модель

def build_model(n_hidden=1, n_neurons=30, learning_rate=3e-3, activation="elu", loss="categorical_crossentropy", shape=X_train.shape[1:]):
    #Create input layer and assign to current output layer
    input_ = keras.layers.Input(shape=shape) 

    cur_output = input_

    #Add hiddem layers
    for layer in range(n_hidden):
        cur_output = keras.layers.Dense(n_neurons, kernel_initializer="he_normal", activation=activation)(cur_output)
        cur_output = keras.layers.BatchNormalization()(cur_output)

    #Concatenate Layer
    concat = keras.layers.concatenate([input_, cur_output])

    #Output Layer
    output = keras.layers.Dense(1)(concat)

    #Create Model
    model = keras.models.Model(inputs=[input_], outputs=[output])

    #Add optimizer
    optimizer=keras.optimizers.SGD(lr=learning_rate, momentum=0.9, nesterov=True)

    #Compile model
    model.compile(loss=loss, optimizer=optimizer)

    return model

model = build_model()
keras_class = keras.wrappers.scikit_learn.KerasClassifier(model)

Установить рандомизированный поиск

param_distribs = {
    "n_hidden": [0, 1, 2, 3],
    "n_neurons": np.arange(1, 100),
    "learning_rate": reciprocal(3e-4, 3e-2),
}

rnd_search_cv = RandomizedSearchCV(keras_class, param_distribs, n_iter=10, cv=3, verbose=2)

Создание обратного вызова для контрольной точки и ранней остановки

checkpoint_cb = keras.callbacks.ModelCheckpoint("MNIST_model.h5", save_best_only=True)
early_stopping_cb = keras.callbacks.EarlyStopping(patience=10,
                                                  restore_best_weights=True)

Подходящая модель

history = rnd_search_cv.fit(X_train, y_train, epochs=100,
                  validation_data=(X_valid, y_valid),
                  callbacks=[checkpoint_cb, early_stopping_cb])

РЕДАКТИРОВАТЬ

Полная ошибкасообщение

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-199-297f92963990> in <module>()
----> 1 history = rnd_search_cv.fit(X_train, y_train)
      2 
      3 

19 frames
/usr/local/lib/python3.6/dist-packages/sklearn/model_selection/_search.py in fit(self, X, y, groups, **fit_params)
    631         n_splits = cv.get_n_splits(X, y, groups)
    632 
--> 633         base_estimator = clone(self.estimator)
    634 
    635         parallel = Parallel(n_jobs=self.n_jobs, verbose=self.verbose,

/usr/local/lib/python3.6/dist-packages/sklearn/base.py in clone(estimator, safe)
     62     new_object_params = estimator.get_params(deep=False)
     63     for name, param in new_object_params.items():
---> 64         new_object_params[name] = clone(param, safe=False)
     65     new_object = klass(**new_object_params)
     66     params_set = new_object.get_params(deep=False)

/usr/local/lib/python3.6/dist-packages/sklearn/base.py in clone(estimator, safe)
     53     elif not hasattr(estimator, 'get_params') or isinstance(estimator, type):
     54         if not safe:
---> 55             return copy.deepcopy(estimator)
     56         else:
     57             raise TypeError("Cannot clone object '%s' (type %s): "

/usr/lib/python3.6/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.

/usr/lib/python3.6/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)

/usr/lib/python3.6/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:

/usr/lib/python3.6/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

/usr/lib/python3.6/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.

/usr/lib/python3.6/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)

/usr/lib/python3.6/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:

/usr/lib/python3.6/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

/usr/lib/python3.6/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.

/usr/lib/python3.6/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)

/usr/lib/python3.6/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:

/usr/lib/python3.6/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

/usr/lib/python3.6/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.

/usr/lib/python3.6/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)

/usr/lib/python3.6/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:

/usr/lib/python3.6/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

/usr/lib/python3.6/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
...