questions_similarity / inference.py
Pankaj Singh Rawat
Initial commit
dbfbc03
import tensorflow as tf
import numpy
model = tf.keras.models.load_model('./model/trained_model.keras', safe_mode=False, compile=False)
# Show the model architecture
model.summary()
# GRADED FUNCTION: predict
def predict(question1, question2, threshold, verbose=False):
"""Function for predicting if two questions are duplicates.
Args:
question1 (str): First question.
question2 (str): Second question.
threshold (float): Desired threshold.
verbose (bool, optional): If the results should be printed out. Defaults to False.
Returns:
bool: True if the questions are duplicates, False otherwise.
"""
generator = tf.data.Dataset.from_tensor_slices((([question1], [question2]),None)).batch(batch_size=1)
### START CODE HERE ###
# Call the predict method of your model and save the output into v1v2
v1v2 = model.predict(generator)
out_size = v1v2.shape[1]
# Extract v1 and v2 from the model output
v1 = v1v2[:,:int(out_size/2)]
v2 = v1v2[:,int(out_size/2):]
print(v1.shape)
# Take the dot product to compute cos similarity of each pair of entries, v1, v2
# Since v1 and v2 are both vectors, use the function tf.math.reduce_sum instead of tf.linalg.matmul
d = tf.reduce_sum(v1 * v2)
# Is d greater than the threshold?
res = d > threshold
### END CODE HERE ###
if(verbose):
print("Q1 = ", question1, "\nQ2 = ", question2)
print("d = ", d.numpy())
print("res = ", res.numpy())
return d.numpy(), res.numpy()