Example #1
0
def init(data_dir, model_file):
    """ Initialize web app """
    global memn2n, test_story, test_questions, test_qstory

    # Try to load model
    memn2n = MemN2N(data_dir, model_file)
    memn2n.load_model()

    # SV create word vectors for all the dictionary words
    print("Embedding dictionary words")
    for word in memn2n.general_config.dictionary.keys():
        # SV assign vector to each word in the dictionary
        memn2n.dict_vectors[word] = memn2n.word_model[word]

    # Read test data
    # test_data_path = glob.glob('%s/qa*_*_test.txt' % memn2n.data_dir)
    test_data_path  = glob.glob(memn2n.data_path.format('test'))
    # load different dataset with samples, e.g. real table data
    # test_data_path  = glob.glob('./data/table_data_{}.txt'.format('test'))
    print("Reading test data from %s ..." % test_data_path)

    # test_story, test_questions, test_qstory = \
    #     parse_babi_task(test_data_path, memn2n.general_config.dictionary, False)
    # update dictionary with new words
    test_story, test_questions, test_qstory = \
        memn2n.parse_babi_task(test_data_path, False)
    
    # SV expand reversed_dict with test data
    print len(memn2n.general_config.dictionary)
    # Get reversed dictionary mapping index to word
    memn2n.reversed_dict = dict((ix, w) for w, ix in memn2n.general_config.dictionary.items())
Example #2
0
def init(data_dir, model_file):
    """ Initialize web app """
    global memn2n, test_story, test_questions, test_qstory

    # Try to load model
    memn2n = MemN2N(data_dir, model_file)
    memn2n.load_model()

    # Read test data
    print("Reading test data from %s ..." % memn2n.data_dir)
    test_data_path = glob.glob('%s/qa*_*_test.txt' % memn2n.data_dir)
    test_story, test_questions, test_qstory = \
        parse_babi_task(test_data_path, memn2n.general_config.dictionary, False)
Example #3
0
def predict_d():
    word_idx = pickle.load(open(model_dir + 'word_idx.pkl', 'rb'))
    import glob
    import numpy as np

    from demo.qa import MemN2N
    from util import parse_babi_task
    """ Initialize app """
    global memn2n, test_story, test_questions, test_qstory
    model_file = 'trained_model/memn2n_model.pklz'

    # Try to load model
    memn2n = MemN2N(data_dir, model_file)
    memn2n.load_model()
    test_story, test_questions, test_qstory = None, None, None
    # Read test data
    print("Reading test data from %s ..." % memn2n.data_dir)
    test_data_path = glob.glob('%s/qa*_*_test.txt' % memn2n.data_dir)
    test_story, test_questions, test_qstory = \
        parse_babi_task(test_data_path, memn2n.general_config.dictionary, False)
    question_idx = np.random.randint(test_questions.shape[1])
    story_idx = test_questions[0, question_idx]
    last_sentence_idx = test_questions[1, question_idx]

    story_txt, question_txt, correct_answer = memn2n.get_story_texts(
        test_story, test_questions, test_qstory, question_idx, story_idx,
        last_sentence_idx)
    # Format text
    story_txt = "\n".join(story_txt)
    question_txt += "?"

    print('Case study:')
    print('Story:', story_txt, '\nQuestion:', question_txt, '\nAnswer:',
          correct_answer)

    model = tf.keras.models.load_model(model_dir)
    predict_answer = model.predict([
        vectorize(story_txt, word_idx, 552),
        vectorize(question_txt, word_idx, 5)
    ])
    print(list(word_idx.keys())[np.argmax(predict_answer, axis=1)[0] - 1])
            train_path = os.path.join(args.data_dir2, 'train')
            if not os.path.exists(train_path):
                print("'%s' does not exist." % train_path)
                sys.exit(1)
            test_path = os.path.join(args.data_dir2, 'test')
            if not os.path.exists(test_path):
                print("'%s' does not exist." % test_path)
                sys.exit(1)
            args.data_dir = train_path, test_path

    if type(args.data_dir) is tuple:
        print("Using data from {} and {}".format(args.data_dir[0],
                                                 args.data_dir[1]))
    else:
        print("Using data from %s" % args.data_dir)

    if args.test or args.all_tests:
        m = MemN2N(args.data_dir, args.model_file)
        m.load_model()
        if args.all_tests:
            run_all_tests(args.data_dir, m)
        else:
            run_test(args.data_dir, args.task, m)
    else:
        if args.all_tasks:
            run_all_tasks(data_dir)
        elif args.joint_tasks:
            run_joint_tasks(data_dir)
        else:
            run_task(data_dir, task_id=args.task)
Example #5
0
          ], [sg.Button('模型一'), sg.Button('模型二')]]
""" Initialize app """
global memn2n, test_story, test_questions, test_qstory
data_dir = 'data/tasks_1-20_v1-2/en'
model_file = 'trained_model/memn2n_model.pklz'

memn2n = None
test_story, test_questions, test_qstory = None, None, None

if __name__ == '__main__':
    # Create the Window
    window = sg.Window('babi数据集算法演示', layout)
    # Event Loop to process "events" and get the "values" of the inputs

    # Try to load model
    memn2n = MemN2N(data_dir, model_file)
    memn2n.load_model()

    # Read test data
    print("Reading test data from %s ..." % memn2n.data_dir)
    test_data_path = glob.glob('%s/qa*_*_test.txt' % memn2n.data_dir)
    test_story, test_questions, test_qstory = \
        parse_babi_task(test_data_path, memn2n.general_config.dictionary, False)

    while True:
        event, values = window.read()
        if event == sg.WIN_CLOSED:
            break

        if event == 'getstory':
            question_idx = np.random.randint(test_questions.shape[1])
Example #6
0
path_to_model_output_dir = './ExperimentA'
'''
Train, test, and save model for three different learning rates each with/without linear start 
and 1,2,3 memory modules (hops).

'''

starting_learning_rate = [.005, .1, .01]
linear_start = [True, False]
hops = [1, 2, 3]

config_swtiches = [None, None, None]

for lr in starting_learning_rate:
    config_swtiches[0] = lr
    for opt in linear_start:
        config_swtiches[1] = opt
        for num in hops:
            config_swtiches[2] = num
            model_file_name = 'lr{}_linearstart{}_hops{}'.format(
                *config_swtiches)
            write_model_to = os.path.join(path_to_model_output_dir,
                                          model_file_name)
            message = 'Training and testing model for {}'.format(
                model_file_name)
            print(message)
            model = MemN2N(path_to_babi_data, write_model_to, config_swtiches)
            model.train_and_test()
            print('\n\n')
Example #7
0
from demo.qa import MemN2N
import os
import random

# Default location for data is location on the HPCC
#path_to_babi_data = '/mnt/research/CSE842/FinalProject/Benchmarks/bAbI'
path_to_babi_data = '/home/bbaker/bAbI'

path_to_model_output_dir = './ExperimentA'
'''
Train, test, and save model from original repo configuration

'''

for i in range(10):
    seed = random.randint(1, 10000000)
    model_file_name = 'default_config_model{}_seed'.format(i)
    write_model_to = os.path.join(path_to_model_output_dir, model_file_name)
    message = 'Training and testing model for {}'.format(model_file_name)
    print(message)
    model = MemN2N(path_to_babi_data, write_model_to)
    model.train_and_test(seed)
    print('\n\n')