Пример #1
0
def train_npi(
    steplists,
    epochs: int = 100,
    batch_size: int = 1,
    pretrained_encoder_weights: str = f'{config.outdir}/weights/f_enc.weights'
):
    state_encoder = StateEncoder().to(config.device)
    warm_up: list = list(
        filter(
            lambda question: 0 <= question['question'][0] < 100 and 0 <=
            question['question'][1] < 100, steplists))
    if not os.path.exists(pretrained_encoder_weights):
        print('start trainning f_enc model')
        train_f_enc(warm_up, epochs=epochs, batch_size=batch_size)
    else:
        state_encoder.load_state_dict(torch.load(pretrained_encoder_weights))

    state_encoder.trainable = False

    npi = NPI(state_encoder).to(config.device)
    optimizer = optim.Adam(npi.parameters(), lr=1e-4, weight_decay=1e-6)

    # warm up with single digit addjj
    for _ in range(10):
        if train_with_plot(npi, optimizer, warm_up, epochs=100):
            break
    while True:
        if train_with_plot(npi,
                           optimizer,
                           steplists,
                           epochs=100,
                           skip_correct=False):
            break
Пример #2
0
def valid_npi(questions: list, pretrained_encoder_weights: str,
              pretrained_npi_weights: str):
    state_encoder = StateEncoder().to(config.device)
    state_encoder.load_state_dict(torch.load(pretrained_encoder_weights))
    npi = NPI(state_encoder, max_depth=20, max_steps=10000).to(config.device)
    npi.load_state_dict(torch.load(pretrained_npi_weights))
    env = AdditionEnv()
    add_program: dict = {'pgid': 2, 'args': []}
    wc: int = 0
    correct: int = 0
    npi.eval().to(config.device)
    loop = tqdm(questions, postfix='correct: {correct} wrong: {wrong}')
    for addend, augend in loop:
        npi.reset()
        with torch.no_grad():
            env.setup(addend, augend)
            # run npi algorithm
            npi.step(env, add_program['pgid'], add_program['args'])
        if env.result != (addend + augend):
            wc += 1
            loop.write('{:>5} + {:>5} = {:>5}'.format(addend, augend,
                                                      env.result))
        else:
            correct += 1
        loop.set_postfix(correct=correct, wrong=wc)
    return correct, wc
Пример #3
0
def test_question(question: list, npi: NPI) -> int:
    env = AdditionEnv()
    addend, augend = question
    add_program: dict = {'pgid': 2, 'args': []}
    npi.reset()
    with torch.no_grad():
        env.setup(addend, augend)
        # run npi algorithm
        npi.step(env, add_program['pgid'], add_program['args'])
        # get environment observation
        return env.result
def evaluate_addition():
    """
    Load NPI Model from Checkpoint, and initialize REPL, for interactive carry-addition.
    """
    with tf.Session() as sess:
        # Load Data
        with open(DATA_PATH_TEST, 'rb') as f:
            data = pickle.load(f)

        # Initialize Addition Core
        core = AdditionCore()

        # Initialize NPI Model
        npi = NPI(core, CONFIG, LOG_PATH)

        # Restore from Checkpoint
        saver = tf.train.Saver()
        saver.restore(sess, CKPT_PATH)

        # with gfile.FastGFile("/tmp/tf/log/graph.pb", 'rb') as f:
        #     graph_def = tf.GraphDef()
        #     graph_def.ParseFromString(f.read())
        #     sess.graph.as_default()
        #     tf.import_graph_def(graph_def)
        # print("map variables")

        # Run REPL
        for x in range(0, 20):
            res = ""
            # try:
            repl(sess, npi, data, x)
            # except:
            print("--------------------------")
Пример #5
0
def train_with_plot(npi: NPI,
                    optimizer,
                    steplists: list,
                    epochs: int = 100,
                    skip_correct: bool = False):
    arg_num, arg_depth = config.arg_shape
    train_loss: list = []
    valid_loss: list = []
    # scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=1e-1, last_epoch=-1)
    # scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=1e-1, patience=2)
    for epoch in range(epochs):
        npi.train().to(config.device)
        # initialize corrent / wrong count
        losses: list = []
        # np.random.shuffle(steplists)
        loop = tqdm(steplists, ncols=100)
        loop.write('epoch: {}/{}'.format(epoch + 1, epochs))
        for idx, step in enumerate(loop):
            question, trace = step['question'], step['trace']
            npi.reset()
            for env, (pgid, args), (pgid_out, args_out), term_out in trace:
                optimizer.zero_grad()
                weights = [1] + [1 if 0 <= pgid < 6 else 1e-10] + [
                    1e-10 if np.argmax(arg) == (arg_depth - 1) else 1
                    for arg in args_out
                ]
                # get environment observation
                term_pred, pgid_pred, args_pred = npi(
                    tensor(env).flatten().type(torch.FloatTensor).to(
                        config.device),
                    tensor(pgid).to(config.device),
                    tensor(args).flatten().type(torch.FloatTensor).to(
                        config.device))
                total_loss = npi_criteria(
                    term_pred,
                    tensor(term_out).type(torch.FloatTensor).to(config.device),
                    pgid_pred,
                    tensor(pgid_out).to(config.device), args_pred,
                    tensor(args_out).type(torch.FloatTensor).to(config.device),
                    weights)
                total_loss.backward()
                optimizer.step()
                losses.append(total_loss.item())
            # total_loss
            loop.set_postfix(loss=np.average(losses))
        loop.close()
        vloss, acc = validate(npi, steplists)
        valid_loss.append(vloss)
        train_loss.append(np.average(losses))
        xlabel = np.array(range(len(train_loss))) + 1
        plt.plot(xlabel, train_loss, 'b')
        plt.plot(xlabel, valid_loss, 'g')
        plt.ylabel('loss')
        plt.xlabel('epochs')
        plt.savefig(f'{config.outdir}/loss.png')
        # scheduler.step()
        loop.close()
        npi.save()
        if acc == 1.:
            return True
