AhmedSSabir commited on
Commit
c6d03f9
1 Parent(s): f6e09cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -27
app.py CHANGED
@@ -87,35 +87,35 @@ def sentence_prob_mean(text):
87
 
88
 
89
 
90
- # def cloze_prob(text):
91
-
92
- # whole_text_encoding = tokenizer.encode(text)
93
- # text_list = text.split()
94
- # stem = ' '.join(text_list[:-1])
95
- # stem_encoding = tokenizer.encode(stem)
96
- # cw_encoding = whole_text_encoding[len(stem_encoding):]
97
- # tokens_tensor = torch.tensor([whole_text_encoding])
98
 
99
- # with torch.no_grad():
100
- # outputs = model(tokens_tensor)
101
- # predictions = outputs[0]
102
-
103
- # logprobs = []
104
- # start = -1-len(cw_encoding)
105
- # for j in range(start,-1,1):
106
- # raw_output = []
107
- # for i in predictions[-1][j]:
108
- # raw_output.append(i.item())
109
 
110
- # logprobs.append(np.log(softmax(raw_output)))
111
 
112
 
113
- # conditional_probs = []
114
- # for cw,prob in zip(cw_encoding,logprobs):
115
- # conditional_probs.append(prob[cw])
116
 
117
 
118
- # return np.exp(np.sum(conditional_probs))
119
 
120
 
121
 
@@ -145,11 +145,11 @@ def Visual_re_ranker(caption_man, caption_woman, context_label, context_prob):
145
  sim_w = get_sim(sim_w)
146
 
147
 
148
- LM_man = sentence_prob_mean(caption_man)
149
- LM_woman = sentence_prob_mean(caption_woman)
150
 
151
- # LM_man = cloze_prob(caption_man)
152
- # LM_woman = cloze_prob(caption_woman)
153
 
154
 
155
 
 
87
 
88
 
89
 
90
+ def cloze_prob(text):
91
+
92
+ whole_text_encoding = tokenizer.encode(text)
93
+ text_list = text.split()
94
+ stem = ' '.join(text_list[:-1])
95
+ stem_encoding = tokenizer.encode(stem)
96
+ cw_encoding = whole_text_encoding[len(stem_encoding):]
97
+ tokens_tensor = torch.tensor([whole_text_encoding])
98
 
99
+ with torch.no_grad():
100
+ outputs = model(tokens_tensor)
101
+ predictions = outputs[0]
102
+
103
+ logprobs = []
104
+ start = -1-len(cw_encoding)
105
+ for j in range(start,-1,1):
106
+ raw_output = []
107
+ for i in predictions[-1][j]:
108
+ raw_output.append(i.item())
109
 
110
+ logprobs.append(np.log(softmax(raw_output)))
111
 
112
 
113
+ conditional_probs = []
114
+ for cw,prob in zip(cw_encoding,logprobs):
115
+ conditional_probs.append(prob[cw])
116
 
117
 
118
+ return np.exp(np.sum(conditional_probs))
119
 
120
 
121
 
 
145
  sim_w = get_sim(sim_w)
146
 
147
 
148
+ #LM_man = sentence_prob_mean(caption_man)
149
+ #LM_woman = sentence_prob_mean(caption_woman)
150
 
151
+ LM_man = cloze_prob(caption_man)
152
+ LM_woman = cloze_prob(caption_woman)
153
 
154
 
155