{"id":15649667,"url":"https://github.com/siddk/npi","last_synced_at":"2025-07-21T00:33:13.437Z","repository":{"id":40523823,"uuid":"77253416","full_name":"siddk/npi","owner":"siddk","description":"Neural Programmer-Interpreter Implementation (Reed, de Freitas: https://arxiv.org/abs/1511.06279), in Tensorflow","archived":false,"fork":false,"pushed_at":"2018-11-17T23:50:34.000Z","size":10095,"stargazers_count":41,"open_issues_count":3,"forks_count":19,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-30T17:51:29.995Z","etag":null,"topics":["hidden-states","lstm","neural-network","neural-programmers","neural-turing-machines","npi","paper","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/siddk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-23T22:17:47.000Z","updated_at":"2025-01-17T13:07:26.000Z","dependencies_parsed_at":"2022-09-23T11:49:35.204Z","dependency_job_id":null,"html_url":"https://github.com/siddk/npi","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/siddk/npi","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siddk%2Fnpi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siddk%2Fnpi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siddk%2Fnpi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siddk%2Fnpi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/siddk","download_url":"https://codeload.github.com/siddk/npi/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siddk%2Fnpi/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266221316,"owners_count":23894966,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hidden-states","lstm","neural-network","neural-programmers","neural-turing-machines","npi","paper","tensorflow"],"created_at":"2024-10-03T12:30:47.164Z","updated_at":"2025-07-21T00:33:08.429Z","avatar_url":"https://github.com/siddk.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Neural Programmer-Interpreters\n\nNeural Programmer-Interpreter Implementation, in Tensorflow (with TFLearn). Based on the original\n[Neural Programmer-Interpreter](https://arxiv.org/abs/1511.06279) paper, by Reed and de Freitas.\n\n## NPI Overview ##\n\nA Neural Programmer-Interpreter can be decomposed into the following components (each of which are\nimplemented either in `npi.py`, or `[task-name].py`:\n\n+ **Neural Programmer-Interpreter Core**: Simple LSTM Network (**f_lstm**), with hidden states *h_t*, *c_t*\n    - Behaves like a traffic controller ==\u003e Based on task-specific encoder inputs, serves as a \n      router, outputting high-dimensional information that encodes information about which \n      subroutine to call next, and with what arguments.\n    - Shared across all tasks ==\u003e only shared, central component.\n\n+ **Task-Specific Encoder Network**: Architecture depends on specific task, but can also be trained \n                                 via gradient descent (**f_enc**). \n    - Given the task environment, and previous subroutine arguments, generates a fixed-length \n      state encoding s_t\n\n+ **Program Termination Network**: Feed-Forward Network (**f_end**), takes LSTM Controller hidden state \n                               *h_t* and outputs a probability of terminating execution.\n    - Paper uses a threshold value of 0.5 to determine whether to stop, and terminate, or \n      call next subroutine.\n\n+ **Subroutine Lookup Network**: Feed-Forward Network (**f_prog**), takes LSTM Controller hidden state \n                             *h_t* and outputs a key embedding *k_t* to look up next subroutine to \n                             be called.\n    - Subroutine information is stored in two matrices, M_key (N x K), and M_prog (N x P), where \n      each of the N rows denotes a different subroutine, and where K and P are the \n      dimensionality of the key and program embeddings respectively. \n    - The next subroutine is chosen as follows:\n        + i* = argmax_i (M_i_key)^T k_t ==\u003e Cosine similarity between the predicted key embedding \n                                           and each of the program keys in M_key\n        + p_(t + 1) = M_i*_prog\n    - Note: To generate probabilities of next program, for training the network, I took a\n            softmax over the array of cosine similarities, and used those as my next-program\n            probabilities.\n\n+ **Argument Networks**: Feed-Forward Networks (**f_arg**), takes LSTM Controller hidden state h_t and \n                     outputs subroutine arguments a_(t + 1).\n    - Note: In this implementation, I built separate networks for each of the three arguments.\n        \n### Directory Structure ###\n    + model/\n        - npi.py: Core model definition for the Neural-Programmer Interpreter. Builds the shared\n        NPI LSTM Controller, the Termination Network, the Program ID Network, as well as the \n        specific Argument Networks.\n        \n    + tasks/\n        - [task-name]/\n            + data/ - Contains training/test execution traces, stored in Python serialized (.pik)\n                      format. Each .pik file contains a list of examples, where each example is \n                      stored as a triple (in1, in2, trace), where in1/in2 are the numbers to be \n                      added, and trace contains the specific execution trace.\n            + env/ - Contains the environment/task-specific code, including any Task Configuration\n                     parameters, trace-building helpers, etc.\n            + [task-name].py - Contains model definition code for the Task-Specific Core - contains\n                               task-specific TF placeholders, as well as the [f_enc] encoder \n                               environment-encoder network.\n            + train.py - Task-Specific Training Script - implements train_task() function.\n            + eval.py - Task-Specific Evaluation Script - implements evaluate_task() function.\n    \n    + main.py - Core runner, accepts TF Flags for generating data (with specified number of \n                examples), training, saving, and evaluating model.\n                \n### Evaluation Procedure ###\n\nTo test addition-task NPI code interactively, just call `python main.py` from the root of this\ndirectory, provided that you have cloned the saved model checkpoints in `tasks/addition/log/`. \nThis will drop you into a REPL, where you can enter two numbers to be added, and step through the\npredicted execution trace. \n\nNote that for the time being, numbers much be smaller than 1000000000. This is not because of any \nlimitations on the part of the NPI, but because of the backend helper functions that display the \ntrace.\n\nA sample execution trace for the addition `18 + 7 = 25` can be found below. Note that this is trace\nis produced entirely by the NPI =\u003e There is are no external guides provided except for the initial\ncall to \"ADD 18 7\":\n\n```\n    Enter Two Numbers, or Hit Enter for Random Pair: 18 7\n    \n    Step: ADD, Arguments: [], Terminate: False\n    IN 1: -1, IN 2: -1, CARRY: -1, OUT: -1\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000000\n    -----------------------\n    Output :     0000000000\n    \n    Continue? c\n    Step: ADD1, Arguments: [], Terminate: False\n    IN 1: -1, IN 2: -1, CARRY: -1, OUT: -1\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000000\n    -----------------------\n    Output :     0000000000\n    \n    Continue? c\n    Step: WRITE, Arguments: [OUT, 5], Terminate: False\n    IN 1: -1, IN 2: -1, CARRY: -1, OUT: -1\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000000\n    -----------------------\n    Output :     0000000005\n    \n    Continue? c\n    Step: CARRY, Arguments: [], Terminate: False\n    IN 1: -1, IN 2: -1, CARRY: -1, OUT: -1\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000000\n    -----------------------\n    Output :     0000000005\n    \n    Continue? c\n    Step: MOVE_PTR, Arguments: [CARRY_PTR, LEFT], Terminate: False\n    IN 1: -1, IN 2: -1, CARRY: -1, OUT: -1\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000000\n    -----------------------\n    Output :     0000000005\n    \n    Continue? c\n    Step: WRITE, Arguments: [CARRY, 1], Terminate: False\n    IN 1: -1, IN 2: -1, CARRY: -2, OUT: -1\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000010\n    -----------------------\n    Output :     0000000005\n    \n    Continue? c\n    Step: MOVE_PTR, Arguments: [CARRY_PTR, RIGHT], Terminate: False\n    IN 1: -1, IN 2: -1, CARRY: -2, OUT: -1\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000010\n    -----------------------\n    Output :     0000000005\n    \n    Continue? c\n    Step: MOVE_PTR, Arguments: [IN1_PTR, LEFT], Terminate: False\n    IN 1: -1, IN 2: -1, CARRY: -1, OUT: -1\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000010\n    -----------------------\n    Output :     0000000005\n    \n    Continue? c\n    Step: MOVE_PTR, Arguments: [IN2_PTR, LEFT], Terminate: False\n    IN 1: -2, IN 2: -1, CARRY: -1, OUT: -1\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000010\n    -----------------------\n    Output :     0000000005\n    \n    Continue? c\n    Step: MOVE_PTR, Arguments: [CARRY_PTR, LEFT], Terminate: False\n    IN 1: -2, IN 2: -2, CARRY: -1, OUT: -1\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000010\n    -----------------------\n    Output :     0000000005\n    \n    Continue? c\n    Step: MOVE_PTR, Arguments: [OUT_PTR, LEFT], Terminate: False\n    IN 1: -2, IN 2: -2, CARRY: -2, OUT: -1\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000010\n    -----------------------\n    Output :     0000000005\n    \n    Continue? c\n    Step: ADD1, Arguments: [], Terminate: False\n    IN 1: -2, IN 2: -2, CARRY: -2, OUT: -2\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000010\n    -----------------------\n    Output :     0000000005\n    \n    Continue? c\n    Step: WRITE, Arguments: [OUT, 2], Terminate: False\n    IN 1: -2, IN 2: -2, CARRY: -2, OUT: -2\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000010\n    -----------------------\n    Output :     0000000025\n    \n    Continue? c\n    Step: MOVE_PTR, Arguments: [IN1_PTR, LEFT], Terminate: False\n    IN 1: -2, IN 2: -2, CARRY: -2, OUT: -2\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000010\n    -----------------------\n    Output :     0000000025\n    \n    Continue? c\n    Step: MOVE_PTR, Arguments: [IN2_PTR, LEFT], Terminate: False\n    IN 1: -3, IN 2: -2, CARRY: -2, OUT: -2\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000010\n    -----------------------\n    Output :     0000000025\n    \n    Continue? c\n    Step: MOVE_PTR, Arguments: [CARRY_PTR, LEFT], Terminate: False\n    IN 1: -3, IN 2: -3, CARRY: -2, OUT: -2\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000010\n    -----------------------\n    Output :     0000000025\n    \n    Step: MOVE_PTR, Arguments: [CARRY_PTR, LEFT], Terminate: True\n    IN 1: -3, IN 2: -3, CARRY: -3, OUT: -2\n    Input 1:     0000000018\n    Input 2:     0000000007\n    Carry  :     0000000010\n    -----------------------\n    Output :     0000000025\n    \n    Model Output: 18 + 7 = 25\n    Correct Out : 18 + 7 = 25\n    Correct!\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiddk%2Fnpi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsiddk%2Fnpi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiddk%2Fnpi/lists"}