Пример #6
0
def validate(npi: NPI, steplists: list, epochs: int = 100):
    _, arg_depth = config.arg_shape
    env = AdditionEnv()
    valid_loss: list = []
    correct = wc = 0
    npi.eval().to(config.device)
    for step in steplists:
        question, trace = step['question'], step['trace']
        res = test_question(question, npi)
        if res == np.sum(question):
            correct += 1
        else:
            wc += 1
        npi.reset()
        with torch.no_grad():
            for env, (pgid, args), (pgid_out, args_out), term_out in trace:
                weights = [1] + [1 if 0 <= pgid < 6 else 1e-10] + [
                    1e-10 if np.argmax(arg) == (arg_depth - 1) else 1
                    for arg in args_out
                ]
                # get environment observation
                term_pred, pgid_pred, args_pred = npi(
                    tensor(env).flatten().type(torch.FloatTensor).to(
                        config.device),
                    tensor(pgid).to(config.device),
                    tensor(args).flatten().type(torch.FloatTensor).to(
                        config.device))
                total_loss = npi_criteria(
                    term_pred,
                    tensor(term_out).type(torch.FloatTensor).to(config.device),
                    pgid_pred,
                    tensor(pgid_out).to(config.device), args_pred,
                    tensor(args_out).type(torch.FloatTensor).to(config.device),
                    weights)
                valid_loss.append(total_loss.item())
    return np.average(valid_loss), correct / len(steplists)
Пример #7
0
def evaluate_addition():
    """
    Load NPI Model from Checkpoint, and initialize REPL, for interactive carry-addition.
    """
    with tf.Session() as sess:
        # Load Data
        with open(DATA_PATH_TEST, 'rb') as f:
            data = pickle.load(f)
        # Initialize Addition Core
        core = AdditionCore()

        # Initialize NPI Model
        npi = NPI(core, CONFIG, LOG_PATH)

        # Restore from Checkpoint
        saver = tf.train.Saver()
        saver.restore(sess, CKPT_PATH)

        # with gfile.FastGFile("/tmp/tf/log/graph.pb", 'rb') as f:
        #     graph_def = tf.GraphDef()
        #     graph_def.ParseFromString(f.read())
        #     sess.graph.as_default()
        #     tf.import_graph_def(graph_def)
        # print("map variables")

        # Run REPL

        predict = {};
        predict["ncw"] = 0;
        predict["ncr"] = 0;
        predict["cw"] = 0;
        predict["cr"] = 0;
        count = 0;

        #f = open('log/numbers.txt', 'r+')
        #f.truncate()

        limit = EVAL_LIMIT
        r = list(range(1000))
       # random.shuffle(r)
        for x in r:
            limit -= 1
            if limit > 0:
                res = ""
                print(repl(sess, npi, data, x, predict))
                print("predict_connect_right " + str(predict["cr"]) + " predict_connect_wrong " + str(predict["cw"]) + " predict_not_connect_right " + str(predict["ncr"]) + " predict_not_connect_wrong " + str(predict["ncw"]))
                print (str(limit)+"--------------------------")
Пример #8
0
    def __init__(self):
      with open("/root/ContextToCode/predictor/log/1class/expect_to_prog", 'r') as handle:
          self.sessions = json.load(handle)
      for key, value in self.sessions.items():
          value['session'] = tf.Session()
	  
	  # Initialize Addition Core
      core = AdditionCore(CONFIG)
 
      # Initialize NPI Model
      self.npi = NPI(core, CONFIG, LOG_PATH)
      # Restore from Checkpoint
      saver = tf.train.Saver()
	  
      for key, value in self.sessions.items():
          saver.restore(value['session'], CKPT_PATH+value['dir']+"/models/model-0006.ckpt")
          print(value)
Пример #9
0
    def __init__(self):
      self.sess1 = tf.Session()
      self.sess2 = tf.Session()
      self.sess3 = tf.Session()
      self.sess4 = tf.Session()
      self.sess5 = tf.Session() 	  

	  # Initialize Addition Core
      core = AdditionCore(CONFIG)
 
      # Initialize NPI Model
      self.npi = NPI(core, CONFIG, LOG_PATH)
      # Restore from Checkpoint
      saver = tf.train.Saver()
      saver.restore(self.sess1, CKPT_PATH_CLASS1)
      saver.restore(self.sess5, CKPT_PATH_CLASS5)
      saver.restore(self.sess3, CKPT_PATH_CLASS3)
      saver.restore(self.sess2, CKPT_PATH_CLASS2)
      saver.restore(self.sess4, CKPT_PATH_CLASS4)
Пример #10
0
def evaluate_multiplication():
    """
    Load NPI Model from Checkpoint, and initialize REPL, for interactive multiplication.
    """
    with tf.Session() as sess:
        # Load Data
        with open(TEST_PATH, 'rb') as f:
            data = pickle.load(f)

        # Initialize Multiplication Core
        core = MultiplicationCore()

        # Initialize NPI Model
        npi = NPI(core, CONFIG, LOG_PATH)

        # Restore from Checkpoint
        saver = tf.train.Saver()
        saver.restore(sess, CKPT_PATH)

        # Run REPL
        repl(sess, npi, data)
Пример #11
0
def evaluate_card_pattern_matching():
    """
    Load NPI Model from Checkpoint, and initialize REPL, for interactive carry-addition.
    """
    # Load Data
    with open(TEST_PATH, 'rb') as f:
        data = pickle.load(f)

    # Initialize Card Pattern Matching Core
    print('Initializing Card Pattern Matching Core!')
    core = CardPatternMatchingCore()

    # Initialize NPI Model
    npi = NPI(core, CONFIG, LOG_PATH)

    with tf.Session() as sess:
        # Restore from Checkpoint
        saver = tf.train.Saver()
        saver.restore(sess, CKPT_PATH)

        # Run REPL
        repl(sess, npi, data)
