(we_file, w2i_file, use_brown=True, n_files=50)
| 179 | |
| 180 | |
| 181 | def main(we_file, w2i_file, use_brown=True, n_files=50): |
| 182 | if use_brown: |
| 183 | cc_matrix = "cc_matrix_brown.npy" |
| 184 | else: |
| 185 | cc_matrix = "cc_matrix_%s.npy" % n_files |
| 186 | |
| 187 | # hacky way of checking if we need to re-load the raw data or not |
| 188 | # remember, only the co-occurrence matrix is needed for training |
| 189 | if os.path.exists(cc_matrix): |
| 190 | with open(w2i_file) as f: |
| 191 | word2idx = json.load(f) |
| 192 | sentences = [] # dummy - we won't actually use it |
| 193 | else: |
| 194 | if use_brown: |
| 195 | keep_words = set([ |
| 196 | 'king', 'man', 'woman', |
| 197 | 'france', 'paris', 'london', 'rome', 'italy', 'britain', 'england', |
| 198 | 'french', 'english', 'japan', 'japanese', 'chinese', 'italian', |
| 199 | 'australia', 'australian', 'december', 'november', 'june', |
| 200 | 'january', 'february', 'march', 'april', 'may', 'july', 'august', |
| 201 | 'september', 'october', |
| 202 | ]) |
| 203 | sentences, word2idx = get_sentences_with_word2idx_limit_vocab(n_vocab=5000, keep_words=keep_words) |
| 204 | else: |
| 205 | sentences, word2idx = get_wikipedia_data(n_files=n_files, n_vocab=2000) |
| 206 | |
| 207 | with open(w2i_file, 'w') as f: |
| 208 | json.dump(word2idx, f) |
| 209 | |
| 210 | V = len(word2idx) |
| 211 | model = Glove(100, V, 10) |
| 212 | model.fit( |
| 213 | sentences, |
| 214 | cc_matrix=cc_matrix, |
| 215 | learning_rate=1e-4, |
| 216 | reg=0.1, |
| 217 | epochs=200, |
| 218 | ) |
| 219 | model.save(we_file) |
| 220 | |
| 221 | |
| 222 | if __name__ == '__main__': |
no test coverage detected