Skip to content

Commit

Permalink
validation checks for memory nets and reading comprehension
Browse files Browse the repository at this point in the history
  • Loading branch information
bethke committed May 14, 2018
1 parent bf49e20 commit a44ddd4
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 67 deletions.
2 changes: 0 additions & 2 deletions examples/kvmemn2n/requirements.txt

This file was deleted.

14 changes: 10 additions & 4 deletions examples/kvmemn2n/train_kvmemn2n.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

from nlp_architect.models.kvmemn2n import KVMemN2N
from nlp_architect.data.wikimovies import WIKIMOVIES
from nlp_architect.utils.io import validate, validate_existing_directory, \
validate_existing_filepath, validate_parent_exists, check_size
from interactive_util import interactive_loop

from tqdm import tqdm
Expand All @@ -37,10 +39,13 @@
# parse the command line arguments
parser = NgraphArgparser(__doc__)
parser.add_argument('--emb_size', type=int, default='50',
help='Size of the word-embedding used in the model. (default 50)')
help='Size of the word-embedding used in the model. (default 50)',
action=check_size(1, 20000))
parser.add_argument('--nhops', type=int, default='3',
help='Number of memory hops in the network')
parser.add_argument('--lr', type=float, default=0.01, help='learning rate')
help='Number of memory hops in the network',
action=check_size(1, 20))
parser.add_argument('--lr', type=float, default=0.01, help='learning rate',
action=check_size(0, 5))
parser.add_argument('--subset', type=str, default='wiki-entities',
choices=['full', 'wiki-entities'],
help='wikiMovies dataset to use for training examples.')
Expand All @@ -51,7 +56,8 @@
parser.add_argument('--use_v_luts', action="store_true",
help="Run the model using separate value lookup tables for each hop")
parser.add_argument('--model_file', type=str, default=None,
help="File to save or load weights from")
help="File to save or load weights from",
action=validate_parent_exists)
parser.add_argument('--inference', action="store_true", help="Run Inference with loaded weight")
parser.add_argument('--restore', action="store_true",
help="Run the model restoring weights from model_file")
Expand Down
18 changes: 13 additions & 5 deletions examples/kvmemn2n/wikiwindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
from multiprocessing import Process, Queue, Condition, Value
import os

from nlp_architect.utils.io import validate, validate_existing_directory, \
validate_existing_filepath, validate_parent_exists, check_size