Пример #12
0
def multiclass_eval():
      with open(DATA_PATH_TEST, 'rb') as f:
          data = pickle.load(f)

      sess1 = tf.Session()
      sess2 = tf.Session()
      sess3 = tf.Session()

      # Initialize Addition Core
      core = AdditionCore()
      # Initialize NPI Model
      npi = NPI(core, CONFIG, LOG_PATH)

        # Restore from Checkpoint
      saver = tf.train.Saver()
    #  saver.restore(sess1, CKPT_PATH_CLASS1)
    #  saver.restore(sess2, CKPT_PATH_CLASS2)
    #  saver.restore(sess3, CKPT_PATH_CLASS3)

      predict = {};
      predict["ncw"] = 0;
      predict["ncr"] = 0;
      predict["cw"] = 0;
      predict["cr"] = 0;

      f = open('log/prog_produced.txt', 'w+')
      f.truncate()

      limit = EVAL_LIMIT
      r = list(range(1000))
      # random.shuffle(r)
      for x in r:
          limit -= 1
          if limit > 0:
            with open("/root/ContextToCode/predictor/log/prog_produced.txt", "a") as myfile:
                myfile.write(str(repl(sess1, npi, data, x, predict))+"\n")
                myfile.write(str(repl(sess2, npi, data, x, predict))+"\n")
                myfile.write(str(repl(sess3, npi, data, x, predict))+"\n")
            print (str(limit)+"--------------------------")
Пример #13
0
def evaluate_vqa():
    """
    Load NPI Model from Checkpoint, and initialize REPL, for interactive carry-addition.
    """
    # Load Data
    with open(TEST_PATH, 'rb') as f:
        data = pickle.load(f)
        data = data[50:100]

    # Initialize Addition Core
    core = VQAcore()

    # Initialize NPI Model
    npi = NPI(core, CONFIG, LOG_PATH)

    with tf.Session() as sess:
        # Restore from Checkpoint
        saver = tf.train.Saver()
        saver.restore(sess, CKPT_PATH)

        # Run REPL
        repl(sess, npi, data)
Пример #14
0
def evaluate_bubblesort():
    """
    Load NPI Model from Checkpoint, and initialize REPL, for interactive bubblesort.
    """
    with tf.Session() as sess:
        # Load Data
        with open(TEST_PATH, 'rb') as f:
            data = pickle.load(f)

        # Initialize Addition Core
        print('initializing bubblesort core ...')
        core = BubblesortCore()

        # Initialize NPI Model
        print('initializing npi model ...')
        npi = NPI(core, CONFIG, LOG_PATH)

        # Restore from Checkpoint
        saver = tf.train.Saver()
        saver.restore(sess, CKPT_PATH)

        # Run REPL
        repl(sess, npi, data)
Пример #15
0
def train_addition(epochs, verbose=0):
    """
    Instantiates an Addition Core, NPI, then loads and fits model to data.

    :param epochs: Number of epochs to train for.
    """
    # Load Data
    with open(DATA_PATH, 'r') as f:
        data = pickle.load(f)

    # Initialize Addition Core
    print 'Initializing Addition Core!'
    core = AdditionCore()

    # Initialize NPI Model
    print 'Initializing NPI Model!'
    npi = NPI(core, CONFIG, LOG_PATH, verbose=verbose)

    # Initialize TF Saver
    saver = tf.train.Saver()

    # Initialize TF Session
    sess = tf.Session()
    sess.run(tf.initialize_all_variables())

    # Start Training
    for ep in range(1, epochs + 1):
        for i in range(len(data)):
            # Reset NPI States
            npi.reset_state()

            # Setup Environment
            in1, in2, steps = data[i]
            scratch = ScratchPad(in1, in2)
            x, y = steps[:-1], steps[1:]
            # Run through steps, and fit!
            step_def_loss, step_arg_loss, term_acc, prog_acc, = 0.0, 0.0, 0.0, 0.0
            arg0_acc, arg1_acc, arg2_acc, num_args = 0.0, 0.0, 0.0, 0
            for j in range(len(x)):
                (prog_name, prog_in_id), arg, term = x[j]
                (_, prog_out_id), arg_out, term_out = y[j]
                # print(x[j], y[j])
                # Update Environment if MOVE or WRITE
                if prog_in_id == MOVE_PID or prog_in_id == WRITE_PID:
                    scratch.execute(prog_in_id, arg)

                # Get Environment, Argument Vectors
                env_in = [scratch.get_env()]
                arg_in, arg_out = [get_args(arg, arg_in=True)
                                   ], get_args(arg_out, arg_in=False)
                prog_in, prog_out = [[prog_in_id]], [prog_out_id]
                term_out = [1] if term_out else [0]

                # Fit!
                if prog_out_id == MOVE_PID or prog_out_id == WRITE_PID:
                    loss, t_acc, p_acc, a_acc, _ = sess.run(
                        [
                            npi.arg_loss, npi.t_metric, npi.p_metric,
                            npi.a_metrics, npi.arg_train_op
                        ],
                        feed_dict={
                            npi.env_in: env_in,
                            npi.arg_in: arg_in,
                            npi.prg_in: prog_in,
                            npi.y_prog: prog_out,
                            npi.y_term: term_out,
                            npi.y_args[0]: [arg_out[0]],
                            npi.y_args[1]: [arg_out[1]],
                            npi.y_args[2]: [arg_out[2]]
                        })
                    # print({npi.prg_in: prog_in, npi.y_prog: prog_out, npi.y_term: term_out})
                    # print({npi.y_args[0]: [arg_out[0]], npi.y_args[1]: [arg_out[1]], npi.y_args[2]: [arg_out[2]]})
                    step_arg_loss += loss
                    term_acc += t_acc
                    prog_acc += p_acc
                    arg0_acc += a_acc[0]
                    arg1_acc += a_acc[1]
                    arg2_acc += a_acc[2]
                    num_args += 1
                else:
                    loss, t_acc, p_acc, _ = sess.run(
                        [
                            npi.default_loss, npi.t_metric, npi.p_metric,
                            npi.default_train_op
                        ],
                        feed_dict={
                            npi.env_in: env_in,
                            npi.arg_in: arg_in,
                            npi.prg_in: prog_in,
                            npi.y_prog: prog_out,
                            npi.y_term: term_out
                        })
                    step_def_loss += loss
                    term_acc += t_acc
                    prog_acc += p_acc

            print "Epoch {0:02d} Step {1:03d} Default Step Loss {2:05f}, " \
                  "Argument Step Loss {3:05f}, Term: {4:03f}, Prog: {5:03f}, A0: {6:03f}, " \
                  "A1: {7:03f}, A2: {8:03}"\
                .format(ep, i, step_def_loss / len(x), step_arg_loss / len(x), term_acc / len(x),
                        prog_acc / len(x), arg0_acc / num_args, arg1_acc / num_args,
                        arg2_acc / num_args)

        # Save Model
        saver.save(sess, 'tasks/addition/log/model.ckpt')

    TRAIN_DATA_PATH = 'tasks/reverse_polish/data/train_8.pik'
    with open(TRAIN_DATA_PATH, 'rb', ) as f:
        train_data = pickle.load(f)

    EVAL_DATA_PATH = 'tasks/reverse_polish/data/eval_8.pik'
    with open(EVAL_DATA_PATH, 'rb', ) as f:
        eval_data = pickle.load(f)

    TEST_DATA_PATH = 'tasks/reverse_polish/data/test_8.pik'
    with open(TEST_DATA_PATH, 'rb', ) as f:
        test_data = pickle.load(f)

    func_core = RevPolishCore().to(device)
    npi       = NPI(func_core, CONFIG).to(device)

    print_net(npi)

    optimizer = torch.optim.Adam(npi.parameters(), lr=1e-4)
    # optimizer = torch.optim.SGD(npi.parameters(), lr=1e-4)
    lr_schedulers = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=max_num_epochs)

    if not FLG_TEST:

        for epoch in range(start_epoch, max_num_epochs+1):
            
            run_epoch(npi, 'train', train_data, writer, os.path.join(exp_dir, 'npi_{}.pth'.format(epoch)))

            lr_schedulers.step(epoch)
    EVAL_DATA_PATH = 'tasks/reverse_polish/data/eval_8.pik'
    with open(
            EVAL_DATA_PATH,
            'rb',
    ) as f:
        eval_data = pickle.load(f)

    TEST_DATA_PATH = 'tasks/reverse_polish/data/test_8.pik'
    with open(
            TEST_DATA_PATH,
            'rb',
    ) as f:
        test_data = pickle.load(f)

    func_core = RevPolishCore().to(device)
    npi = NPI(func_core, CONFIG).to(device)

    print_net(npi)

    # optimizer = torch.optim.Adam(npi.parameters(), lr=1e-4)
    optimizer = torch.optim.SGD(npi.parameters(), lr=1e-4)
    lr_schedulers = torch.optim.lr_scheduler.CosineAnnealingLR(
        optimizer, T_max=max_num_epochs)

    if not FLG_TEST:

        for epoch in range(start_epoch, max_num_epochs + 1):

            run_epoch(npi, 'train', train_data, writer,
                      os.path.join(exp_dir, 'npi_{}.pth'.format(epoch)))
