| 98 | return k, golden, np.array(R) |
| 99 | |
| 100 | def predict(text): |
| 101 | k, golden, R = encode(text) |
| 102 | sample = np.reshape(R, (1, config.MAX_LEN)) |
| 103 | #print("sample.shape:", sample.shape) #(1, 256) |
| 104 | prediction = mlm_model.predict(sample) |
| 105 | #print("prediction.shape:", prediction.shape) #(1, 256, 512) |
| 106 | |
| 107 | # position of token2id['[mask]'] in list: |
| 108 | masked_index = k |
| 109 | #print("masked_index:", masked_index) |
| 110 | # all substitute word probabilities: |
| 111 | mask_prediction = prediction[0][masked_index] |
| 112 | # word indices with top-k highest probabilities: |
| 113 | top_k = 5 |
| 114 | # Trick: negate array so order is reversed: |
| 115 | #top_indices = (-mask_prediction).argsort()[0:top_k] |
| 116 | top_indices = mask_prediction.argsort()[-top_k:][::-1] |
| 117 | # probabilities of the top_k |
| 118 | values = mask_prediction[top_indices] |
| 119 | correct_top1 = top_indices[0] == golden |
| 120 | correct_top5 = False |
| 121 | for i in range(len(top_indices)): |
| 122 | if top_indices[i] == golden: |
| 123 | correct_top5 = True |
| 124 | break |
| 125 | return correct_top1, correct_top5 |
| 126 | |
| 127 | # enumerate all tests |
| 128 | correct_top1 = 0 |