'''
Things that were changed from the original:
1) Entities are replaced by their keys both in the key and the value (center word)
Expand Down Expand Up @@ -120,14 +123,18 @@
description='Generates windowed examples for wikipedia files. By default,' +
' creates pairs of window<TAB>entity when used with entities.'
)
parser.add_argument('data_dir', type=str, help='name of root directory for files')
parser.add_argument('data_dir', type=str, help='name of root directory for files',
action=validate_existing_directory)
# parser.add_argument('input_file', type=str, nargs='+',
# help='name of a input file in memnns format')
parser.add_argument('-o', '--output_file', type=str,
help='name of a output file, otherwise data_dir will be used')
parser.add_argument('-n', type=int, help='Max number of examples to process.')
help='name of a output file, otherwise data_dir will be used',
action=validate_parent_exists)
parser.add_argument('-n', type=int, help='Max number of examples to process.',
action=check_size(1, 100000000))
parser.add_argument('-e', '--entities', type=str,
help='entities file (each line specifies ngrams to always chunk together)')
help='entities file (each line specifies ngrams to always chunk together)',
action=validate_existing_filepath)
parser.add_argument('-a', '--all_windows', action='store_true',
help='if set, keeps all windows (not just ones entities). defaults to ' +
' True if entities file not present, False if it is present.')
Expand All @@ -150,7 +157,8 @@
'sentence "hello world how are things" creates a window of "2:hello ' +
'1:world <NULL> 1:are 2:things"')
parser.add_argument('-t', '--num_threads', type=int, default=4,
help='number of threads to use')
help='number of threads to use',
action=check_size(1,20))
args = vars(parser.parse_args())

beg = time.time()
Expand Down
14 changes: 10 additions & 4 deletions examples/memn2n_dialogue/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
from nlp_architect.models.memn2n_dialogue import MemN2N_Dialog
from memn2n_dialogue.utils import interactive_loop
from nlp_architect.utils.io import sanitize_path
from nlp_architect.utils.io import validate, validate_existing_directory, \
validate_existing_filepath, validate_parent_exists, check_size

# parse the command line arguments
parser = NgraphArgparser(__doc__)
Expand All @@ -67,9 +69,11 @@
'--emb_size',
type=int,
default='32',
help='Size of the word-embedding used in the model. (default 128)')
help='Size of the word-embedding used in the model. (default 128)',
action=check_size(1, 2000))
parser.add_argument('--nhops', type=int, default='3',
help='Number of memory hops in the network')
help='Number of memory hops in the network',
action=check_size(1, 20))
parser.add_argument(
'--use_match_type',
default=False,
Expand All @@ -94,11 +98,13 @@
'--eps',
type=float,
default=1e-8,
help='epsilon used to avoid divide by zero in softmax renormalization.')
help='epsilon used to avoid divide by zero in softmax renormalization.',
action=check_size(1e-100,1e-2))
parser.add_argument(
'--model_file',
default='memn2n_weights.npz',
help='File to load model weights from.')
help='File to load model weights from.',
action=validate_existing_filepath)

parser.set_defaults(batch_size=32, epochs=200)
args = parser.parse_args()
Expand Down
6 changes: 0 additions & 6 deletions examples/memn2n_dialogue/requirements.txt

This file was deleted.

24 changes: 16 additions & 8 deletions examples/memn2n_dialogue/train_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
from tqdm import tqdm
from utils import interactive_loop
from nlp_architect.utils.io import sanitize_path

from nlp_architect.utils.io import validate, validate_existing_directory, \
validate_existing_filepath, validate_parent_exists, check_size

# parse the command line arguments
parser = NgraphArgparser(__doc__)
Expand All @@ -69,9 +70,11 @@
'--emb_size',
type=int,
default='32',
help='Size of the word-embedding used in the model. (default 128)')
help='Size of the word-embedding used in the model. (default 32)',
action=check_size(1, 2000))
parser.add_argument('--nhops', type=int, default='3',
help='Number of memory hops in the network')
help='Number of memory hops in the network',
action=check_size(1, 20))
parser.add_argument(
'--use_match_type',
default=False,
Expand All @@ -97,12 +100,14 @@
'--grad_clip_norm',
type=float,
default=40.0,
help='Clip gradients such that norm is below this value.')
help='Clip gradients such that norm is below this value.',
action=check_size(0,100))
parser.add_argument(
'--eps',
type=float,
default=1e-8,
help='epsilon used to avoid divide by zero in softmax renormalization.')
help='epsilon used to avoid divide by zero in softmax renormalization.',
action=check_size(1e-100,1e-2))
parser.add_argument(
'--save_log',
action='store_true',
Expand All @@ -112,17 +117,20 @@
'--log_file',
type=str,
default='memn2n_dialgoue_results.txt',
help='File to write evaluation set results to.')
help='File to write evaluation set results to.',
action=validate_parent_exists)
parser.add_argument(
'--weights_save_path',
type=str,
default='memn2n_weights.npz',
help='File to save model weights to.')
help='File to save model weights to.',
action=validate_parent_exists)
parser.add_argument(
'--save_epochs',
type=int,
default=1,
help='Number of epochs between saving model weights.')
help='Number of epochs between saving model weights.',
action=check_size(1,1000))
parser.add_argument(
'--restore',
default=False,
Expand Down
8 changes: 6 additions & 2 deletions examples/reading_comprehension/prepare_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import os
from tqdm import *
from nlp_architect.utils.io import sanitize_path
from nlp_architect.utils.io import validate, validate_existing_directory, \
validate_existing_filepath, validate_parent_exists, check_size

PAD = "<pad>"
SOS = "<sos>"
Expand Down Expand Up @@ -199,13 +201,15 @@ def get_ids_list(data_lists, vocab):
parser.add_argument(
'--data_path',
default='',
help='enter path where training data and the glove embeddings were downloaded')
help='enter path where training data and the glove embeddings were downloaded',
action=validate_existing_directory)

parser.add_argument(
'--preprocess_glove',
type=int,
default=1,
help='Chose whether or not to preprocess glove embeddings')
help='Chose whether or not to preprocess glove embeddings',
action=check_size(0,1))

parser.set_defaults()

Expand Down
17 changes: 11 additions & 6 deletions examples/reading_comprehension/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
get_data_array_squad_ngraph,
cal_f1_score)
import math
from nlp_architect.utils.weight_initilizers import (make_placeholder, make_weights)
from nlp_architect.contrib.ngraph.weight_initilizers import (make_placeholder, make_weights)
from nlp_architect.utils.io import sanitize_path

from nlp_architect.utils.io import validate, validate_existing_directory, \
validate_existing_filepath, validate_parent_exists, check_size

"""
Model converges on gpu backened.
Expand All @@ -55,18 +56,22 @@

parser.add_argument(
'--data_path',
help='enter path for training data')
help='enter path for training data',
action=validate_existing_directory)

parser.add_argument('--gpu_id', default="0",
help='enter gpu id')
help='enter gpu id',
action=check_size(0,10))

parser.add_argument('--max_para_req', default=100,
help='enter the max length of paragraph')
help='enter the max length of paragraph',
action=check_size(0,10000))

parser.add_argument(
'--batch_size_squad',
default=16,
help='enter the batch size') # 16 is the max batch size o be fit on gpu
help='enter the batch size',
action=check_size(1,10000)) # 16 is the max batch size o be fit on gpu

parser.set_defaults()

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion nlp_architect/models/kvmemn2n.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from ngraph.frontends.neon import Layer
from ngraph.frontends.neon import GaussianInit
from ngraph.frontends.neon.graph import SubGraph
from nlp_architect.utils.encodings import position_encoding
from nlp_architect.contrib.ngraph.encodings import position_encoding
from nlp_architect.contrib.ngraph.modified_lookup_table import ModifiedLookupTable


Expand Down
32 changes: 5 additions & 27 deletions examples/memn2n_dialogue/tests.py → tests/test_memn2n_dialogue.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,13 @@
from ngraph.frontends.neon import ax
import pytest

pytestmark = pytest.mark.transformer_dependent


rng = RandomTensorGenerator(0, np.float32)

delta = 1e-8
rtol = atol = 1e-2


def pytest_generate_tests(metafunc):
bsz_rng = [1]

if 'lut_args' in metafunc.fixturenames:
fargs = []
vocab_rng = [10, 50, 100]
embed_rng = [20, 18, 31]
# Add third axis (memory size) to test multiaxis LUT
mem_rng = [1, 5, 19]
bsz_rng = [4, 32]
seq_rng = [3, 5]
fargs = itt.product(vocab_rng, embed_rng, bsz_rng, seq_rng, mem_rng)
metafunc.parametrize('lut_args', fargs)

(V, F, N, T, M) = (3, 6, 4, 5, 7)
lut_args = (V, F, N, T, M)

def lut_fprop_ref(lut, idx):
"""
Expand All @@ -71,7 +55,9 @@ def lut_update_ref(error, lut, idx, pad_idx):
dw_ref[wrd_id, :] = np.sum(error.take(group[0], axis=0), axis=0)
return dw_ref


@pytest.mark.parametrize('show_tok', [True, False])
@pytest.mark.parametrize('show_doc', [True, False])
@pytest.mark.parametrize('lut_args', [lut_args])
def test_lut(lut_args):
"""
test lut fprop and bprop
Expand Down Expand Up @@ -127,11 +113,3 @@ def test_lut(lut_args):
pad_idx=pad_idx)
ng.testing.assert_allclose(
update_lut, update_ref, rtol=0.0, atol=1.0e-5)


if __name__ == '__main__':
factory = ngt.make_transformer_factory('cpu')
ngt.set_transformer_factory(factory)
(V, F, N, T, M) = (3, 6, 4, 5, 7)
lut_args = (V, F, N, T, M)
test_lut(lut_args)
3 changes: 1 addition & 2 deletions tests/test_np_semantic_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_model_training():
Test model end2end training
"""
data_path = os.path.abspath('fixtures/data/np_semantic_segmentation_prepared_data.csv')
model_path = os.path.abspath('fixtures/data/np_semantic_segmentation')
model_path = os.path.abspath('fixtures/data/np_semantic_segmentation.prm')
num_epochs = 200
be = gen_backend(batch_size=64)
# load data sets from file
Expand Down Expand Up @@ -45,4 +45,3 @@ def test_model_inference():
assert len(output_reader_list) == len(input_reader_list) - 1
os.remove(model_path)
os.remove(output_path)

0 comments on commit a44ddd4

Please sign in to comment.