Пример #18
0
def train_vqa(epochs, verbose=0):
    """
    Instantiates a VQA Core, NPI, then loads and fits model to data.

    :param epochs: Number of epochs to train for.
    """
    # Load Data
    with open(DATA_PATH, 'rb') as f:
        dataT = pickle.load(f)
        data = dataT[:80]
        test = dataT[80:100]
    with open(DATA_PATH2, 'rb') as f:
        dataT2 = pickle.load(f)
        test_out = dataT2[:20]
    with open(DATA_PATH3, 'rb') as f:
        dataT3 = pickle.load(f)
        test_out2 = dataT3[:20]

    # Initialize VQA Core
    print('Initializing VQA Core!')
    core = VQAcore()

    # Initialize NPI Model
    print('Initializing NPI Model!')
    npi = NPI(core, CONFIG, LOG_PATH, verbose=verbose)

    # Initialize TF Saver
    saver = tf.train.Saver()

    # Initialize TF Session
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        # Start Training
        removed = {}
        errors = {}
        # for learning curve
        count = 0
        # tot_loss_def = []
        # tot_loss_arg = []
        # test_loss_def = []
        # test_loss_arg = []
        # test1_loss_def = []
        # test1_loss_arg = []
        # test2_loss_def = []
        # test2_loss_arg = []
        test_term_acct = []
        test_prog_acct = []
        test_arg_acct = []
        train_term_acct = []
        train_prog_acct = []
        train_arg_acct = []
        test1_term_acct = []
        test1_prog_acct = []
        test1_arg_acct = []
        test2_term_acct = []
        test2_prog_acct = []
        test2_arg_acct = []

        step = []
        for ep in range(1, epochs + 1):
            removed[ep] = 0
            for i in range(len(data)):
                # Reset NPI States
                npi.reset_state()

                # Setup Environment
                _, imgid, qid, qtype, steps = data[i]
                scene = Scene(imgid)
                x, y = steps[:-1], steps[1:]
                if len(x) == 0 or len(y) == 0:
                    removed[ep] += 1
                    continue
                count += 1

                # Run through steps, and fit!
                step_def_loss, step_arg_loss, term_acc, prog_acc, = 0.0, 0.0, 0.0, 0.0
                arg0_acc, arg1_acc, arg2_acc, num_args = 0.0, 0.0, 0.0, 0
                for j in range(len(x)):
                    (prog_name, prog_in_id), arg, term = x[j]
                    (_, prog_out_id), arg_out, term_out = y[j]

                    # Update Environment if MOVE or WRITE
                    if prog_in_id in EX_PROG_PID:
                        scene.execute(prog_in_id, arg)

                    # Get Environment, Argument Vectors
                    env_in = [scene.get_env()]
                    # env_in = [np.asarray(list(env_in.values())).transpose().flatten()]
                    arg_in, arg_out = [get_args(arg, arg_in=True)
                                       ], get_args(arg_out, arg_in=False)
                    prog_in, prog_out = [[prog_in_id]], [prog_out_id]
                    term_out = [1] if term_out else [0]

                    # Fit!
                    if prog_out_id in PAR_PROG_PID:
                        loss, t_acc, p_acc, a_acc, _ = sess.run(
                            [
                                npi.arg_loss, npi.t_metric, npi.p_metric,
                                npi.a_metrics, npi.arg_train_op
                            ],
                            feed_dict={
                                npi.env_in: env_in,
                                npi.arg_in: arg_in,
                                npi.prg_in: prog_in,
                                npi.y_prog: prog_out,
                                npi.y_term: term_out,
                                npi.y_args[0]: [arg_out[0]],
                                npi.y_args[1]: [arg_out[1]],
                                npi.y_args[2]: [arg_out[2]]
                            })
                        step_arg_loss += loss
                        term_acc += t_acc
                        prog_acc += p_acc
                        arg0_acc += a_acc[0]
                        arg1_acc += a_acc[1]
                        arg2_acc += a_acc[2]
                        num_args += 1

                    else:
                        loss, t_acc, p_acc, _ = sess.run(
                            [
                                npi.default_loss, npi.t_metric, npi.p_metric,
                                npi.default_train_op
                            ],
                            feed_dict={
                                npi.env_in: env_in,
                                npi.arg_in: arg_in,
                                npi.prg_in: prog_in,
                                npi.y_prog: prog_out,
                                npi.y_term: term_out
                            })
                        step_def_loss += loss
                        term_acc += t_acc
                        prog_acc += p_acc

                try:
                    print ("Epoch {0:02d} Step {1:03d} Default Step Loss {2:05f}, " \
                    "Argument Step Loss {3:05f}, Term: {4:03f}, Prog: {5:03f}, A0: {6:03f}, " \
                    "A1: {7:03f}, A2: {8:03}".format(ep, i, step_def_loss / len(x), step_arg_loss / len(x), term_acc / len(x),
                            prog_acc / len(x), arg0_acc / num_args, arg1_acc / num_args,
                            arg2_acc / num_args))
                    if count % 10 == 0:
                        # Save Model
                        tmp = stat.mean([
                            arg0_acc / num_args, arg1_acc / num_args,
                            arg2_acc / num_args
                        ])
                        saver.save(sess, 'tasks/vqa/log/model.ckpt')
                        train_arg_acct.append(tmp / len(x))
                        train_prog_acct.append(prog_acc / len(x))
                        train_term_acct.append(term_acc / len(x))
                        step.append(count)
                        a, b, c = test_vqa(test, npi, core, sess)
                        test_arg_acct.append(c)
                        test_prog_acct.append(b)
                        test_term_acct.append(a)
                        a, b, c = test_vqa(test_out, npi, core, sess)
                        test1_arg_acct.append(c)
                        test1_prog_acct.append(b)
                        test1_term_acct.append(a)
                        a, b, c = test_vqa(test_out2, npi, core, sess)
                        test2_arg_acct.append(c)
                        test2_prog_acct.append(b)
                        test2_term_acct.append(a)
                except:
                    print('main print failed')

            # Save Model
            saver.save(sess, 'tasks/vqa/log/model.ckpt')
        # print learning curve
        print('train term,prog,arg: ', test_term_acct[-1], test_prog_acct[-1],
              test_arg_acct[-1])
        print('test_inside term,prog,arg: ', test_term_acct[-1],
              test_prog_acct[-1], test_arg_acct[-1])
        print('test_out term,prog,arg: ', test1_term_acct[-1],
              test1_prog_acct[-1], test1_arg_acct[-1])
        print('test_out2 term,prog,arg: ', test2_term_acct[-1],
              test2_prog_acct[-1], test2_arg_acct[-1])

        plt.figure(figsize=(20, 5))
        plt.plot(step, train_term_acct, 'b', label='train_query_term')
        plt.plot(step, test_term_acct, 'm', label='test_query_term')
        plt.plot(step, test1_term_acct, 'c', label='test_count_term')
        plt.plot(step, test2_term_acct, 'k', label='test_exist_term')
        plt.legend()
        plt.xticks(step)
        plt.xlabel('step')
        plt.ylabel('acc')
        plt.title('learning curve for termination')
        plt.savefig(SAVE_PATH + 'acc_query_term')
        plt.close()
        plt.figure(figsize=(20, 5))
        plt.plot(step, train_prog_acct, 'b', label='train_query_prog')
        plt.plot(step, test_prog_acct, 'm', label='test_query_prog')
        plt.plot(step, test1_prog_acct, 'c', label='test_count_prog')
        plt.plot(step, test2_prog_acct, 'k', label='test_exist_prog')
        plt.legend()
        plt.xticks(step)
        plt.xlabel('step')
        plt.ylabel('acc')
        plt.title('learning curve for program')
        plt.savefig(SAVE_PATH + 'acc_query_prog')
        plt.close()
        plt.figure(figsize=(20, 5))
        plt.plot(step, train_arg_acct, 'b', label='train_query_arg')
        plt.plot(step, test_arg_acct, 'm', label='test_query_arg')
        plt.plot(step, test1_arg_acct, 'c', label='test_count_arg')
        plt.plot(step, test2_arg_acct, 'k', label='test_exist_arg')
        plt.legend()
        plt.xticks(step)
        plt.xlabel('step')
        plt.ylabel('acc')
        plt.title('learning curve for arguments')
        plt.savefig(SAVE_PATH + 'acc_query_arg')
        plt.close()
