This repository was archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New problem: mathematical language understanding #1290
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1a80488
fix bAbi data generator and readme
artitw ae57670
Fix bAbi hparams deletion
artitw 5806b38
Fix bAbi hparams delete unecessary keys
artitw 4dcec1c
Fix bAbi hparams clean keys
artitw 46963d0
bAbi hparams delete keys
artitw daf36db
fix merge conflict
artitw 4b58a34
fix readme
artitw a7b3f7e
fix merge conflict
artitw 53be905
fix universal transformer decoding
artitw 3c55a1a
fix merge conflict
artitw 7533dab
fix merge conflict
artitw 1d5d27a
mathematical language understanding
artitw c68fc61
clarify usage
artitw 03aff7a
add to authors
artitw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
# of contributors, see the revision history in source control. | ||
|
||
Google Inc. | ||
Artit Wangperawong |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
tensor2tensor/data_generators/mathematical_language_understanding.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# coding=utf-8 | ||
# Copyright 2018 Artit Wangperawong artitw@gmail.com | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
r"""Data generators for the Mathematical Language Understanding dataset. | ||
|
||
The training and test data were generated by assigning symbolic variables | ||
either positive or negative decimal integers and then describing the algebraic | ||
operation to perform. We restrict our variable assignments to the range | ||
x,y->[-1000,1000) and the operations to the set {+,-,*}. To ensure that the | ||
model embraces symbolic variables, the order in which x and y appears in the | ||
expression is randomly chosen. For instance, an input string contrasting from | ||
the example shown above might be y=129,x=531,x-y. Each input string is | ||
accompanied by its target string, which is the evaluation of the mathematical | ||
expression. For this study, all targets considered are decimal integers | ||
represented at the character level. About 12 million unique samples were thus | ||
generated and randomly split into training and test sets at an approximate | ||
ratio of 9:1, respectively. | ||
|
||
For more information check the following paper: | ||
Artit Wangperawong. Attending to Mathematical Language with Transformers, | ||
arXiv:1812.02825. | ||
Available at: https://arxiv.org/abs/1812.02825 | ||
|
||
""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import os | ||
|
||
from tensor2tensor.data_generators import generator_utils | ||
from tensor2tensor.data_generators import problem | ||
from tensor2tensor.data_generators import text_problems | ||
from tensor2tensor.utils import registry | ||
|
||
import tensorflow as tf | ||
|
||
@registry.register_problem | ||
class MathematicalLanguageUnderstanding(text_problems.Text2TextProblem): | ||
URL = "https://art.wangperawong.com/mathematical_language_understanding_train.tar.gz" | ||
|
||
@property | ||
def vocab_type(self): | ||
return text_problems.VocabType.CHARACTER | ||
|
||
@property | ||
def dataset_splits(self): | ||
return [{ | ||
"split": problem.DatasetSplit.TRAIN, | ||
"shards": 10, | ||
}, { | ||
"split": problem.DatasetSplit.EVAL, | ||
"shards": 1, | ||
}] | ||
|
||
@property | ||
def is_generate_per_split(self): | ||
return False | ||
|
||
def generate_samples(self, data_dir, tmp_dir, dataset_split): | ||
"""Downloads and extracts the dataset and generates examples | ||
|
||
Args: | ||
tmp_dir: temp directory to download and extract the dataset | ||
data_dir: The base directory where data and vocab files are stored. | ||
|
||
Returns: | ||
data generator | ||
""" | ||
|
||
if not tf.gfile.Exists(tmp_dir): | ||
tf.gfile.MakeDirs(tmp_dir) | ||
|
||
if not tf.gfile.Exists(data_dir): | ||
tf.gfile.MakeDirs(data_dir) | ||
|
||
# Download and extract | ||
compressed_filename = os.path.basename(self.URL) | ||
download_path = generator_utils.maybe_download(tmp_dir, compressed_filename, | ||
self.URL) | ||
|
||
with tarfile.open(download_path, "r:gz") as tar: | ||
tar.extractall(tmp_dir) | ||
|
||
filepath = os.path.join(tmp_dir, "mathematical_language_understanding_train.txt") | ||
|
||
with open(filepath, 'r') as fp: | ||
for l in fp: | ||
prob, ans = l.strip().split(':') | ||
yield {"inputs": prob, "targets": ans} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.