示例#1
0
class AdditionCore(TaskBase):
    def __init__(self, hidden_dim, state_dim, batch_size=1):
        self.hidden_dim = hidden_dim
        self.state_dim = state_dim
        self.env_dim = CONFIG["ENVIRONMENT_ROW"] * CONFIG[
            "ENVIRONMENT_DEPTH"]  # 4 * 10 = 40
        self.arg_dim = CONFIG["ARGUMENT_NUM"] * CONFIG[
            "ARGUMENT_DEPTH"]  # 3 * 10 = 30
        self.in_dim = self.env_dim + self.arg_dim
        self.prog_embedding_dim = CONFIG["PROGRAM_EMBEDDING_SIZE"]
        self.prog_dim = CONFIG["PROGRAM_SIZE"]
        self.scratchPad = None
        # for f_enc
        self.fenc1 = nn.Linear(self.in_dim, self.hidden_dim)
        self.fenc2 = nn.Linear(self.hidden_dim, self.hidden_dim)
        self.fenc3 = nn.Linear(self.hidden_dim, self.state_dim)
        # for f_env
        self.fenv1 = nn.Embedding(self.prog_dim, self.prog_embedding_dim)

    def init_scratchPad(self, in1, in2):
        self.scratchPad = ScratchPad(in1, in2)
        print('Scratch Initialized')

    def get_args(self, args, arg_in=True):
        if arg_in:
            arg_vec = np.zeros(
                (CONFIG["ARGUMENT_NUM"], CONFIG["ARGUMENT_DEPTH"]),
                dtype=np.int32)
        else:
            arg_vec = [
                np.zeros((CONFIG["ARGUMENT_DEPTH"]), dtype=np.int32)
                for _ in range(CONFIG["ARGUMENT_NUM"])
            ]
        if len(args) > 0:
            for i in range(CONFIG["ARGUMENT_NUM"]):
                if i >= len(args):
                    arg_vec[i][CONFIG["DEFAULT_ARG_VALUE"]] = 1
                else:
                    arg_vec[i][args[i]] = 1
        else:
            for i in range(CONFIG["ARGUMENT_NUM"]):
                arg_vec[i][CONFIG["DEFAULT_ARG_VALUE"]] = 1
        return [arg_vec.flatten() if arg_in else arg_vec]

    def f_enc(self, env, args):
        merge = torch.cat([env, args], -1)
        elu = F.elu(self.fenc1(merge))
        elu = F.elu(self.fenc2(elu))
        out = self.fenc3(elu)
        return out

    def get_program_embedding(self, prog_id, args):
        embedding = self.fenv1(prog_id)
        return embedding

    def f_env(self, prog_id, args):
        if prog_id == MOVE_PID or prog_id == WRITE_PID:
            self.scratchPad.execute(prog_id, args)
        return [self.scratchPad.get_env()]
示例#2
0
文件: trace.py 项目: AnuragGolla/npi
    def __init__(self, in1, in2, debug=False):
        """
        Instantiates a trace object, and builds the exact execution pipeline for adding the given
        parameters.
        """
        self.in1, self.in2, self.debug = in1, in2, debug
        self.trace, self.scratch = [], ScratchPad(in1, in2)

        # Build Execution Trace
        self.build()

        # Check answer
        true_ans = self.in1 + self.in2
        trace_ans = int("".join(map(str, map(int, self.scratch[3]))))

        assert (true_ans == trace_ans)
示例#3
0
文件: eval.py 项目: nayname/npi
def repeat():
    with open("/root/npi/numbers.txt", 'r') as f:
        fl = f.readline()
        x, y = fl.rstrip('\n').split(",")
        print(x, y)
        scratch = ScratchPad(x, y)

    lines = [line.rstrip('\n') for line in open("/root/npi/prog.txt")]

    for c in lines:
        prog_id, arg0, arg1 = map(int, c.rstrip('\n').split(","))

        if prog_id == MOVE_PID or prog_id == WRITE_PID:
            scratch.execute(prog_id, [arg0, arg1])
        # Print Environment
        scratch.pretty_print()
