Kaggle competition: TensorFlow 2.0 Question Answering
<SOS> metric: NQMicroF1 Kaggle data source: tensorflow-2.0-question-answering<init_hyperparams>
pred_path = ".. /input/nq-sample-csv/first50.csv"
ALPHA = 0.2
BETA = 0.45
ETA = 0.4
PHI = 0.2
DELTA = 0.85
brown_freqs = dict()
N = 0
def get_best_synset_pair(word_1, word_2):

 max_sim = -1.0
synsets_1 = wn.synsets(word_1)
 synsets_2 = wn.synsets(word_2)
 if len(synsets_1)== 0 or len(synsets_2)== 0:
return None, None
else:
max_sim = -1.0
best_pair = None, None
for synset_1 in synsets_1:
for synset_2 in synsets_2:
sim = wn.wup_similarity(synset_1, synset_2)
 if not sim:
continue
if sim > max_sim:
max_sim = sim
best_pair = synset_1, synset_2
return best_pair
def length_dist(synset_1, synset_2):

 l_dist = sys.maxsize
if synset_1 is None or synset_2 is None:
return 0.0
if synset_1 == synset_2:
l_dist = 0.0
else:
wset_1 = set([str(x.name())for x in synset_1.lemmas() ])
 wset_2 = set([str(x.name())for x in synset_2.lemmas() ])
 if len(wset_1.intersection(wset_2)) > 0:
l_dist = 1.0
else:
l_dist = synset_1.shortest_path_distance(synset_2)
 if l_dist is None:
l_dist = 0.0
return math.exp(-ALPHA * l_dist)
 def hierarchy_dist(synset_1, synset_2):

 h_dist = sys.maxsize
if synset_1 is None or synset_2 is None:
return h_dist
if synset_1 == synset_2:
h_dist = max([x[1] for x in synset_1.hypernym_distances() ])
 else:
hypernyms_1 = {x[0]:x[1] for x in synset_1.hypernym_distances() }
hypernyms_2 = {x[0]:x[1] for x in synset_2.hypernym_distances() }
lcs_candidates = set(hypernyms_1.keys() ).intersection(
 set(hypernyms_2.keys()))
if len(lcs_candidates)> 0:
lcs_dists = []
for lcs_candidate in lcs_candidates:
lcs_d1 = 0
if lcs_candidate in hypernyms_1:
lcs_d1 = hypernyms_1[lcs_candidate]
lcs_d2 = 0
if lcs_candidate in hypernyms_2:
lcs_d2 = hypernyms_2[lcs_candidate]
lcs_dists.append(max([lcs_d1, lcs_d2]))
h_dist = max(lcs_dists)
 else:
h_dist = 0
return(( math.exp(BETA * h_dist)- math.exp(-BETA * h_dist)) / 
(math.exp(BETA * h_dist)+ math.exp(-BETA * h_dist)))
 def word_similarity(word_1, word_2):
synset_pair = get_best_synset_pair(word_1, word_2)
 return(length_dist(synset_pair[0], synset_pair[1])*
hierarchy_dist(synset_pair[0], synset_pair[1]))
def most_similar_word(word, word_set):

 max_sim = -1.0
sim_word = ""
for ref_word in word_set:
sim = word_similarity(word, ref_word)
 if sim > max_sim:
max_sim = sim
sim_word = ref_word
return sim_word, max_sim
def info_content(lookup_word):

 global N
if N == 0:
for sent in brown.sents() :
for word in sent:
word = word.lower()
if word not in brown_freqs:
brown_freqs[word] = 0
brown_freqs[word] = brown_freqs[word] + 1
N = N + 1
lookup_word = lookup_word.lower()
n = 0 if lookup_word not in brown_freqs else brown_freqs[lookup_word]
return 1.0 -(math.log(n + 1)/ math.log(N + 1))
def semantic_vector(words, joint_words, info_content_norm):

 sent_set = set(words)
 semvec = np.zeros(len(joint_words))
i = 0
for joint_word in joint_words:
if joint_word in sent_set:
semvec[i] = 1.0
if info_content_norm:
semvec[i] = semvec[i] * math.pow(info_content(joint_word), 2)
 else:
sim_word, max_sim = most_similar_word(joint_word, sent_set)
 semvec[i] = PHI if max_sim > PHI else 0.0
if info_content_norm:
semvec[i] = semvec[i] * info_content(joint_word)* info_content(sim_word)
 i = i + 1
return semvec
def semantic_similarity(sentence_1, sentence_2, info_content_norm):

 words_1 = nltk.word_tokenize(sentence_1)
 words_2 = nltk.word_tokenize(sentence_2)
 joint_words = set(words_1 ).union(set(words_2))
vec_1 = semantic_vector(words_1, joint_words, info_content_norm)
 vec_2 = semantic_vector(words_2, joint_words, info_content_norm)
 return np.dot(vec_1, vec_2.T)/(np.linalg.norm(vec_1)* np.linalg.norm(vec_2))
def word_order_vector(words, joint_words, windex):

 wovec = np.zeros(len(joint_words))
i = 0
wordset = set(words)
 for joint_word in joint_words:
if joint_word in wordset:
wovec[i] = windex[joint_word]
else:
sim_word, max_sim = most_similar_word(joint_word, wordset)
 if max_sim > ETA:
wovec[i] = windex[sim_word]
else:
wovec[i] = 0
i = i + 1
return wovec
def word_order_similarity(sentence_1, sentence_2):

 words_1 = nltk.word_tokenize(sentence_1)
 words_2 = nltk.word_tokenize(sentence_2)
 joint_words = list(set(words_1 ).union(set(words_2)))
 windex = {x[1]: x[0] for x in enumerate(joint_words)}
r1 = word_order_vector(words_1, joint_words, windex)
 r2 = word_order_vector(words_2, joint_words, windex)
 return 1.0 -(np.linalg.norm(r1 - r2)/ np.linalg.norm(r1 + r2))
def similarity(sentence_1, sentence_2, info_content_norm):

 return DELTA * semantic_similarity(sentence_1, sentence_2, info_content_norm)+ \ 
(1.0 - DELTA)* word_order_similarity(sentence_1, sentence_2 )<compute_test_metric>
df = pd.read_json('.. /input/tensorflow2-question-answering/simplified-nq-test.jsonl', lines = True, dtype={'example_id':'Object'})
 submission = pd.DataFrame(index=pd.concat([df.example_id + '_short', df.example_id + '_long']), columns=['PredictionString'] ).sort_index()
for i in [5,7,11,12,13,15,16,18]:
print(train_df['question1'][i])
 print(train_df['question2'][i])
 print(train_df['is_duplicate'][i])
 print(similarity(train_df['question1'][i], train_df['question2'][i], False))
print(similarity(train_df['question1'][i], train_df['question2'][i], True))<train_model>
updates = pd.read_csv(pred_path, na_filter=False ).set_index('example_id' ).sort_index()
submission.loc[updates.index.intersection(submission.index),'PredictionString'] = updates['PredictionString']
def runXGB(train_X, train_y, test_X, test_y=None, feature_names=None, seed_val=0):
params = {}
params["objective"] = "binary:logistic"
params['eval_metric'] = 'logloss'
params["eta"] = 0.02
params["subsample"] = 0.7
params["min_child_weight"] = 1
params["colsample_bytree"] = 0.7
params["max_depth"] = 4
params["silent"] = 1
params["seed"] = seed_val
num_rounds = 250
plst = list(params.items())
 xgtrain = xgb.DMatrix(train_X, label=train_y)
 if test_y is not None:
xgtest = xgb.DMatrix(test_X, label=test_y)
 watchlist = [(xgtrain,'train'),(xgtest, 'test')]
model = xgb.train(plst, xgtrain, num_rounds, watchlist, early_stopping_rounds=100, verbose_eval=10)
 else:
xgtest = xgb.DMatrix(test_X)
 model = xgb.train(plst, xgtrain, num_rounds)
 pred_test_y = model.predict(xgtest)
 loss = 1
if test_y is not None:
loss = log_loss(test_y, pred_test_y)
 return pred_test_y, loss, model
else:
return pred_test_y, loss, model<train_on_grid>
submission.to_csv('submission.csv' )