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("--------------------------")
Example #2
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)+"--------------------------")
Example #3
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)
Example #4
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)
Example #5
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)+"--------------------------")
Example #6
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)
        # !!!!
Example #7
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)