|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2018 Artit Wangperawong [email protected] |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +r"""Data generators for the Mathematical Language Understanding dataset. |
| 17 | +
|
| 18 | +The training and test data were generated by assigning symbolic variables |
| 19 | +either positive or negative decimal integers and then describing the algebraic |
| 20 | +operation to perform. We restrict our variable assignments to the range |
| 21 | +x,y->[-1000,1000) and the operations to the set {+,-,*}. To ensure that the |
| 22 | +model embraces symbolic variables, the order in which x and y appears in the |
| 23 | +expression is randomly chosen. For instance, an input string contrasting from |
| 24 | +the example shown above might be y=129,x=531,x-y. Each input string is |
| 25 | +accompanied by its target string, which is the evaluation of the mathematical |
| 26 | +expression. For this study, all targets considered are decimal integers |
| 27 | +represented at the character level. About 12 million unique samples were thus |
| 28 | +generated and randomly split into training and test sets at an approximate |
| 29 | +ratio of 9:1, respectively. |
| 30 | +
|
| 31 | +For more information check the following paper: |
| 32 | +Artit Wangperawong. Attending to Mathematical Language with Transformers, |
| 33 | +arXiv:1812.02825. |
| 34 | +Available at: https://arxiv.org/abs/1812.02825 |
| 35 | +
|
| 36 | +""" |
| 37 | + |
| 38 | +from __future__ import absolute_import |
| 39 | +from __future__ import division |
| 40 | +from __future__ import print_function |
| 41 | + |
| 42 | +import os |
| 43 | + |
| 44 | +from tensor2tensor.data_generators import generator_utils |
| 45 | +from tensor2tensor.data_generators import problem |
| 46 | +from tensor2tensor.data_generators import text_problems |
| 47 | +from tensor2tensor.utils import registry |
| 48 | + |
| 49 | +import tensorflow as tf |
| 50 | + |
| 51 | +@registry.register_problem |
| 52 | +class MathematicalLanguageUnderstanding(text_problems.Text2TextProblem): |
| 53 | + URL = "https://art.wangperawong.com/mathematical_language_understanding_train.tar.gz" |
| 54 | + |
| 55 | + @property |
| 56 | + def vocab_type(self): |
| 57 | + return text_problems.VocabType.CHARACTER |
| 58 | + |
| 59 | + @property |
| 60 | + def dataset_splits(self): |
| 61 | + return [{ |
| 62 | + "split": problem.DatasetSplit.TRAIN, |
| 63 | + "shards": 10, |
| 64 | + }, { |
| 65 | + "split": problem.DatasetSplit.EVAL, |
| 66 | + "shards": 1, |
| 67 | + }] |
| 68 | + |
| 69 | + @property |
| 70 | + def is_generate_per_split(self): |
| 71 | + return False |
| 72 | + |
| 73 | + def generate_samples(self, data_dir, tmp_dir, dataset_split): |
| 74 | + """Downloads and extracts the dataset and generates examples |
| 75 | +
|
| 76 | + Args: |
| 77 | + tmp_dir: temp directory to download and extract the dataset |
| 78 | + data_dir: The base directory where data and vocab files are stored. |
| 79 | +
|
| 80 | + Returns: |
| 81 | + data generator |
| 82 | + """ |
| 83 | + |
| 84 | + if not tf.gfile.Exists(tmp_dir): |
| 85 | + tf.gfile.MakeDirs(tmp_dir) |
| 86 | + |
| 87 | + if not tf.gfile.Exists(data_dir): |
| 88 | + tf.gfile.MakeDirs(data_dir) |
| 89 | + |
| 90 | + # Download and extract |
| 91 | + compressed_filename = os.path.basename(self.URL) |
| 92 | + download_path = generator_utils.maybe_download(tmp_dir, compressed_filename, |
| 93 | + self.URL) |
| 94 | + |
| 95 | + with tarfile.open(download_path, "r:gz") as tar: |
| 96 | + tar.extractall(tmp_dir) |
| 97 | + |
| 98 | + filepath = os.path.join(tmp_dir, "mathematical_language_understanding_train.txt") |
| 99 | + |
| 100 | + with open(filepath, 'r') as fp: |
| 101 | + for l in fp: |
| 102 | + prob, ans = l.strip().split(':') |
| 103 | + yield {"inputs": prob, "targets": ans} |
| 104 | + |
0 commit comments