Пример #19
0
def train_addition(epochs, verbose=0):
    """
    Instantiates an Addition Core, NPI, then loads and fits model to data.

    :param epochs: Number of epochs to train for.
    """
    # Load Data
    with open(DATA_PATH_TRAIN, 'rb') as f:
        data = pickle.load(f)

    # Initialize Addition Core
    print ('Initializing Addition Core!')
    core = AdditionCore()

    # Initialize NPI Model
    print ('Initializing NPI Model!')
    npi = NPI(core, CONFIG, LOG_PATH, verbose=verbose)

    # Initialize TF Saver
    saver = tf.train.Saver()

    # Initialize TF Session
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

    # Start Training
    for ep in range(1, epochs + 1):
        for i in range(len(data)):
            # Reset NPI States
            npi.reset_state()

            # Setup Environment
            steps = data[i]
            # print(data[i])

            x, y = steps[:-1], steps[1:]
            # Run through steps, and fit!
            step_def_loss, step_arg_loss, term_acc, prog_acc, = 0.0, 0.0, 0.0, 0.0
            arg0_acc, arg1_acc, arg2_acc, num_args = 0.0, 0.0, 0.0, 0

            # dsl = DSL([], [])

            for j in range(len(x)):
                # {'program': {'program': 'check'}, 'environment': {'terminate': False, 'answer': 1, 'is_redirect': 2},'args': {'id': 0}}
                # print(y[j])
                prog_name, prog_in_id, arg, term = x[j]["program"]["program"], x[j]["program"]["id"], x[j]["args"]["id"], x[j]["environment"]["terminate"]
                prog_name_out, prog_out_id, arg_out, term_out = y[j]["program"]["program"], y[j]["program"]["id"], y[j]["args"]["id"], y[j]["environment"]["terminate"]
                # Get Environment, Argument Vectors
                env_in = [get_env(x[j]["environment"])]

                arg_in, arg_out = [get_args(arg, arg_in=True)], get_args(arg_out, arg_in=False)
                term_out = [1] if term_out else [0]

                # if prog_name_out=="WRITE":
                #     prog_out_id = dsl.get_code(y[j]["prog"]["arg"][1])
                #     os._exit()

                prog_in, prog_out = [[prog_in_id]], [prog_out_id]

                # Fit!
                if True:
                    t_acc, p_acc, _, loss = sess.run(
                        [npi.t_metric, npi.p_metric, npi.default_train_op, npi.default_loss],
                        feed_dict={npi.env_in: env_in, npi.arg_in: arg_in, npi.prg_in: prog_in,
                                   npi.y_prog: prog_out, npi.y_term: term_out})
                    # print({npi.env_in: env_in, npi.arg_in: arg_in, npi.prg_in: prog_in, npi.y_prog: prog_out, npi.y_term: term_out})
                    # print({npi.y_args[0]: [arg_out[0]], npi.y_args[1]: [arg_out[1]], npi.y_args[2]: [arg_out[2]]})
                    # step_arg_loss += loss
                    term_acc += t_acc
                    prog_acc += p_acc
                    step_def_loss += loss
                    # arg0_acc += a_acc[0]
                    # arg1_acc += a_acc[1]
                    # arg2_acc += a_acc[2]
                    # num_args += 1
                    # else:
                    #     loss, t_acc, p_acc, _ = sess.run(
                    #         [npi.default_loss, npi.t_metric, npi.p_metric, npi.default_train_op],
                    #         feed_dict={npi.env_in: env_in, npi.arg_in: arg_in, npi.prg_in: prog_in,
                    #                    npi.y_prog: prog_out, npi.y_term: term_out})
                    #     step_def_loss += loss
                    #     term_acc += t_acc
                    #     prog_acc += p_acc

            print ("Epoch {0:02d} Step {1:03d} Loss: {2:03f} Term: {3:03f}, Prog: {4:03f}" \
                    .format(ep, i, step_def_loss / len(x), term_acc / len(x), prog_acc / len(x)))

        # Save Model
        saver.save(sess, CKPT_PATH)
        # !!!!
        tf.train.write_graph(sess.graph_def, '/tmp/tf/log', 'graph.pb', as_text=False)
        # tf.train.write_graph(my_graph, path_to_model_pb, 'saved_model.pb', as_text=False)
        # !!!!
