'KerasClassifier' object has no attribute 'loss' - python-3.x

I am doing churn prediction using keras. I have used column transformer from Sklearn. My code is--
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
def keras_classifier_wrapper():
classifier = Sequential()
classifier.add(Dense(9, input_dim=13, activation='relu'))
classifier.add(Dense(8, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return clf
clf = KerasClassifier(keras_classifier_wrapper, epochs=20, batch_size=50, verbose=0)
categorical_pipe = Pipeline([
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
numerical_pipe = Pipeline([
('imputer', SimpleImputer(strategy='median'))
])
preprocessing = ColumnTransformer(
[('cat', categorical_pipe, cat_var1),
('num', numerical_pipe, num_var1)])
model3 = Pipeline([
('preprocess', preprocessing),
('keras_clf', clf)
])
model3.fit(X_train, y_train)
But it showing an error-
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-162-1f0472b386ae> in <module>()
----> 1 model3.fit(X_train, y_train)
2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/wrappers/scikit_learn.py in fit(self, x, y, **kwargs)
157 self.model = self.build_fn(**self.filter_sk_params(self.build_fn))
158
--> 159 if (losses.is_categorical_crossentropy(self.model.loss) and
160 len(y.shape) != 2):
161 y = to_categorical(y)
AttributeError: 'KerasClassifier' object has no attribute 'loss'
Can you plz tell me why this error is showing and how to solve it.
Thanks in advance

problem is in your keras_classifier_wrapper function
def keras_classifier_wrapper():
classifier = Sequential()
classifier.add(Dense(9, input_dim=13, activation='relu'))
classifier.add(Dense(8, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return clf # should be return classifier
you are trying to return clf but there is no clf it is defined afterwards. try to return classifier then it will work

Related

Why is there no improvement in a categorical data time series model?

I built a simple categorical time series model to predict the next number of a random sequence, but the accuracy hardly moved even I trained it for 10000 epochs. The validation loss started to take off after a few hundred epochs. Could anyone make suggestions for improvement? Here's the model:
import os
import sys
import pandas as pd
import numpy as np
from sklearn.preprocessing import OneHotEncoder
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
DEVICE = 'CPU'
if DEVICE == 'CPU':
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
else:
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
print(tf.test.gpu_device_name())
TOTAL_CATALOG=4
POSSIBLE_OUTCOME_COL=4
LOOK_BACK_WINDOW=1
TRAINING_DATA_RATIO=0.8
TRAINING_EPOCHS=10000
sys.path.insert(0, '/DataScience/MyModules')
from m6data import getDrawData, series_to_supervised, Split_data, get_all_categories
def get_all_categories_local(last_combination):
all_category = np.arange(1, last_combination+1)
return all_category.reshape(1,all_category.shape[0])
All_categories=get_all_categories_local(TOTAL_CATALOG)
data_sequence = [1,1,2,4,2,3,1,2,3,3,4,1,2,3,4,2,2,3,1,3]
raw_df = pd.DataFrame(data_sequence, columns=['NE'])
values = raw_df.values
# 05-Apr-2022: One-Hot Encoding
oh_encoder = OneHotEncoder(categories=All_categories, sparse=False)
encoded_input = oh_encoder.fit_transform(values)
FEATURES = encoded_input.shape[1]
POSSIBLE_OUTCOME_COL = FEATURES
draw_reframe = series_to_supervised(encoded_input, LOOK_BACK_WINDOW,1)
train, test = Split_data(draw_reframe, TRAINING_DATA_RATIO)
# Total input = all possible One-Hot Encoding outcome * number of look-back samples.
ALL_INPUT = POSSIBLE_OUTCOME_COL * LOOK_BACK_WINDOW
# split into input and outputs
train_X, train_y = train.iloc[:,:ALL_INPUT], train.iloc[:,ALL_INPUT:]
test_X, test_y = test.iloc[:,:ALL_INPUT], test.iloc[:,ALL_INPUT:]
train_X = train_X.values.reshape((train_X.shape[0], LOOK_BACK_WINDOW , FEATURES))
test_X = test_X.values.reshape((test_X.shape[0], LOOK_BACK_WINDOW, FEATURES))
print(train_X.shape, train_y.shape)
print(test_X.shape, test_y.shape)
def create_model():
model = Sequential()
model.add(LSTM(10,
return_sequences=False,
input_shape=(train_X.shape[1], train_X.shape[2]),
activation='relu'
)
)
#model.add(LSTM(20))
model.add(Dense(units=train_y.shape[1], activation='softmax'))
model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.00005),
loss = 'categorical_crossentropy',
metrics=['accuracy'])
return model
model=create_model()
history = model.fit(
train_X, train_y,
epochs=TRAINING_EPOCHS,
batch_size=8,
validation_data=(test_X, test_y),
verbose=1,
)
Here are the plots of accuracies and losses (red=training, blue=validation).
Accuracies
Losses
Thank you in advance for any suggestions.
Update (13-Jun-2022)
I changed my model to the following
def create_model():
model = Sequential()
model.add(LSTM(50,
return_sequences=True,
input_shape=(train_X.shape[1], train_X.shape[2]),
activation='relu'
)
)
model.add(LSTM(units=1000, kernel_regularizer=regularizers.l1(0.05), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=1000, kernel_regularizer=regularizers.l1(0.05), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=1000, kernel_regularizer=regularizers.l1(0.05), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=1000, kernel_regularizer=regularizers.l1(0.05), activation='relu'))
model.add(Dropout(0.3))
model.add(BatchNormalization())
model.add(Dense(1000))
model.add(Dense(units=train_y.shape[1], activation='softmax'))
model.compile(optimizer = tf.keras.optimizers.SGD(learning_rate=1e-2, nesterov=True),
#tf.keras.optimizers.Adam(learning_rate=0.001),
loss = 'categorical_crossentropy',
metrics=['accuracy'])
return model
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
patience=20,min_lr=1e-10)
early_stop = EarlyStopping(monitor='loss', patience=100)
history = model.fit(
train_X, train_y,
epochs=TRAINING_EPOCHS,
batch_size=16,
validation_split=0.1,
validation_data=(test_X, test_y),
verbose=1,
shuffle=False,
callbacks=([reduce_lr], [early_stop])
Accuracy was bouncing around and Val_accuracy was zero all the way. The loss and val_loss were almost the same and dropping together.
Can anyone advise what I can do in this scenario?

AttributeError: 'Sequential' object has no attribute 'eval'

'''
I am using SHAP, for my model analysis, and while calling 'DeepExplainer' I am getting "AttributeError: 'Sequential' object has no attribute 'eval' "
I am using theano with keras instead of tensor-flow, because there is some version mismatch issue with SHAP, same i have posted in other question. So now i am trying same stuff but this time, _Backend i am using PyTorch and model building is fine but while using SHAP DeepExplainer it is throwing Attribute error, i am newbee to this type of error for Model Explainer domain
'''
Input:
print('Pad sequences (samples x time)')
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)
print('Build model...')
model = Sequential()
model.add(Embedding(max_features, 128))
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
# try using different optimizers and different optimizer configs
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
print('Train...')
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=1,
validation_data=(x_test, y_test))
score, acc = model.evaluate(x_test, y_test,
batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)
output: No Error
After That:
Input:
import shap
# we use the first 100 training examples as our background dataset to integrate over
explainer = shap.DeepExplainer(model, x_train[:100])
Error:
AttributeError Traceback (most recent call last)
<ipython-input-12-9cca779d01d2> in <module>
1 # we use the first 100 training examples as our background dataset to integrate over
----> 2 explainer = shap.DeepExplainer(model,1)
c:\users\shubh\.conda\envs\pytorch_cpu\lib\site-packages\shap\explainers\deep\__init__.py in __init__(self, model, data, session, learning_phase_flags)
79 self.explainer = TFDeepExplainer(model, data, session, learning_phase_flags)
80 elif framework == 'pytorch':
---> 81 self.explainer = PyTorchDeepExplainer(model, data)
82
83 self.expected_value = self.explainer.expected_value
c:\users\shubh\.conda\envs\pytorch_cpu\lib\site-packages\shap\explainers\deep\deep_pytorch.py in __init__(self, model, data)
47 self.target_handle.remove()
48 del self.layer.target_input
---> 49 self.model = model.eval()
50
51 self.multi_output = False
AttributeError: 'Sequential' object has no attribute 'eval'
Any help or direction for resolving this error?

cross_val_score's n_jobs = -1 argument not working in python 3.6

i was trying to improve the accuracy and evaluate my Artificial Neural Network, but i encountered an issue that n_jobs = -1 of cross_val_score was not working,
i am using tensorflow on my cpu and my error was:-
BrokenProcessPool: A task has failed to un-serialize. Please ensure
that the arguments of the function are all picklable.
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
def build_classifier():
classifier = Sequential()
classifier.add(Dense(6, kernel_initializer='uniform', activation='relu', input_dim=11))
classifier.add(Dense(6, kernel_initializer='uniform', activation='relu'))
classifier.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
classifier.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
return classifier
classifier = KerasClassifier(build_fn = build_classifier, batch_size = 10, nb_epoch = 100)
accuracies = cross_val_score(estimator= classifier, X= x_train, y= y_train, cv = 10, n_jobs= 1)

tensorflow model.load problems

I was working on tensorflow and tried to save and load a model. model resides in below file
model = keras.Sequential()
model.add(keras.layers.Dense(785, activation ='sigmoid' ))
model.add(keras.layers.Dense(25, activation = 'sigmoid'))
model.add(keras.layers.Dense(10, activation = 'sigmoid'))
model.compile(optimizer=tf.train.GradientDescentOptimizer(0.01),
loss='mse',
metrics=['mae'])
model.fit(X,Y,epochs = 20, callbacks=[history])
f = h5py.File(r'C:\Users\akash\Desktop\Big Data\Model\model1', "w")
tf.keras.models.save_model(
model,
f,
overwrite=True,
include_optimizer=True
)
and my load file is as below
model1 = tf.keras.models.load_model(
r'C:\Users\akash\Desktop\Big Data\Model\model1',
custom_objects=None,
compile=True
)
model1.compile(optimizer=tf.train.GradientDescentOptimizer(0.01),
loss='mse',
metrics=['mae'])
I had to compile my model again as tensorflow requires you to do so and does not allow optimisers to be saved
and due to this im getting the below error
Using TensorFlow backend.
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.
Traceback (most recent call last):
File "C:/Users/akash/Desktop/Big Data/scripts/load_model.py", line 21, in <module>
metrics=['mae'])
File "C:\Python\lib\site-packages\tensorflow\python\training\checkpointable\base.py", line 426, in _method_wrapper
method(self, *args, **kwargs)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 525, in compile
metrics, self.output_names)
AttributeError: 'Sequential' object has no attribute 'output_names'
Maybe this helps you:
# MLP for Pima Indians Dataset Serialize to JSON and HDF5
from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_json
import numpy
import os
# fix random seed for reproducibility
numpy.random.seed(7)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10, verbose=0)
# evaluate the model
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
# later...
# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
# evaluate loaded model on test data
loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
score = loaded_model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))

