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? ')
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')
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