Пример #20
0
def train_card_pattern_matching(epochs, verbose=0):
    # Load Data
    with open(DATA_PATH, 'rb') as f:
        data = pickle.load(f)

    # Initialize Card Pattern Matching Core
    print('Initializing Card Pattern Matching Core!')
    core = CardPatternMatchingCore()

    # Initialize NPI Model
    print('Initializing NPI Model!')
    npi = NPI(core, CONFIG, LOG_PATH, verbose=verbose)

    # Initialize TF Saver
    saver = tf.train.Saver()

    # Initialize TF Session
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        # Start Training
        for ep in range(1, epochs + 1):
            for i in range(len(data)):
                # Reset NPI States
                npi.reset_state()

                # Setup Environment
                card1, card2, steps = data[i]
                scratch = ScratchPad(card1, card2)
                x, y = steps[:-1], steps[1:]

                # Run through steps, and fit!
                step_def_loss, step_arg_loss, term_acc, prog_acc, = 0.0, 0.0, 0.0, 0.0
                arg0_acc, arg1_acc, arg2_acc, num_args = 0.0, 0.0, 0.0, 0
                for j in range(len(x)):
                    (prog_name, prog_in_id), arg, term = x[j]
                    (_, prog_out_id), arg_out, term_out = y[j]

                    # Update Environment if MOVE or WRITE
                    if prog_in_id == MOVE_PID or prog_in_id == WRITE_PID:
                        scratch.execute(prog_in_id, arg)

                    # Get Environment, Argument Vectors
                    env_in = [scratch.get_env()]
                    arg_in, arg_out = [get_args(arg, arg_in=True)
                                       ], get_args(arg_out, arg_in=False)
                    prog_in, prog_out = [[prog_in_id]], [prog_out_id]
                    term_out = [1] if term_out else [0]

                    # Fit!
                    if prog_out_id == MOVE_PID or prog_out_id == WRITE_PID:
                        loss, t_acc, p_acc, a_acc, _ = sess.run(
                            [
                                npi.arg_loss, npi.t_metric, npi.p_metric,
                                npi.a_metrics, npi.arg_train_op
                            ],
                            feed_dict={
                                npi.env_in: env_in,
                                npi.arg_in: arg_in,
                                npi.prg_in: prog_in,
                                npi.y_prog: prog_out,
                                npi.y_term: term_out,
                                npi.y_args[0]: [arg_out[0]],
                                npi.y_args[1]: [arg_out[1]],
                                npi.y_args[2]: [arg_out[2]]
                            })
                        step_arg_loss += loss
                        term_acc += t_acc
                        prog_acc += p_acc
                        arg0_acc += a_acc[0]
                        arg1_acc += a_acc[1]
                        arg2_acc += a_acc[2]
                        num_args += 1
                    else:
                        loss, t_acc, p_acc, _ = sess.run(
                            [
                                npi.default_loss, npi.t_metric, npi.p_metric,
                                npi.default_train_op
                            ],
                            feed_dict={
                                npi.env_in: env_in,
                                npi.arg_in: arg_in,
                                npi.prg_in: prog_in,
                                npi.y_prog: prog_out,
                                npi.y_term: term_out
                            })
                        step_def_loss += loss
                        term_acc += t_acc
                        prog_acc += p_acc

                print ("Epoch {0:02d} Step {1:03d} Default Step Loss {2:05f}, " \
                    "Argument Step Loss {3:05f}, Term: {4:03f}, Prog: {5:03f}, A0: {6:03f}, " \
                    "A1: {7:03f}, A2: {8:03}".format(ep, i, step_def_loss / len(x), step_arg_loss / len(x), term_acc / len(x),
                            prog_acc / len(x), arg0_acc / num_args, arg1_acc / num_args,
                            arg2_acc / num_args))

            # Save Model
            saver.save(
                sess,
                'tasks/card_pattern_matching/log/card_pattern_matching_model.ckpt'
            )

    print('Model generation complete!')
