Update QA_Tensorflow.py
Browse files- QA_Tensorflow.py +13 -9
QA_Tensorflow.py
CHANGED
|
@@ -40,20 +40,24 @@ def question_answering_tf(question, context):
|
|
| 40 |
|
| 41 |
#Finding (start token, end token) pair with best probability score
|
| 42 |
max_score = 0.0
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
for i, probs in enumerate(zip(start_probabilities, end_probabilities)):
|
| 45 |
sp, ep = probs
|
| 46 |
-
scores =
|
| 47 |
index = np.triu(scores).argmax().item()
|
| 48 |
-
row =
|
| 49 |
-
col = index %
|
| 50 |
score = scores[row][col]
|
| 51 |
if(score > max_score):
|
| 52 |
max_score = score
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
#Return characters from context corresponding to start and end of token characters
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
return context[
|
|
|
|
| 40 |
|
| 41 |
#Finding (start token, end token) pair with best probability score
|
| 42 |
max_score = 0.0
|
| 43 |
+
start_token = 0
|
| 44 |
+
end_token = 0
|
| 45 |
+
offset_index = 0
|
| 46 |
+
|
| 47 |
for i, probs in enumerate(zip(start_probabilities, end_probabilities)):
|
| 48 |
sp, ep = probs
|
| 49 |
+
scores = sp[:,np.newaxis] * ep[np.newaxis,:]
|
| 50 |
index = np.triu(scores).argmax().item()
|
| 51 |
+
row = index // scores.shape[1]
|
| 52 |
+
col = index % scores.shape[1]
|
| 53 |
score = scores[row][col]
|
| 54 |
if(score > max_score):
|
| 55 |
max_score = score
|
| 56 |
+
start_token = row
|
| 57 |
+
end_token = col
|
| 58 |
+
offset_index = i
|
| 59 |
|
| 60 |
#Return characters from context corresponding to start and end of token characters
|
| 61 |
+
start_char, _ = offset_mapping[offset_index][start_token]
|
| 62 |
+
_, end_char = offset_mapping[offset_index][end_token]
|
| 63 |
+
return context[start_char:end_char+1]
|