| 151 | output.flush() |
| 152 | |
| 153 | def _visualize_attention(self, model, beam_size, output_history, sliced_data, res1file, res2file, res3file, output): |
| 154 | res1 = json.load(open(res1file, 'r')) |
| 155 | res1 = res1['per_item'] |
| 156 | res2 = json.load(open(res2file, 'r')) |
| 157 | res2 = res2['per_item'] |
| 158 | res3 = json.load(open(res3file, 'r')) |
| 159 | res3 = res3['per_item'] |
| 160 | interest_cnt = 0 |
| 161 | cnt = 0 |
| 162 | for i, item in enumerate(tqdm.tqdm(sliced_data)): |
| 163 | |
| 164 | if res1[i]['hardness'] != 'extra': |
| 165 | continue |
| 166 | |
| 167 | cnt += 1 |
| 168 | if (res1[i]['exact'] == 0) and (res2[i]['exact'] == 0) and (res3[i]['exact'] == 0): |
| 169 | continue |
| 170 | interest_cnt += 1 |
| 171 | ''' |
| 172 | print('sample index: ') |
| 173 | print(i) |
| 174 | beams = beam_search.beam_search( |
| 175 | model, item, beam_size=beam_size, max_steps=1000, visualize_flag=True) |
| 176 | entry = item.orig |
| 177 | print('ground truth SQL:') |
| 178 | print(entry['query_toks']) |
| 179 | print('prediction:') |
| 180 | print(res2[i]) |
| 181 | decoded = [] |
| 182 | for beam in beams: |
| 183 | model_output, inferred_code = beam.inference_state.finalize() |
| 184 | |
| 185 | decoded.append({ |
| 186 | 'model_output': model_output, |
| 187 | 'inferred_code': inferred_code, |
| 188 | 'score': beam.score, |
| 189 | **({ |
| 190 | 'choice_history': beam.choice_history, |
| 191 | 'score_history': beam.score_history, |
| 192 | } if output_history else {})}) |
| 193 | |
| 194 | output.write( |
| 195 | json.dumps({ |
| 196 | 'index': i, |
| 197 | 'beams': decoded, |
| 198 | }) + '\n') |
| 199 | output.flush() |
| 200 | ''' |
| 201 | print(interest_cnt * 1.0 / cnt) |
| 202 | |
| 203 | |
| 204 | def add_parser(): |