Пример #21
0
def train_addition(epochs, start_epoch, start_step, sub, verbose=0):
    """
    Instantiates an Addition Core, NPI, then loads and fits model to data.

    :param epochs: Number of epochs to train for.
    """
    if not os.path.exists("log/" + sub + "/models/"):
        os.mkdir("log/" + sub + "/models/")
    # Load Data
    with open(DATA_PATH_TRAIN, 'rb') as f:
        data = pickle.load(f)

# f = open('log/log_train.txt', 'r+')
#f.truncate()

# Initialize Addition Core
    print('Initializing Addition Core!')
    core = AdditionCore()

    # Initialize NPI Model
    print('Initializing NPI Model!')
    npi = NPI(core, CONFIG, LOG_PATH, verbose=verbose)

    # Initialize TF Saver
    saver = tf.train.Saver()

    # Initialize TF Session
    sess = tf.Session()

    if start_epoch > 0 or start_step > 0:
        saver = tf.train.Saver()
        if start_step > 0:
            saver.restore(
                sess,
                "log/" + sub + "/models/model-{0:04d}-{1:06d}.ckpt".format(
                    start_epoch + 1, start_step))
            print("log/" + sub + "/models/model-{0:04d}-{1:06d}.ckpt".format(
                start_epoch + 1, start_step))
        else:
            saver.restore(
                sess, "log/" + sub +
                "/models/model-{0:04d}.ckpt".format(start_epoch))
            print("log/" + sub +
                  "/models/model-{0:04d}.ckpt".format(start_epoch))
    else:
        sess.run(tf.global_variables_initializer())

    # Reset NPI States
    npi.reset_state()
    tf.get_default_graph().finalize()

    # Start Training
    for ep in range(start_epoch + 1, epochs + 1):
        sum = 0
        for i in range(len(data)):
            if i > start_step:
                # Setup Environment
                steps = data[i]
                # print(data[i])

                x, y = steps[:-1], steps[1:]
                # Run through steps, and fit!
                step_def_loss, step_arg_loss, term_acc, prog_acc, = 0.0, 0.0, 0.0, 0.0
                arg0_acc, arg1_acc, arg2_acc, num_args = 0.0, 0.0, 0.0, 0

                # dsl = DSL([], [])

                if len(x) > 0:
                    for j in range(len(x)):
                        # {'program': {'program': 'check'}, 'environment': {'terminate': False, 'answer': 1, 'is_redirect': 2},'args': {'id': 0}}
                        #print(x[j]['addinfo'])
                        prog_name, prog_in_id, arg, term = x[j]["program"][
                            "program"], x[j]["program"]["id"], x[j]["args"][
                                "id"], x[j]["environment"]["terminate"]
                        prog_name_out, prog_out_id, arg_out, term_out = y[j][
                            "program"]["program"], y[j]["program"]["id"], y[j][
                                "args"]["id"], y[j]["environment"]["terminate"]
                        # Get Environment, Argument Vectors
                        env_in = [get_env(x[j]["environment"])]

                        arg_in, arg_out = [get_args(arg, arg_in=True)
                                           ], get_args(arg_out, arg_in=False)
                        term_out = [1] if term_out else [0]

                        # if prog_name_out=="WRITE":
                        #     prog_out_id = dsl.get_code(y[j]["prog"]["arg"][1])
                        #     os._exit()

                        prog_in, prog_out = [[prog_in_id]], [prog_out_id]

                        # Fit!
                        if True:
                            t_acc, p_acc, _, loss = sess.run(
                                [
                                    npi.t_metric, npi.p_metric,
                                    npi.default_train_op, npi.default_loss
                                ],
                                feed_dict={
                                    npi.env_in: env_in,
                                    npi.arg_in: arg_in,
                                    npi.prg_in: prog_in,
                                    npi.y_prog: prog_out,
                                    npi.y_term: term_out
                                })
                            # print({npi.env_in: env_in, npi.arg_in: arg_in, npi.prg_in: prog_in, npi.y_prog: prog_out, npi.y_term: term_out})
                            # print({npi.y_args[0]: [arg_out[0]], npi.y_args[1]: [arg_out[1]], npi.y_args[2]: [arg_out[2]]})
                            # step_arg_loss += loss
                            term_acc += t_acc
                            prog_acc += p_acc
                            step_def_loss += loss
                            # arg0_acc += a_acc[0]
                            # arg1_acc += a_acc[1]
                            # arg2_acc += a_acc[2]
                            # num_args += 1
                            # else:
                            #     loss, t_acc, p_acc, _ = sess.run(
                            #         [npi.default_loss, npi.t_metric, npi.p_metric, npi.default_train_op],
                            #         feed_dict={npi.env_in: env_in, npi.arg_in: arg_in, npi.prg_in: prog_in,
                            #                    npi.y_prog: prog_out, npi.y_term: term_out})
                            #     step_def_loss += loss
                            #     term_acc += t_acc
                            #     prog_acc += p_acc
                    sum += prog_acc / len(x)
                    #with open('log/log_train.txt', "a") as myfile:
                    if i % 1000 == 0:
                        message = "Epoch "+sub+" {0:02d} Step {1:03d} Loss: {2:03f} Term: {3:03f}, Prog: {4:03f} AVG: {5:03f}" \
                            .format(ep, i, step_def_loss / len(x), term_acc / len(x), prog_acc / len(x), sum / (i - start_step))
                        print(message)
                        with open("log/" + sub + "/info", "a") as myfile:
                            myfile.write(message + "\n")
                    if i % chunk == 0:
                        saver.save(
                            sess, "log/" + sub +
                            "/models/model-{0:04d}-{1:06d}.ckpt".format(ep, i))
                        #if os.path.exists("log/model-{0:04d}-{1:06d}.ckpt.meta".format(ep, i-chunk)):
                        #    os.remove("log/model-{0:04d}-{1:06d}.ckpt.meta".format(ep, i-chunk))
                        #    os.remove("log/model-{0:04d}-{1:06d}.ckpt.index".format(ep, i-chunk))
                        #    os.remove("log/model-{0:04d}-{1:06d}.ckpt.data-00000-of-00001".format(ep, i-chunk))
        print ("Epoch {0:02d} Step {1:03d}  AVG: {2:03f}" \
                        .format(ep, len(data), sum / len(data)))
        # Save Model
        saver.save(sess,
                   "log/" + sub + "/models/model-{0:04d}.ckpt".format(ep))
        # !!!!
        tf.train.write_graph(sess.graph_def,
                             '/tmp/tf/log',
                             'graph.pb',
                             as_text=False)
