{"id":15633444,"url":"https://github.com/philipperemy/tensorflow-multi-dimensional-lstm","last_synced_at":"2025-04-14T13:12:31.362Z","repository":{"id":70249867,"uuid":"94387656","full_name":"philipperemy/tensorflow-multi-dimensional-lstm","owner":"philipperemy","description":"Multi dimensional LSTM as described in Alex Graves' Paper https://arxiv.org/pdf/0705.2011.pdf","archived":false,"fork":false,"pushed_at":"2020-03-30T14:40:48.000Z","size":1162,"stargazers_count":156,"open_issues_count":1,"forks_count":46,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-03-28T02:21:36.578Z","etag":null,"topics":["2d-lstm","grid-lstm","lstm","multi-dimensional","recurrent-networks","tensroflow"],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/philipperemy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":["philipperemy"]}},"created_at":"2017-06-15T01:37:08.000Z","updated_at":"2025-03-21T16:06:34.000Z","dependencies_parsed_at":"2023-03-04T00:00:57.760Z","dependency_job_id":null,"html_url":"https://github.com/philipperemy/tensorflow-multi-dimensional-lstm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipperemy%2Ftensorflow-multi-dimensional-lstm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipperemy%2Ftensorflow-multi-dimensional-lstm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipperemy%2Ftensorflow-multi-dimensional-lstm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philipperemy%2Ftensorflow-multi-dimensional-lstm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/philipperemy","download_url":"https://codeload.github.com/philipperemy/tensorflow-multi-dimensional-lstm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248886325,"owners_count":21177644,"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":["2d-lstm","grid-lstm","lstm","multi-dimensional","recurrent-networks","tensroflow"],"created_at":"2024-10-03T10:49:20.519Z","updated_at":"2025-04-14T13:12:31.310Z","avatar_url":"https://github.com/philipperemy.png","language":"Jupyter Notebook","funding_links":["https://github.com/sponsors/philipperemy"],"categories":[],"sub_categories":[],"readme":"# Multi Dimensional Recurrent Networks\nTensorflow Implementation of the model described in Alex Graves' paper https://arxiv.org/pdf/0705.2011.pdf.\n\n- [x] **Fully compatible with Tensorflow 1.7.**\n- [x] **Only 2D is supported now.**\n- [x] **[Comes with some big limitations](https://github.com/philipperemy/tensorflow-multi-dimensional-lstm#limitations).**\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"assets/2d_lstm_1.png\" width=\"100\"\u003e\n  \u003cbr\u003e\u003ci\u003eExample: 2D LSTM Architecture\u003c/i\u003e\n\u003c/p\u003e\n\n## What is MD LSTM?\n\nBasically a LSTM that is multidirectional, for example, that can operate on a 2D grid. Here's a figure describing the way it works:\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"assets/2d_lstm_1.png\" width=\"300\"\u003e\n  \u003cbr\u003e\u003ci\u003eExample: 2D LSTM Architecture\u003c/i\u003e\n\u003c/p\u003e\n\n## How to get started?\n```bash\ngit clone git@github.com:philipperemy/tensorflow-multi-dimensional-lstm.git\ncd tensorflow-multi-dimensional-lstm\n\n# create a new virtual python environment\nvirtualenv -p python3 venv\nsource venv/bin/activate\npip install -r requirements.txt\n\n# usage: trainer.py [-h] --model_type {MD_LSTM,HORIZONTAL_SD_LSTM,SNAKE_SD_LSTM}\npython trainer.py --model_type MD_LSTM\npython trainer.py --model_type HORIZONTAL_SD_LSTM\npython trainer.py --model_type SNAKE_SD_LSTM\n```\n\n## Random diagonal Task\n\nThe random diagonal task consists in initializing a matrix with values very close to 0 except two which are set to 1. Those two values are on a straight line parallel to the diagonal of the matrix. The idea is to predict where those two values are. Here are some examples:\n\n```\n____________\n|          |\n|x         |\n| x        |\n|          |\n|__________|\n\n\n____________\n|          |\n|          |\n|     x    |\n|      x   |\n|__________|\n\n____________\n|          |\n| x        |\n|  x       |\n|          |\n|__________|\n\n```\n\nA model performing on this task is considered as successful if it can correctly predict the second x (it's impossible to predict the first x).\n\n- A simple recurrent model going vertical or horizontal cannot predict any locations of x. This model is called `HORIZONTAL_SD_LSTM`. It should perform the worst.\n- If the matrix is flattened as one single vector, then the first location of x still cannot be predicted. However, a recurrent model should understand that the second x always comes after the first x (width+1 steps). (Model is `SNAKE_SD_LSTM`).\n- When predicting the second location of x, a MD recurrent model has a full view of the TOP LEFT corner. In that case, it should understand that when the first x is in the bottom right of its window, the second x will be next on the diagonal axis. Of course the first location x still cannot be predicted at all with this MD model.\n\nAfter training on this task for 8x8 matrices, the losses look like this:\n\n\u003cp align=\"center\"\u003e\n  \u003cb\u003eOverall loss of the random diagonal task (loss applied on all the elements of the inputs)\u003c/b\u003e\u003cbr\u003e\u003cbr\u003e\n  \u003cimg src=\"assets/md_lstm2.png\" width=\"500\" /\u003e\n\u003c/p\u003e\n\n\n\u003cp align=\"center\"\u003e\n  \u003cb\u003eOverall loss of the random diagonal task (loss applied only on the location of the second x)\u003c/b\u003e\u003cbr\u003e\u003cbr\u003e\n  \u003cimg src=\"assets/md_lstm.png\" width=\"500\" /\u003e \n\u003c/p\u003e\n\nNo surprise that MD LSTM performs the best here. It has direct connections between the grid cell that contains the first x and the second x (2 connections). The snake LSTM has width+1 = 9 steps between the two x. As expected, the vertical LSTM does not learn anything apart from outputting values very close to 0.\n\n\n\u003cp float=\"left\"\u003e\n  \u003cimg src=\"assets/md_pred_0.png\" width=\"300\" /\u003e\n  \u003cimg src=\"assets/md_truth_0.png\" width=\"300\" /\u003e \n  \u003cbr\u003e\u003ci\u003eMD LSTM predictions (left) and ground truth (right) before training (predictions are all random).\u003c/i\u003e\n\u003c/p\u003e\n\n\u003cp float=\"left\"\u003e\n  \u003cimg src=\"assets/md_pred_500.png\" width=\"300\" /\u003e\n  \u003cimg src=\"assets/md_truth_500.png\" width=\"300\" /\u003e \n  \u003cbr\u003e\u003ci\u003eMD LSTM predictions (left) and ground truth (right) after training. As expected, the MD LSTM can only predict the second x and not the first one. That means the task is correctly predicted.\u003c/i\u003e\n\u003c/p\u003e\n\n## Limitations\n- I could test it successfully with 32x32 matrices but the implementation is far from being well optimised.\n- This implementation can become numerically unstable quite easily.\n- I've noticed that inputs should be != 0. Otherwise some gradients are nan. So consider `inputs += eps` in case.\n- It's hard to use in Keras. This implementation is in pure tensorflow.\n- It runs on a GPU but the code is not optimized at all so I would say it's equally fast (CPU vs GPU).\n\n## Contributions\n\nWelcome!\n\n\n## Special Thanks\n- A big *thank you* to [Mosnoi Ion](https://stackoverflow.com/questions/42071074/multidimentional-lstm-tensorflow) who provided the first skeleton of this MD LSTM.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilipperemy%2Ftensorflow-multi-dimensional-lstm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphilipperemy%2Ftensorflow-multi-dimensional-lstm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilipperemy%2Ftensorflow-multi-dimensional-lstm/lists"}