From tensorflow.keras.wrappers.scikit_learn import KerasClassifier

I am running the following code "import os
import sys
import pandas as pd
import numpy as np

from sklearn.metrics import confusion_matrix
from sklearn.model_selection import GridSearchCV

import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, GlobalMaxPooling1D, LeakyReLU
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier

from tensorflow.keras.models import Model

from transformers import TFGPT2Model, GPT2Tokenizer, AutoConfig

os.environ[‘TF_XLA_FLAGS’] = ‘–tf_xla_enable_xla_devices’

Loading directories to data

cur_dir = os.path.dirname(file)
data_dir_name = “data”
data_name = “restaurant.xlsx”

Accepting file name if given as arg

if len(sys.argv) >= 2:
print(f"\nLooking for data file {sys.argv[1]} \n")
data_name = sys.argv[1]

repo_dir = os.path.dirname(cur_dir)
data_dir = os.path.join(repo_dir, data_dir_name, data_name)

Load and preprocess data

news_data = pd.read_excel(data_dir)

Shuffle the data

news_data = news_data.sample(frac=1, random_state=42)

Remove rows with NaN values

news_data = news_data.dropna()

X = news_data[‘DATA’]
y = pd.get_dummies(news_data[‘CODES’]).values # Convert to one-hot encoded numpy array

Tokenize the text using a smaller GPT-2 variant

tokenizer = GPT2Tokenizer.from_pretrained(‘distilgpt2’)

Set padding token if it’s not already set

if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token

Limit the token length to a reasonable number to save memory

max_token_length = min(512, max([len(tokenizer.encode(text)) for text in news_data[‘DATA’]]))

Tokenize and truncate/pad inputs

encoded_inputs = tokenizer(list(news_data[‘DATA’]), padding=True, truncation=True, max_length=max_token_length, return_tensors=‘tf’)
x = encoded_inputs[‘input_ids’].numpy() # Convert to numpy array for compatibility with scikit-learn

Function to create the model for KerasClassifier

def create_model(activation=‘relu’):

print(f"\nDownloading model\n")
cache_dir = 'pretrained'
config = AutoConfig.from_pretrained('distilgpt2', cache_dir=cache_dir)
gpt2_model = TFGPT2Model.from_pretrained('distilgpt2', config=config)
print(f"\Finished downloading model\n")

input_ids = Input(shape=(max_token_length,), name='input_ids', dtype='int32')
outputs = gpt2_model(input_ids)[0]
pooled_output = GlobalMaxPooling1D()(outputs)

if activation == 'leaky_relu':
    dense_layer = Dense(64)(pooled_output)
    outputs = LeakyReLU()(dense_layer)
    outputs = Dense(64, activation=activation)(pooled_output)

outputs = Dense(y.shape[1], activation='softmax')(outputs)  # y.shape[1] will give the number of categories
model = Model(inputs=input_ids, outputs=outputs)
return model

Wrap the model with KerasClassifier

model = KerasClassifier(build_fn=create_model, verbose=0)

Define the grid search parameters

batch_size = [8, 16,32]
epochs = [10]
activation = [‘relu’, ‘elu’, ‘swish’, ‘leaky_relu’]

param_grid = dict(batch_size=batch_size, epochs=epochs, activation=activation)

grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3)
grid_result =, y)

Get the best model

best_model = grid_result.best_estimator_.model

Get the predicted labels from the best model

y_pred = best_model.predict(x)

Convert predicted labels to one-hot encoded format

y_pred_onehot = np.zeros(y.shape)
y_pred_onehot[np.arange(len(y_pred)), y_pred.argmax(axis=1)] = 1

Calculate the confusion matrix

conf_matrix = confusion_matrix(y.argmax(axis=1), y_pred_onehot.argmax(axis=1))

Display the confusion matrix

print(“\nConfusion Matrix:”)

Summarize grid search results

print(“\nGrid Search Results:”)
print(“Best: %f using %s” % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_[‘mean_test_score’]
stds = grid_result.cv_results_[‘std_test_score’]
params = grid_result.cv_results_[‘params’]

for mean, stdev, param in zip(means, stds, params):
print(“%f (%f) with: %r” % (mean, stdev, param))

and getting the following error

" from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
ModuleNotFoundError: No module named ‘tensorflow.keras.wrappers’".

Is there any solution for this?

Hi @Tauhid_ullah_shah, There is a no wrapper module in tensorflow.keras. You can import KerasClassifier using

from scikeras.wrappers import KerasClassifier 

Thank You.

Hi Kiran and Tauhid, I got this error:

  1 from sklearn.model_selection import StratifiedKFold, GridSearchCV

----> 2 from scikeras.wrappers import KerasClassifier
3 import tensorflow as tf
4 from tensorflow.keras import layers

ModuleNotFoundError: No module named ‘scikeras’

Hi @Konstantin_F, Could please try to install scikeras using pip install scikeras and then import from scikeras.wrappers import KerasClassifier. Thank You.

A huge thanks, this solved my problem