how to fix 'TypeError: can't pickle _thread.RLock objects" in cross_val_predict

I ran the following code:

gram_input = Input(shape = input_sh)

conv_1_layer = Conv2D(filters=32, input_shape=input_sh, activation='relu', 
                      kernel_size=(7,embeddigs_cols), padding='valid')(gram_input)
conv_1_layer.shape

max_1_pooling = MaxPooling2D(pool_size=((max_len_sentences-7+1),1), strides=None, padding='valid')(conv_1_layer)
max_1_pooling.shape

fully_connected_1_gram = Flatten()(max_1_pooling)
fully_connected_1_gram.shape

fully_connected_1_gram = Reshape((32, 1, 1))(fully_connected_1_gram)
fully_connected_1_gram.shape

conv_2_layer = Conv2D(filters=64, kernel_size=(5,1), activation='relu', 
                      padding='valid')(fully_connected_1_gram)
conv_2_layer.shape

max_2_pooling = MaxPooling2D(pool_size=((32-5+1),1), strides=None, padding='valid')(conv_2_layer)
max_2_pooling.shape

fully_connected_2_gram = Flatten()(max_2_pooling)
fully_connected_2_gram.shape

fully_connected_2_gram = Reshape((64, 1, 1))(fully_connected_2_gram)
fully_connected_2_gram.shape

conv_3_layer =  Conv2D(filters=128, kernel_size=(3,1), activation='relu', 
                      padding='valid')(fully_connected_2_gram)
conv_3_layer.shape

conv_4_layer = Conv2D(filters=128, kernel_size=(3,1), activation='relu', 
                     padding='valid')(conv_3_layer)
conv_4_layer.shape

conv_5_layer = Conv2D(filters=64, kernel_size=(3,1), activation='relu', 
                     padding='valid')(conv_4_layer)
conv_5_layer.shape


max_5_pooling = MaxPooling2D(pool_size=(58,1), strides=None, padding='valid')(conv_5_layer)
max_5_pooling.shape



fully_connected = Flatten()(max_5_pooling)
fully_connected.shape

deep_dense_1_layer = Dense(32, activation='relu')(fully_connected)
deep_dense_1_layer = Dropout(0.2)(deep_dense_1_layer) # <-------- [HyperParameter]

deep_dense_1_layer.shape

deep_dense_2_layer = Dense(32, activation='relu')(deep_dense_1_layer)
deep_dense_2_layer = Dropout(0.2)(deep_dense_2_layer) # <-------- [HyperParameter]

deep_dense_2_layer.shape

deep_dense_3_layer = Dense(16, activation='relu')(deep_dense_2_layer)
deep_dense_3_layer = Dropout(0.2)(deep_dense_3_layer) # <-------- [HyperParameter]

deep_dense_3_layer.shape

predictions = Dense(K, activation='softmax')(deep_dense_3_layer)

criticality_network = Model(inputs=[gram_input],outputs=[predictions]) 

print(criticality_network.summary())

criticality_network.compile(optimizer='adam',loss='binary_crossentropy',
                                  metrics=['accuracy'])


history= KerasClassifier(build_fn=criticality_network,
                                epochs = 100,
                                batch_size=64,
                                )

kf = KFold(n_splits=10)
pred = cross_val_predict(history, 
                      corpora_train_x, 
                      target_y,
                      cv=kf)

Everything is ok but in line of pred = cross_val_predict(history, ...), I get the error:

TypeError: can't pickle _thread.RLock objects

The shape of corpora_train_x is (10000, 72, 100, 1) and the shape of train_y_data is (10000, )

The shape of input_sh is (72, 100, 1)

The value of max_len_sentences is 72

Type of corpora_train_x is: <class 'numpy.memmap'>

Type of target_y is: <class 'numpy.ndarray'>

Also, I use:

  • Python v3.7
  • Scikit-learn 0.20.2
  • Tensorflow 2.0
  • Keras 2.3.1.

I didn't install PyTorch to avoid any conflict reason.

It should be noted that when I use fit to training the same data everything is Ok.

How can I fix the error?