Making predictions using Keras. I keep getting error message

Sorry if the query is primitive.
I have some code trying to classify integers if they are prime numbers or not. I have trained model using Keras. I am trying make predictions using:
predict( x, batch_size=None, verbose=0, steps=None)
I keep getting the following error message:
----> predict(x=5000003, batch_size=None, verbose=0, steps=None)
NameError: name 'predict' is not defined
When I used the the following command :"model.predict(x=5000003, batch_size=None, verbose=0, steps=None)" I got this error message "AttributeError: 'KerasClassifier' object has no attribute 'model'"
Code:
import numpy
from numpy import array
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import GridSearchCV
seed = 7
numpy.random.seed(seed)
def isPrime(number):
if number == 1:
return 0
elif number == 2:
return 1
elif number % 2 == 0:
return 0
for d in range(3, int(number**(0.5)+1), 2):
if number % d == 0:
return 0
else:
return 1
p=[]
N=[]
for i in range (1,10000):
p=[i,isPrime(i)]
N=N+[p]
a=array (N)
X=a[:10000,0]
Y=a[:10000,1]
def create_model(optimizer='rmsprop', init='glorot_uniform'):
# create model
model = Sequential()
model.add(Dense(2, input_dim=1, kernel_initializer=init, activation='selu'))
model.add(Dense(1, kernel_initializer=init, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
# create model
model = KerasClassifier(build_fn=create_model, epochs=1000, batch_size=100, init='glorot_uniform', verbose=0)
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed)
results = cross_val_score(model, X, Y, cv=kfold)
print(results.mean())
predict(x=5000003, batch_size=None, verbose=0, steps=None)
predict is a function of the model object, so you would use it as:
model = KerasClassifier(build_fn=create_model, epochs=1000, batch_size=100, init='glorot_uniform', verbose=0)
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed)
results = cross_val_score(model, X, Y, cv=kfold)
print(results.mean())
# Call on model
model.predict(x=5000003, batch_size=None, verbose=0, steps=None)
Here is the source code to investigate what it does behind the scenes.

Resources