Пример #22
0
def test_vqa(verbose=0):
    """
    Instantiates a VQA Core, NPI, then loads and fits model to data.

    :param epochs: Number of epochs to train for.
    """

    # Load Data
    with open(DATA_PATH, 'rb') as f:
        data = pickle.load(f)
        data = data[80:300]  # the default model saved has been trained on same data but [:80]
    # Initialize VQA Core
    print ('Initializing VQA Core!')
    core = VQAcore()

    # Initialize NPI Model
    print ('Initializing NPI Model!')
    npi = NPI(core, CONFIG, LOG_PATH, verbose=verbose)

    with tf.Session() as sess:
        # Restore from Checkpoint
        saver = tf.train.Saver()
        saver.restore(sess, CKPT_PATH)

        term_acct = []
        prog_acct = []
        arg_acct = []
        step = []
        count = 0
        # Start Testing
        for i in range(len(data)):
            # Reset NPI States
            npi.reset_state()

            # Setup Environment
            _,imgid, qid, qtype, steps = data[i]
            scene = Scene(imgid)
            x, y = steps[:-1], steps[1:]
            if len(x) == 0 or len(y) == 0:
                continue
            count += 1

            # Run through steps, and fit!
            step_def_loss, step_arg_loss, term_acc, prog_acc, = 0.0, 0.0, 0.0, 0.0
            arg0_acc, arg1_acc, arg2_acc, num_args = 0.0, 0.0, 0.0, 0
            for j in range(len(x)):
                if random.uniform(0,1) > ABAL_THR:
                    (prog_name, prog_in_id), arg, term = y[j]
                    (_, prog_out_id), arg_out, term_out = x[j]
                else:
                    (prog_name, prog_in_id), arg, term = x[j]
                    (_, prog_out_id), arg_out, term_out = y[j]

                # Update Environment if MOVE or WRITE
                if prog_in_id in EX_PROG_PID:
                    scene.execute(prog_in_id, arg)

                # Get Environment, Argument Vectors
                env_in = [scene.get_env()]
                arg_in, arg_out = [get_args(arg, arg_in=True)], get_args(arg_out, arg_in=False)
                prog_in, prog_out = [[prog_in_id]], [prog_out_id]
                term_out = [1] if term_out else [0]

                # Fit!
                if prog_out_id in PAR_PROG_PID:

                    loss, t_acc, p_acc, a_acc= sess.run(
                        [npi.arg_loss, npi.t_metric, npi.p_metric, npi.a_metrics],
                        feed_dict={npi.env_in: env_in, npi.arg_in: arg_in, npi.prg_in: prog_in,
                                npi.y_prog: prog_out, npi.y_term: term_out,
                                npi.y_args[0]: [arg_out[0]], npi.y_args[1]: [arg_out[1]],
                                npi.y_args[2]: [arg_out[2]]})
                    step_arg_loss += loss
                    term_acc += t_acc
                    prog_acc += p_acc
                    arg0_acc += a_acc[0]
                    arg1_acc += a_acc[1]
                    arg2_acc += a_acc[2]
                    num_args += 1

                else:

                    loss, t_acc, p_acc= sess.run(
                        [npi.default_loss, npi.t_metric, npi.p_metric],
                        feed_dict={npi.env_in: env_in, npi.arg_in: arg_in, npi.prg_in: prog_in,
                                npi.y_prog: prog_out, npi.y_term: term_out})
                    step_def_loss += loss
                    term_acc += t_acc
                    prog_acc += p_acc



            try:
                print ("Step {} Default Step Loss {}, " \
                "Argument Step Loss {}, Term: {}, Prog: {}, A0: {}, " \
                "A1: {}, A2: {}".format(i, step_def_loss / len(x), step_arg_loss / len(x), term_acc / len(x),
                        prog_acc / len(x), arg0_acc / num_args, arg1_acc / num_args,
                        arg2_acc / num_args))
                tmp = stat.mean([arg0_acc / num_args, arg1_acc / num_args, arg2_acc / num_args])
                term_acct.append(term_acc / len(x))
                prog_acct.append(prog_acc / len(x))
                arg_acct.append(tmp)
                step.append(count)
            except:
                print('main print failed')

            plt.figure(figsize=(20, 5))
            plt.plot(step, term_acct, 'b', label='term')
            plt.plot(step, prog_acct, 'm', label='prog')
            plt.plot(step, arg_acct, 'c', label='arg')
            plt.legend()
            plt.xticks(step)
            plt.xlabel('step')
            plt.ylabel('acc')
            plt.title('Ablation Study')
            plt.savefig(SAVE_PATH + 'acc_ablation')
            plt.close()