Giter Site home page Giter Site logo

jingweiz / pytorch-dnc Goto Github PK

View Code? Open in Web Editor NEW
278.0 23.0 52.0 3.3 MB

Neural Turing Machine (NTM) & Differentiable Neural Computer (DNC) with pytorch & visdom

License: MIT License

Python 100.00%
ntm dnc pytorch visdom external-memory deep-learning

pytorch-dnc's Introduction

Neural Turing Machine (NTM) &

Differentiable Neural Computer (DNC) with

pytorch & visdom


  • Sample on-line plotting while training(avg loss)/testing(write/read weights & memory) NTM on the copy task (top 2 rows, 1st row converges to sequentially write to lower locations, 2nd row converges to sequentially write to upper locations) and DNC on the repeat-copy task (3rd row) (the write/read weights here are after location focus so are no longer necessarily normalized within each head by design):

  • Sample loggings while training DNC on the repeat-copy task (we use WARNING as the logging level currently to get rid of the INFO printouts from visdom):
[WARNING ] (MainProcess) <===================================>
[WARNING ] (MainProcess) bash$: python -m visdom.server
[WARNING ] (MainProcess) http://localhost:8097/env/daim_17051000
[WARNING ] (MainProcess) <===================================> Agent:
[WARNING ] (MainProcess) <-----------------------------======> Env:
[WARNING ] (MainProcess) Creating {repeat-copy | } w/ Seed: 123
[WARNING ] (MainProcess) Word     {length}:   {4}
[WARNING ] (MainProcess) Words #  {min, max}: {1, 2}
[WARNING ] (MainProcess) Repeats  {min, max}: {1, 2}
[WARNING ] (MainProcess) <-----------------------------======> Circuit:    {Controller, Accessor}
[WARNING ] (MainProcess) <--------------------------------===> Controller:
[WARNING ] (MainProcess) LSTMController (
  (in_2_hid): LSTMCell(70, 64, bias=1)
)
[WARNING ] (MainProcess) <--------------------------------===> Accessor:   {WriteHead, ReadHead, Memory}
[WARNING ] (MainProcess) <-----------------------------------> WriteHeads: {1 heads}
[WARNING ] (MainProcess) DynamicWriteHead (
  (hid_2_key): Linear (64 -> 16)
  (hid_2_beta): Linear (64 -> 1)
  (hid_2_alloc_gate): Linear (64 -> 1)
  (hid_2_write_gate): Linear (64 -> 1)
  (hid_2_erase): Linear (64 -> 16)
  (hid_2_add): Linear (64 -> 16)
)
[WARNING ] (MainProcess) <-----------------------------------> ReadHeads:  {4 heads}
[WARNING ] (MainProcess) DynamicReadHead (
  (hid_2_key): Linear (64 -> 64)
  (hid_2_beta): Linear (64 -> 4)
  (hid_2_free_gate): Linear (64 -> 4)
  (hid_2_read_mode): Linear (64 -> 12)
)
[WARNING ] (MainProcess) <-----------------------------------> Memory:     {16(batch_size) x 16(mem_hei) x 16(mem_wid)}
[WARNING ] (MainProcess) <-----------------------------======> Circuit:    {Overall Architecture}
[WARNING ] (MainProcess) DNCCircuit (
  (controller): LSTMController (
    (in_2_hid): LSTMCell(70, 64, bias=1)
  )
  (accessor): DynamicAccessor (
    (write_heads): DynamicWriteHead (
      (hid_2_key): Linear (64 -> 16)
      (hid_2_beta): Linear (64 -> 1)
      (hid_2_alloc_gate): Linear (64 -> 1)
      (hid_2_write_gate): Linear (64 -> 1)
      (hid_2_erase): Linear (64 -> 16)
      (hid_2_add): Linear (64 -> 16)
    )
    (read_heads): DynamicReadHead (
      (hid_2_key): Linear (64 -> 64)
      (hid_2_beta): Linear (64 -> 4)
      (hid_2_free_gate): Linear (64 -> 4)
      (hid_2_read_mode): Linear (64 -> 12)
    )
  )
  (hid_to_out): Linear (128 -> 5)
)
[WARNING ] (MainProcess) No Pretrained Model. Will Train From Scratch.
[WARNING ] (MainProcess) <===================================> Training ...
[WARNING ] (MainProcess) Reporting       @ Step: 500 | Elapsed Time: 30.609361887
[WARNING ] (MainProcess) Training Stats:   avg_loss:         0.014866309287
[WARNING ] (MainProcess) Evaluating      @ Step: 500
[WARNING ] (MainProcess) Evaluation        Took: 1.6457400322
[WARNING ] (MainProcess) Iteration: 500; loss_avg: 0.0140423600748
[WARNING ] (MainProcess) Saving Model    @ Step: 500: /home/zhang/ws/17_ws/pytorch-dnc/models/daim_17051000.pth ...
[WARNING ] (MainProcess) Saved  Model    @ Step: 500: /home/zhang/ws/17_ws/pytorch-dnc/models/daim_17051000.pth.
[WARNING ] (MainProcess) Resume Training @ Step: 500
...

