{"id":15654392,"url":"https://github.com/cpury/lstm-math","last_synced_at":"2025-04-30T23:39:01.086Z","repository":{"id":23297155,"uuid":"98656043","full_name":"cpury/lstm-math","owner":"cpury","description":"Neural network that solves math equations on the character level","archived":false,"fork":false,"pushed_at":"2023-03-25T01:01:59.000Z","size":1893,"stargazers_count":27,"open_issues_count":2,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-30T23:38:48.242Z","etag":null,"topics":["deep-learning","keras","lstm","machine-learning","python","seq2seq","tensorflow"],"latest_commit_sha":null,"homepage":"http://cpury.github.io/learning-math/","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cpury.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-28T14:13:18.000Z","updated_at":"2025-03-21T16:06:40.000Z","dependencies_parsed_at":"2024-10-23T07:47:08.421Z","dependency_job_id":null,"html_url":"https://github.com/cpury/lstm-math","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cpury%2Flstm-math","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cpury%2Flstm-math/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cpury%2Flstm-math/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cpury%2Flstm-math/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cpury","download_url":"https://codeload.github.com/cpury/lstm-math/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251800994,"owners_count":21645966,"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":["deep-learning","keras","lstm","machine-learning","python","seq2seq","tensorflow"],"created_at":"2024-10-03T12:51:28.336Z","updated_at":"2025-04-30T23:39:01.057Z","avatar_url":"https://github.com/cpury.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lstm-math\n\nSee the accompanying blog post here: http://cpury.github.io/learning-math/\n\nSimply try it yourself in Google Colab: https://colab.research.google.com/github/cpury/lstm-math/blob/master/lstm_math.ipynb (**NEW**)\n\nTrain a LSTM-based Seq2Seq network to predict the result of math equations on\nthe character level.\nConfiguration through global variables because I'm lazy.\n\nWritten by Max Schumacher (@cpury) in Summer 2017. Updated in Summer 2019.\n\n\n## Preview\n\nWill learn how to complete simple math formulas. E.g. during training, you will\nget samples like this:\n\n```\nExamples:\n  67 + 38 =  108   (expected:  105)\n  15 + 49 =   69   (expected:   64)\n  84 - 91 =   -5   (expected:   -7)\n  71 + 53 =  123   (expected:  124)\n  72 -  1 =   75   (expected:   71)\n```\n\nAnd ideally, after an half an hour CPU-time or so, it will learn (almost)\nperfectly to get the right results :)\n\nMaybe something like:\n\n```\nExamples:\n  36 + 55 =  91   (expected:  91)\n  32 + 45 =  77   (expected:  77)\n  15 + 93 = 108   (expected: 108)\n  41 + 82 = 123   (expected: 123)\n   4 + 89 =  93   (expected:  93)\n```\n\n\n## Details\n\nThis uses a Seq2Seq model based on LSTMs in Keras. Depending on the complexity\nof equations you choose, it will train on some small percentage of the complete\nequation space and validate on another small percentage. So all the equations you\nsee in the example above have not been seen by the network before.\n\n\n## Running it yourself\n\nPlease note that I wrote this for Python 3.6+. It will probably work with 2.7+,\nbut I don't offer any guarantees on that.\n\n0. Set up a virtual env: `virtualenv venv; source venv/bin/activate`\n1. Install the requirements: `pip install -r requirements.txt`\n2. *Optional:* Open `main.py` and play around with the global config values.\n3. `python main.py`\n\nYou can cancel the training any time with `ctrl+c`. It will always output some\nexamples at the end and store the model in `model.h5`, even if you cancel.\n\nActually, since it's not stopping when overfitting has begun, it might actually\nbe good to hit `ctr+c` once you're happy with the results.\n\n\n## Playing with the config\n\nAll the config values at the top of `main.py` should be fairly\nself-explanatory. You could e.g.\n\n* Change the math formulas to be trained on:\n  * Change `MAX_NUMBER` to allow higher values in the equations\n  * Work with negative numbers by setting `MIN_NUMBER` to something negative\n  * Add multiplication by adding `'*'` to `OPERATIONS`\n  * Have more operations per equation by increasing `N_OPERATIONS`\n  * etc.\n* Change the model to be trained:\n  * Change the size of the hidden layer(s) with `HIDDEN_SIZE`\n  * Make the encoder deeper with `ENCODER_DEPTH`\n  * Make the decoder deeper with `DECODER_DEPTH`\n  * Use dropout with `DROPOUT`\n  * etc.\n* Reverse all strings to overcome the digit ordering problem with `REVERSE`\n  (see the blog post for more info)\n\n\n## Feedback\n\nFeel free to submit issues if you find bugs or room for improvement.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcpury%2Flstm-math","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcpury%2Flstm-math","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcpury%2Flstm-math/lists"}