(challenge_type)
| 157 | # we want to load both the single supporting fact data |
| 158 | # and the two supporting fact data later |
| 159 | def get_data(challenge_type): |
| 160 | # input should either be 'single_supporting_fact_10k' or 'two_supporting_facts_10k' |
| 161 | challenge = challenges[challenge_type] |
| 162 | |
| 163 | |
| 164 | # returns a list of triples of: |
| 165 | # (story, question, answer) |
| 166 | # story is a list of sentences |
| 167 | # question is a sentence |
| 168 | # answer is a word |
| 169 | train_stories = get_stories(tar.extractfile(challenge.format('train'))) |
| 170 | test_stories = get_stories(tar.extractfile(challenge.format('test'))) |
| 171 | |
| 172 | |
| 173 | # group all the stories together |
| 174 | stories = train_stories + test_stories |
| 175 | |
| 176 | # so we can get the max length of each story, of each sentence, and of each question |
| 177 | story_maxlen = max((len(s) for x, _, _ in stories for s in x)) |
| 178 | story_maxsents = max((len(x) for x, _, _ in stories)) |
| 179 | query_maxlen = max(len(x) for _, x, _ in stories) |
| 180 | |
| 181 | # Create vocabulary of corpus and find size, including a padding element. |
| 182 | vocab = sorted(set(flatten(stories))) |
| 183 | vocab.insert(0, '<PAD>') |
| 184 | vocab_size = len(vocab) |
| 185 | |
| 186 | # Create an index mapping for the vocabulary. |
| 187 | word2idx = {c:i for i, c in enumerate(vocab)} |
| 188 | |
| 189 | # convert stories from strings to lists of integers |
| 190 | inputs_train, queries_train, answers_train = vectorize_stories( |
| 191 | train_stories, |
| 192 | word2idx, |
| 193 | story_maxlen, |
| 194 | query_maxlen |
| 195 | ) |
| 196 | inputs_test, queries_test, answers_test = vectorize_stories( |
| 197 | test_stories, |
| 198 | word2idx, |
| 199 | story_maxlen, |
| 200 | query_maxlen |
| 201 | ) |
| 202 | |
| 203 | # convert inputs into 3-D numpy arrays |
| 204 | inputs_train = stack_inputs(inputs_train, story_maxsents, story_maxlen) |
| 205 | inputs_test = stack_inputs(inputs_test, story_maxsents, story_maxlen) |
| 206 | print("inputs_train.shape, inputs_test.shape", inputs_train.shape, inputs_test.shape) |
| 207 | |
| 208 | |
| 209 | # return model inputs for keras |
| 210 | return train_stories, test_stories, \ |
| 211 | inputs_train, queries_train, answers_train, \ |
| 212 | inputs_test, queries_test, answers_test, \ |
| 213 | story_maxsents, story_maxlen, query_maxlen, \ |
| 214 | vocab, vocab_size |
| 215 | |
| 216 |
no test coverage detected