What is included?

This repo currently contains the following algorithms:

  • Neural Turing Machines (NTM) [1]
  • Differentiable Neural Computers (DNC) [2]

Tasks:

  • copy
  • repeat-copy

Code structure & Naming conventions

NOTE: we follow the exact code structure as pytorch-rl so as to make the code easily transplantable.

  • ./utils/factory.py

We suggest the users refer to ./utils/factory.py, where we list all the integrated Env, Circuit, Agent into Dict's. All of the core classes are implemented in ./core/. The factory pattern in ./utils/factory.py makes the code super clean, as no matter what type of Circuit you want to train, or which type of Env you want to train on, all you need to do is to simply modify some parameters in ./utils/options.py, then the ./main.py will do it all (NOTE: this ./main.py file never needs to be modified).

  • namings

To make the code more clean and readable, we name the variables using the following pattern:

  • *_vb: torch.autograd.Variable's or a list of such objects
  • *_ts: torch.Tensor's or a list of such objects
  • otherwise: normal python datatypes

Dependencies


How to run:

You only need to modify some parameters in ./utils/options.py to train a new configuration.

  • Configure your training in ./utils/options.py:
  • line 12: add an entry into CONFIGS to define your training (agent_type, env_type, game, circuit_type)
  • line 28: choose the entry you just added
  • line 24-25: fill in your machine/cluster ID (MACHINE) and timestamp (TIMESTAMP) to define your training signature (MACHINE_TIMESTAMP), the corresponding model file and the log file of this training will be saved under this signature (./models/MACHINE_TIMESTAMP.pth & ./logs/MACHINE_TIMESTAMP.log respectively). Also the visdom visualization will be displayed under this signature (first activate the visdom server by type in bash: python -m visdom.server &, then open this address in your browser: http://localhost:8097/env/MACHINE_TIMESTAMP)
  • line 28: to train a model, set mode=1 (training visualization will be under http://localhost:8097/env/MACHINE_TIMESTAMP); to test the model of this current training, all you need to do is to set mode=2 (testing visualization will be under http://localhost:8097/env/MACHINE_TIMESTAMP_test).
  • Run:

python main.py


Implementation Notes:

The difference between NTM & DNC is stated as follows in the DNC[2] paper:

Comparison with the neural Turing machine. The neural Turing machine (NTM) was the predecessor to the DNC described in this work. It used a similar architecture of neural network controller with read–write access to a memory matrix, but differed in the access mechanism used to interface with the memory. In the NTM, content-based addressing was combined with location-based addressing to allow the network to iterate through memory locations in order of their indices (for example, location n followed by n+1 and so on). This allowed the network to store and retrieve temporal sequences in contiguous blocks of memory. However, there were several drawbacks. First, the NTM has no mechanism to ensure that blocks of allocated memory do not overlap and interfere—a basic problem of computer memory management. Interference is not an issue for the dynamic memory allocation used by DNCs, which provides single free locations at a time, irrespective of index, and therefore does not require contiguous blocks. Second, the NTM has no way of freeing locations that have already been written to and, hence, no way of reusing memory when processing long sequences. This problem is addressed in DNCs by the free gates used for de-allocation. Third, sequential information is preserved only as long as the NTM continues to iterate through consecutive locations; as soon as the write head jumps to a different part of the memory (using content-based addressing) the order of writes before and after the jump cannot be recovered by the read head. The temporal link matrix used by DNCs does not suffer from this problem because it tracks the order in which writes were made.

We thus make some effort to put those two together in a combined codebase. The classes implemented have the following hierarchy:

  • Agent
    • Env
    • Circuit
      • Controller
      • Accessor
        • WriteHead
        • ReadHead
        • Memory

The part where NTM & DNC differs is the Accessor, where in the code NTM uses the StaticAccessor(may not be an appropriate name but we use this to make the code more consistent) and DNC uses the DynamicAccessor. Both Accessor classes use _content_focus() and _location_focus()(may not be an appropriate name for DNC but we use this to make the code more consistent). The _content_focus() is the same for both classes, but the _location_focus() for DNC is much more complicated as it uses dynamic allocation additionally for write and temporal link additionally for read. Those focus (or attention) mechanisms are implemented in Head classes, and those focuses output a weight vector for each head (write/read). Those weight vectors are then used in _access() to interact with the external memory.

A side note:

The sturcture for Env might look strange as this class was originally designed for reinforcement learning settings as in pytorch-rl; here we use it for providing datasets for supervised learning, so the reward, action and terminal are always left blank in this repo.


Repos we referred to during the development of this repo:


The following paper might be interesting to take a look:)

Neural SLAM: We present an approach for agents to learn representations of a global map from sensor data, to aid their exploration in new environments. To achieve this, we embed procedures mimicking that of traditional Simultaneous Localization and Mapping (SLAM) into the soft attention based addressing of external memory architectures, in which the external memory acts as an internal representation of the environment. This structure encourages the evolution of SLAM-like behaviors inside a completely differentiable deep neural network. We show that this approach can help reinforcement learning agents to successfully explore new environments where long-term memory is essential. We validate our approach in both challenging grid-world environments and preliminary Gazebo experiments. A video of our experiments can be found at: \url{https://goo.gl/RfiSxo}.

@article{zhang2017neural,
  title={Neural SLAM},
  author={Zhang, Jingwei and Tai, Lei and Boedecker, Joschka and Burgard, Wolfram and Liu, Ming},
  journal={arXiv preprint arXiv:1706.09520},
  year={2017}
}


Citation

If you find this library useful and would like to cite it, the following would be appropriate:

@misc{pytorch-dnc,
  author = {Zhang, Jingwei},
  title = {jingweiz/pytorch-dnc},
  url = {https://github.com/jingweiz/pytorch-dnc},
  year = {2017}
}

pytorch-dnc's People

Contributors

jingweiz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytorch-dnc's Issues

Simplest recommended way to add new envs

Hi Jingweiz,

this a great implementation of the DNC, thanks a lot for sharing it. I was wondering what's the recommended/simplest way to add new environments, (from OpenAI)? I guess it would just mean adding them to ./utils/factory.py?

There's an interesting paper I've been reading, Bridging the Gap Between Value and Policy Based Reinforcement Learning, which compares some A3C like algorithms on the six OpenAI algorithmic tasks.

Seems like it would be a nice test for the DNC to train it using A3C on these tasks - I'm interested in both the extra benefit of external memory, and also different ways of training the DNC?

Support Python3

It should be possible to make it so that it works with python2 and python3.
Mainly just a bunch of print "..." --> print("...") changes usually.

Bidirectional DNC

Hello, thank you for this package it has been very helpful to start experimenting with DNCs. I tried to implement bidirectional version of the circuit by using two separate lstm controllers for forward and backward pass respectively, one thing I am not sure of is whether it makes sense to concatenate read vectors for both directions along with hidden layer activations for the input at time T before feeding them to the linear layer. I would appreciate if you could provide feedback:
https://github.com/Rahim16/pytorch-dnc/blob/master/core/circuits/bi_circuit.py

Thank you!
Rahim

应该如何将您的程序作为API使用?

我们希望在学术项目中使用您的代码,需要的功能是基于原有状态运行一步DNC,返回状态和输出。请问哪一个文件实现了这样的功能?

Failed to establish a new connection: [Errno 111] Connection refused',)) Input:

+- - - - - - - - - - 1 1 1 1 1 1 1 1 - - - -+
+- - - - - - - - - - - - - - 1 1 1 1 - - - -+
+- - - - - - - - - - 1 1 1 1 1 1 1 1 - - - -+
+- - - - - - - - - - 1 1 1 1 1 - - - - - - -+
Exception in user code:

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/visdom/init.py", line 228, in _send
data=json.dumps(msg),
File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 110, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 487, in send
raise ConnectionError(e, request=request)
ConnectionError: HTTPConnectionPool(host='localhost', port=8097): Max retries exceeded with url: /events (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7faed051b610>: Failed to establish a new connection: [Errno 111] Connection refused',))
Input:
+- - 1 1 1 - - - 1 1 1 - - - - - - - - - - -+
+- - 1 1 1 1 - 1 1 1 - - - - - - - - - - - -+
+- - - 1 - - 1 1 1 1 - - - - - - - - - - - -+
+- 1 - 1 - 1 1 1 - - 1 - - - - - - - - - - -+

Add Licensing

Hi, great work with this!
What licensing is this repo under?
Would be helpful to know so that I can ensure compatibility with my own personal projects!

Latest PyTorch Issue

I know you mention not supporting python 3, but this looks like just an issue with pytorch.
I have built pytorch from HEAD to use the new features such as broadcasting.

➜  pytorch-dnc git:(master) python main.py 
Traceback (most recent call last):
  File "main.py", line 21, in <module>
    agent.fit_model()
  File "/home/jason/projects/pytorch-dnc/core/agents/sl_agent.py", line 115, in fit_model
    action = self._forward(self.experience.state1)
  File "/home/jason/projects/pytorch-dnc/core/agents/sl_agent.py", line 61, in _forward
    output_vb = self.circuit.forward(Variable(input_ts[i]))
  File "/home/jason/projects/pytorch-dnc/core/circuit.py", line 83, in forward
    self.read_vec_vb = self.accessor.forward(hidden_vb)
  File "/home/jason/projects/pytorch-dnc/core/accessors/static_accessor.py", line 48, in forward
    self.memory.memory_vb = self.write_heads.forward(hidden_vb, self.memory.memory_vb)
  File "/home/jason/projects/pytorch-dnc/core/heads/static_write_head.py", line 35, in forward
    super(StaticWriteHead, self).forward(hidden_vb, memory_vb)
  File "/home/jason/projects/pytorch-dnc/core/heads/static_head.py", line 104, in forward
    self._content_focus(memory_vb)
  File "/home/jason/projects/pytorch-dnc/core/heads/static_head.py", line 40, in _content_focus
    K_vb = batch_cosine_sim(self.key_vb, memory_vb)  # [batch_size x num_heads x mem_hei]
  File "/home/jason/projects/pytorch-dnc/utils/similarities.py", line 15, in batch_cosine_sim
    denominator = torch.sqrt(torch.bmm(u.norm(2, 2).pow(2) + epsilon, v.norm(2, 2).pow(2).transpose(1, 2) + epsilon))
  File "/home/jason/anaconda3/lib/python3.6/site-packages/torch/autograd/variable.py", line 706, in transpose
    return Transpose.apply(self, dim1, dim2)
  File "/home/jason/anaconda3/lib/python3.6/site-packages/torch/autograd/_functions/tensor.py", line 80, in forward
    result = i.transpose(dim1, dim2)
RuntimeError: dimension out of range (expected to be in range of [-2, 1], but got 2)

pytorch-dnc-Question

Hi Dear
Thank you for sharing code of NTM-DNC on github & thanks for the your explanations, I have a problem when trying to test the code, can you help me?
the error is: NameError: name 'raw_input' is not defined
how can i fixed this?
image
your kind attention is highly appreciated in advance.
best regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.