nalinaksh commited on
Commit
e314fcf
·
1 Parent(s): df4cb04

Update QA_Tensorflow.py

Browse files
Files changed (1) hide show
  1. 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
- start_index,end_index = 0,0
 
 
 
44
  for i, probs in enumerate(zip(start_probabilities, end_probabilities)):
45
  sp, ep = probs
46
- scores = tf.matmul(sp[:,np.newaxis], ep[np.newaxis,:])
47
  index = np.triu(scores).argmax().item()
48
- row = int(index/384)
49
- col = index % 384
50
  score = scores[row][col]
51
  if(score > max_score):
52
  max_score = score
53
- start_index = row
54
- end_index = col
 
55
 
56
  #Return characters from context corresponding to start and end of token characters
57
- start = int(offset_mapping[i][start_index][0])
58
- end = int(offset_mapping[i][end_index][1])
59
- return context[start:end+1]
 
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]