Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change requirements to allow newer versions #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ About
=====

Implementation of [Neural Programmer-Interpreters](http://arxiv.org/abs/1511.06279) with Keras.
Modified to use Python2 instead of Python3 and with example Jupyter notebooks (in the notebooks directory).

How to Demo
===========

[Demo Movie](https://youtu.be/s7PuBqwI2YA)

[View the Notebook](notebooks/TrainModel.ipynb)

requirement
-----------

* Python3
* Python2

setup
-----
Expand Down
947 changes: 947 additions & 0 deletions notebooks/TrainModel.ipynb

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
h5py==2.6.0
Keras==1.0.2
numpy==1.11.0
pydot-ng==1.0.0
pyparsing==2.1.1
PyYAML==3.11
scipy==0.17.0
six==1.10.0
Theano==0.8.2
h5py>=2.6.0
Keras>=1.0.2
numpy>=1.11.0
pydot-ng>=1.0.0
pyparsing>=2.1.1
PyYAML>=3.11
scipy>=0.17.0
six>=1.10.0
Theano>=0.8.2
7 changes: 5 additions & 2 deletions src/npi/add/create_training_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# coding: utf-8
from __future__ import with_statement
from __future__ import absolute_import
import os
import curses
import pickle
Expand All @@ -8,9 +10,10 @@
from npi.add.lib import AdditionEnv, AdditionProgramSet, AdditionTeacher, create_char_map, create_questions, run_npi
from npi.core import ResultLogger
from npi.terminal_core import TerminalNPIRunner, Terminal
from io import open


def main(stdscr, filename: str, num: int, result_logger: ResultLogger):
def main(stdscr, filename, num, result_logger):
terminal = Terminal(stdscr, create_char_map())
terminal.init_window(FIELD_WIDTH, FIELD_ROW)
program_set = AdditionProgramSet()
Expand Down Expand Up @@ -45,4 +48,4 @@ def main(stdscr, filename: str, num: int, result_logger: ResultLogger):
num_data = int(sys.argv[2]) if len(sys.argv) > 2 else 1000
log_filename = sys.argv[3] if len(sys.argv) > 3 else 'result.log'
curses.wrapper(main, output_filename, num_data, ResultLogger(log_filename))
print("create %d training data" % num_data)
print "create %d training data" % num_data
49 changes: 25 additions & 24 deletions src/npi/add/lib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding: utf-8
from __future__ import absolute_import
from random import random

import numpy as np
Expand All @@ -9,7 +10,7 @@
__author__ = 'k_morishita'


class AdditionEnv:
class AdditionEnv(object):
"""
Environment of Addition
"""
Expand All @@ -23,9 +24,9 @@ def reset(self):
self.screen.fill(0)
self.pointers = [self.screen.width-1] * self.screen.height # rightmost

def get_observation(self) -> np.ndarray:
def get_observation(self):
value = []
for row in range(len(self.pointers)):
for row in xrange(len(self.pointers)):
value.append(self.to_one_hot(self.screen[row, self.pointers[row]]))
return np.array(value) # shape of FIELD_ROW * FIELD_DEPTH

Expand Down Expand Up @@ -70,7 +71,7 @@ class MovePtrProgram(Program):
TO_LEFT = 0
TO_RIGHT = 1

def do(self, env: AdditionEnv, args: IntegerArguments):
def do(self, env, args):
ptr_kind = args.decode_at(0)
left_or_right = args.decode_at(1)
env.move_pointer(ptr_kind, left_or_right)
Expand All @@ -81,13 +82,13 @@ class WriteProgram(Program):
WRITE_TO_CARRY = 0
WRITE_TO_OUTPUT = 1

def do(self, env: AdditionEnv, args: IntegerArguments):
def do(self, env, args):
row = 2 if args.decode_at(0) == self.WRITE_TO_CARRY else 3
digit = args.decode_at(1)
env.write(row, digit+1)


class AdditionProgramSet:
class AdditionProgramSet(object):
NOP = Program('NOP')
MOVE_PTR = MovePtrProgram('MOVE_PTR', 4, 2) # PTR_KIND(4), LEFT_OR_RIGHT(2)
WRITE = WriteProgram('WRITE', 2, 10) # CARRY_OR_OUT(2), DIGITS(10)
Expand All @@ -109,17 +110,17 @@ def __init__(self):
self.register(self.LSHIFT)
self.register(self.RSHIFT)

def register(self, pg: Program):
def register(self, pg):
pg.program_id = self.program_id
self.map[pg.program_id] = pg
self.program_id += 1

def get(self, i: int):
def get(self, i):
return self.map.get(i)


class AdditionTeacher(NPIStep):
def __init__(self, program_set: AdditionProgramSet):
def __init__(self, program_set):
self.pg_set = program_set
self.step_queue = None
self.step_queue_stack = []
Expand All @@ -141,7 +142,7 @@ def register_subprogram(self, pg, method):
self.sub_program[pg.program_id] = method

@staticmethod
def decode_params(env_observation: np.ndarray, arguments: IntegerArguments):
def decode_params(env_observation, arguments):
return env_observation.argmax(axis=1), arguments.decode_all()

def enter_function(self):
Expand All @@ -151,7 +152,7 @@ def enter_function(self):
def exit_function(self):
self.step_queue = self.step_queue_stack.pop()

def step(self, env_observation: np.ndarray, pg: Program, arguments: IntegerArguments) -> StepOutput:
def step(self, env_observation, pg, arguments):
if not self.step_queue:
self.step_queue = self.sub_program[pg.program_id](env_observation, arguments)
if self.step_queue:
Expand All @@ -162,17 +163,17 @@ def step(self, env_observation: np.ndarray, pg: Program, arguments: IntegerArgum
return ret

@staticmethod
def convert_for_step_return(step_values: tuple) -> StepOutput:
def convert_for_step_return(step_values):
if len(step_values) == 2:
return StepOutput(PG_CONTINUE, step_values[0], IntegerArguments(step_values[1]))
else:
return StepOutput(step_values[0], step_values[1], IntegerArguments(step_values[2]))

@staticmethod
def pg_primitive(env_observation: np.ndarray, arguments: IntegerArguments):
def pg_primitive(env_observation, arguments):
return None

def pg_add(self, env_observation: np.ndarray, arguments: IntegerArguments):
def pg_add(self, env_observation, arguments):
ret = []
(in1, in2, carry, output), (a1, a2, a3) = self.decode_params(env_observation, arguments)
if in1 == 0 and in2 == 0 and carry == 0:
Expand All @@ -181,7 +182,7 @@ def pg_add(self, env_observation: np.ndarray, arguments: IntegerArguments):
ret.append((self.pg_set.LSHIFT, None))
return ret

def pg_add1(self, env_observation: np.ndarray, arguments: IntegerArguments):
def pg_add1(self, env_observation, arguments):
ret = []
p = self.pg_set
(in1, in2, carry, output), (a1, a2, a3) = self.decode_params(env_observation, arguments)
Expand All @@ -200,15 +201,15 @@ def sum_ch_list(ch_list):
ret += ch - 1
return ret

def pg_carry(self, env_observation: np.ndarray, arguments: IntegerArguments):
def pg_carry(self, env_observation, arguments):
ret = []
p = self.pg_set
ret.append((p.MOVE_PTR, (p.MOVE_PTR.PTR_CARRY, p.MOVE_PTR.TO_LEFT)))
ret.append((p.WRITE, (p.WRITE.WRITE_TO_CARRY, 1)))
ret.append((PG_RETURN, p.MOVE_PTR, (p.MOVE_PTR.PTR_CARRY, p.MOVE_PTR.TO_RIGHT)))
return ret

def pg_lshift(self, env_observation: np.ndarray, arguments: IntegerArguments):
def pg_lshift(self, env_observation, arguments):
ret = []
p = self.pg_set
ret.append((p.MOVE_PTR, (p.MOVE_PTR.PTR_IN1, p.MOVE_PTR.TO_LEFT)))
Expand All @@ -217,7 +218,7 @@ def pg_lshift(self, env_observation: np.ndarray, arguments: IntegerArguments):
ret.append((PG_RETURN, p.MOVE_PTR, (p.MOVE_PTR.PTR_OUT, p.MOVE_PTR.TO_LEFT)))
return ret

def pg_rshift(self, env_observation: np.ndarray, arguments: IntegerArguments):
def pg_rshift(self, env_observation, arguments):
ret = []
p = self.pg_set
ret.append((p.MOVE_PTR, (p.MOVE_PTR.PTR_IN1, p.MOVE_PTR.TO_RIGHT)))
Expand All @@ -228,21 +229,21 @@ def pg_rshift(self, env_observation: np.ndarray, arguments: IntegerArguments):


def create_char_map():
char_map = dict((i+1, "%s" % i) for i in range(10))
char_map = dict((i+1, "%s" % i) for i in xrange(10))
char_map[0] = ' '
return char_map


def create_questions(num=100, max_number=10000):
questions = []
for in1 in range(10):
for in2 in range(10):
for in1 in xrange(10):
for in2 in xrange(10):
questions.append(dict(in1=in1, in2=in2))

for _ in range(100):
for _ in xrange(100):
questions.append(dict(in1=int(random() * 100), in2=int(random() * 100)))

for _ in range(100):
for _ in xrange(100):
questions.append(dict(in1=int(random() * 1000), in2=int(random() * 1000)))

questions += [
Expand All @@ -255,7 +256,7 @@ def create_questions(num=100, max_number=10000):

def create_random_questions(num=100, max_number=10000):
questions = []
for _ in range(num):
for _ in xrange(num):
questions.append(dict(in1=int(random() * max_number), in2=int(random() * max_number)))
return questions

Expand Down
Loading