TransWikia.com

How to add Earlystopper in Classifier Model

Data Science Asked on March 9, 2021

I have designed the following Binary Classifier Neural Network Model for a task. I want to add an early stopper to the model so that the model stops at an epoch where it has stopped learning significantly. How can I do that?

Model

X = badge1_data[['1','2','3','Score']] 
y = badge1_data['APR']

#Standardizing the Input Features
scaler = StandardScaler()
X = scaler.fit_transform(X)

#Train Test split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.3)

#Create Model
model = Sequential()
model.add(Dense(4, 
                  input_dim=4, 
                  kernel_initializer='normal', 
                  activation='relu'))

model.add(Dense(2, 
                  kernel_initializer='normal', 
                  activation='relu'))
   
model.add(Dense(1, 
                  activation='sigmoid',
                  kernel_initializer='normal'))
    
#Compile Model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

#Fit Model
model.fit(X_train, y_train, epochs = 5000, validation_split = 0.3, verbose=0, batch_size=256)

#Make predictions and convert to binary value
pred_train = model.predict(X_train)
pred_test = model.predict(X_test)

#ROC AUC Score
print('Train AUC = {:}'.format(roc_auc_score(y_train,pred_train)))
print('Test AUC = {:}'.format(roc_auc_score(y_test,pred_test)))

#Accuracy
print('Train Accuracy = ',accuracy_score(y_train,pred_train.round()))
print('Test Accuracy = ',accuracy_score(y_test,pred_test.round()))

One Answer

Early stopping can be achieved with Keras callbacks.

early_stop= tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3)

history = model.fit(X, y, callbacks=[early_stop])

patience: Number of epochs with no improvement after which training will be stopped.

To define the meaning of "significant learning stopping", you will have to try with the parameters of Callback. e.g.
monitor="val_loss", min_delta=0, patience=0, mode="auto", baseline=None, etc.

Keras_API

Answered by 10xAI on March 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP