Building the model is like below
def build_smallBERT_CNN_classifier_model():
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='text')
preprocessing_layer = hub.KerasLayer(bert_preprocessor, name='preprocessing')
encoder_inputs = preprocessing_layer(text_input)
encoder = hub.KerasLayer(bert_encoder_model, trainable=True, name='BERT_encoder')
outputs = encoder(encoder_inputs)
net = sequence_output = outputs["sequence_output"]
net = tf.keras.layers.Conv1D(32, 2, activation='relu')(net)
net = tf.keras.layers.Conv1D(64, 2, activation='relu')(net)
net = tf.keras.layers.MaxPooling1D(pool_size=2)(net)
net = tf.keras.layers.GlobalMaxPool1D()(net)
net = tf.keras.layers.Dense(512, activation="relu")(net)
net = tf.keras.layers.Dropout(0.1)(net)
net = tf.keras.layers.Dense(num_classes, activation="softmax", name='classifier')(net)
return tf.keras.Model(text_input, net)
Compiling is like below
loss = tf.keras.losses.CategoricalCrossentropy(from_logits=False)
metrics = tf.metrics.CategoricalAccuracy()
epochs = 15
optimizer=tf.keras.optimizers.Adam(1e-5)
# build the model
classifier_smallBERT_model_cnn = build_smallBERT_CNN_classifier_model()
# compile the model
classifier_smallBERT_model_cnn.compile(optimizer=optimizer,
loss=loss,
metrics=metrics)
Optional: Enhance with Callbacks:
class MyCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
if logs['val_categorical_accuracy'] >= 0.94:
print("\nValidation accuracy reached 94%!")
self.model.stop_training = True
Training the Model:
callback = MyCallback()
history = classifier_smallBERT_model_cnn.fit(x=X_train, y=y_train_one_hot,
validation_data=(X_val, y_val_one_hot),
batch_size=32,
epochs=epochs,
callbacks=[callback])
Testing the model
intent_mapping = df.groupby('intent_encoding')['intent'].first().to_dict()
user_utterance = "what should i prepare before i go"
user_utterance = np.array([user_utterance])
# Make the prediction
predictions = loaded_model.predict(user_utterance)
predicted_intent_index = np.argmax(predictions)
# Map the predicted intent index to intent label and response
predicted_intent_label = intent_mapping[str(predicted_intent_index)]
predicted_response = df.loc[df['intent'] == predicted_intent_label, 'response'].iloc[0]
print("Predicted Intent:", predicted_intent_label)
print("VELA:", predicted_response)
In this project, the spaCy Custom NER model to accurately identify and classify that.
def offsetter(lbl, doc, matchitem):
one = len(str(doc[0:matchitem[1]]))
subdoc = doc[matchitem[1]:matchitem[2]]
two = one + len(str(subdoc))
if one != 0:
one += 1
two += 1
return (one, two, lbl)
def spacify_row(document, label, entity_keywords):
matcher = PhraseMatcher(nlp.vocab)
for i in entity_keywords:
matcher.add(label, None, nlp(i))
nlp_document = nlp(document)
matches = matcher(nlp_document)
entity_list = [offsetter(label, nlp_document, match) for match in matches]
return (document, {'entities': entit
spacify_row('I love to eat seafood', 'FOOD_PREFERENCE',
entity_keywords = entity_phrases.get('FOOD_PREFERENCE'))
References:
https://medium.com/@reynxzz/building-a-smart-chatbot-with-intent-classification-and-named-entity-recognition-travelah-a-case-ee905f23fd9d
No comments:
Post a Comment