示例#4
0
文件: train.py 项目: nayname/npi
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')
示例#5
0
def repl(session, npi, data):
    while True:
        inpt = input('Enter Two Numbers, or Hit Enter for Random Pair: ')

        if inpt == "":
            x, y, _ = data[np.random.randint(len(data))]

        else:
            x, y = map(int, inpt.split())

        # Reset NPI States
        print("")
        npi.reset_state()

        # Setup Environment
        scratch = ScratchPad(x, y)
        prog_name, prog_id, arg, term = 'ADD', 2, [], False

        cont = 'c'
        while cont == 'c' or cont == 'C':
            # Print Step Output
            if prog_id == MOVE_PID:
                a0, a1 = PTRS.get(arg[0], "OOPS!"), R_L[arg[1]]
                a_str = "[%s, %s]" % (str(a0), str(a1))
            elif prog_id == WRITE_PID:
                a0, a1 = W_PTRS[arg[0]], arg[1]
                a_str = "[%s, %s]" % (str(a0), str(a1))
            else:
                a_str = "[]"

            print('Step: %s, Arguments: %s, Terminate: %s' %
                  (prog_name, a_str, str(term)))
            print('IN 1: %s, IN 2: %s, CARRY: %s, OUT: %s' %
                  (scratch.in1_ptr[1], scratch.in2_ptr[1],
                   scratch.carry_ptr[1], scratch.out_ptr[1]))

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

            # Print Environment
            scratch.pretty_print()

            # Get Environment, Argument Vectors
            env_in, arg_in, prog_in = [scratch.get_env()
                                       ], [get_args(arg,
                                                    arg_in=True)], [[prog_id]]
            t, n_p, n_args = session.run(
                [npi.terminate, npi.program_distribution, npi.arguments],
                feed_dict={
                    npi.env_in: env_in,
                    npi.arg_in: arg_in,
                    npi.prg_in: prog_in
                })

            if np.argmax(t) == 1:
                print('Step: %s, Arguments: %s, Terminate: %s' %
                      (prog_name, a_str, str(True)))
                print('IN 1: %s, IN 2: %s, CARRY: %s, OUT: %s' %
                      (scratch.in1_ptr[1], scratch.in2_ptr[1],
                       scratch.carry_ptr[1], scratch.out_ptr[1]))
                # Update Environment if MOVE or WRITE
                if prog_id == MOVE_PID or prog_id == WRITE_PID:
                    scratch.execute(prog_id, arg)

                # Print Environment
                scratch.pretty_print()

                output = int("".join(map(str, map(int, scratch[3]))))
                print("Model Output: %s + %s = %s" %
                      (str(x), str(y), str(output)))
                print("Correct Out : %s + %s = %s" %
                      (str(x), str(y), str(x + y)))
                print("Correct!" if output == (x + y) else "Incorrect!")

            else:
                prog_id = np.argmax(n_p)
                prog_name = PROGRAM_SET[prog_id][0]
                if prog_id == MOVE_PID or prog_id == WRITE_PID:
                    arg = [np.argmax(n_args[0]), np.argmax(n_args[1])]
                else:
                    arg = []
                term = False

            cont = input('Continue? ')
示例#6
0
文件: eval.py 项目: nayname/npi
def repl(session, npi, data):
    inpt = raw_input('Enter Numbers, or Hit Enter for Random Pair: ')

    if inpt == "":
        x, y, _ = data[np.random.randint(len(data))]

    else:
        x, y = map(int, inpt.split())

    f = open('/root/npi/numbers.txt', 'r+')
    f.truncate()

    f = open('/root/npi/prog.txt', 'r+')
    f.truncate()

    with open("/root/npi/numbers.txt", "a") as myfile:
        myfile.write(str(x) + "," + str(y) + "\n")

    # Reset NPI States
    print ""
    npi.reset_state()

    # Setup Environment
    scratch = ScratchPad(x, y)
    prog_name, prog_id, term = 'ADD', 2, False

    cont = 'c'

    while cont == 'c' or cont == 'C':
        #Previous step
        if prog_id == MOVE_PID or prog_id == WRITE_PID:
            arg = [np.argmax(n_args[0]), np.argmax(n_args[1])]
        else:
            arg = []

        # Print Step Output
        if prog_id == MOVE_PID:
            a0, a1 = PTRS.get(arg[0], "OOPS!"), R_L[arg[1]]
            a_str = "[%s, %s]" % (str(a0), str(a1))
        elif prog_id == WRITE_PID:
            a0, a1 = W_PTRS[arg[0]], arg[1]
            a_str = "[%s, %s]" % (str(a0), str(a1))
        else:
            a_str = "[]"

        print 'Step: %s, Arguments: %s, Terminate: %s' % (prog_name, a_str,
                                                          str(term))
        print 'IN 1: %s, IN 2: %s, CARRY: %s, OUT: %s' % (
            scratch.in1_ptr[1], scratch.in2_ptr[1], scratch.carry_ptr[1],
            scratch.out_ptr[1])

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

        if arg:
            with open("/root/npi/prog.txt", "a") as myfile:
                myfile.write(
                    str(prog_id) + "," + str(np.argmax(n_args[0])) + "," +
                    str(np.argmax(n_args[1])) + "\n")

        # Print Environment
        scratch.pretty_print()

        # Get Environment, Argument Vectors
        # Current step
        env_in, arg_in, prog_in = [scratch.get_env()
                                   ], [get_args(arg, arg_in=True)], [[prog_id]]
        t, n_p, n_args = session.run(
            [npi.terminate, npi.program_distribution, npi.arguments],
            feed_dict={
                npi.env_in: env_in,
                npi.arg_in: arg_in,
                npi.prg_in: prog_in
            })

        # Next step
        if np.argmax(t) == 1:
            print 'Step: %s, Arguments: %s, Terminate: %s' % (prog_name, a_str,
                                                              str(True))
            print 'IN 1: %s, IN 2: %s, CARRY: %s, OUT: %s' % (
                scratch.in1_ptr[1], scratch.in2_ptr[1], scratch.carry_ptr[1],
                scratch.out_ptr[1])
            # Update Environment if MOVE or WRITE
            # if prog_id == MOVE_PID or prog_id == WRITE_PID:
            #     scratch.execute(prog_id, arg)

            output = int("".join(map(str, map(int, scratch[3]))))
            print "Model Output: %s + %s = %s" % (str(x), str(y), str(output))
            print "Correct Out : %s + %s = %s" % (str(x), str(y), str(x + y))
            print "Correct!" if output == (x + y) else "Incorrect!"
            cont = "n"

        else:
            prog_id = np.argmax(n_p)
            prog_name = PROGRAM_SET[prog_id][0]
            term = False
示例#7
0
 def init_scratchPad(self, in1, in2):
     self.scratchPad = ScratchPad(in1, in2)
     print('Scratch Initialized')