{"id":15642951,"url":"https://github.com/trekhleb/micrograd-ts","last_synced_at":"2025-04-30T11:15:16.332Z","repository":{"id":186583436,"uuid":"671138668","full_name":"trekhleb/micrograd-ts","owner":"trekhleb","description":"🤖 A TypeScript version of karpathy/micrograd — a tiny scalar-valued autograd engine and a neural net on top of it","archived":false,"fork":false,"pushed_at":"2023-09-10T17:28:59.000Z","size":5639,"stargazers_count":69,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-30T15:36:47.370Z","etag":null,"topics":["javascript","machine-learning","neural-networks","neural-networks-from-scratch","typescript"],"latest_commit_sha":null,"homepage":"https://trekhleb.dev/micrograd-ts","language":"TypeScript","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/trekhleb.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":"2023-07-26T16:14:57.000Z","updated_at":"2025-03-11T05:38:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"a51fa574-7bcb-45f2-8cea-88f0e5da4da9","html_url":"https://github.com/trekhleb/micrograd-ts","commit_stats":null,"previous_names":["trekhleb/micrograd-ts"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trekhleb%2Fmicrograd-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trekhleb%2Fmicrograd-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trekhleb%2Fmicrograd-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trekhleb%2Fmicrograd-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trekhleb","download_url":"https://codeload.github.com/trekhleb/micrograd-ts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251687597,"owners_count":21627597,"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":["javascript","machine-learning","neural-networks","neural-networks-from-scratch","typescript"],"created_at":"2024-10-03T11:58:19.662Z","updated_at":"2025-04-30T11:15:16.310Z","avatar_url":"https://github.com/trekhleb.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Micrograd TS\n\n\u003e This is a TypeScript version of [karpathy/micrograd](https://github.com/karpathy/micrograd) repo.\u003cbr /\u003e\n\u003e A tiny scalar-valued autograd engine and a neural net on top of it ([~200 lines](./micrograd/) of TS code).\n\nThis repo might be useful for those who want to get a basic understanding of how neural networks work, using a TypeScript environment for experimentation.  \n\n## Project structure\n\n- [micrograd/](./micrograd/) — this folder is the core/purpose of the repo\n  - [engine.ts](./micrograd/engine.ts) — the scalar `Value` class that supports basic math operations like `add`, `sub`, `div`, `mul`, `pow`, `exp`, `tanh` and has a `backward()` method that calculates a derivative of the expression, which is required for back-propagation flow.\n  - [nn.ts](./micrograd/nn.ts) — the `Neuron`, `Layer`, and `MLP` (multi-layer perceptron) classes that implement a neural network on top of the differentiable scalar `Values`.\n- [demo/](./demo/) - demo React application to experiment with the micrograd code\n  - [src/demos/](./demo/src/demos/) - several playgrounds where you can experiment with the `Neuron`, `Layer`, and `MLP` classes.\n\n## Micrograd\n\nSee the 🎬 [The spelled-out intro to neural networks and back-propagation: building micrograd](https://www.youtube.com/watch?v=VMj-3S1tku0) YouTube video for the detailed explanation of how neural networks and back propagation work. The video also explains in detail what the `Neuron`, `Layer`, `MLP`, and `Value` classes do.\n\nBriefly, the `Value` class allows you to build a computation graph for some expression that consists of scalar values.\n\nHere is an example of how the computation graph for the `a * b + c` expression looks like:\n\n![graph-1.png](./demo/src/assets/graph-1.png)\n\nBased on the `Value` class we can build a `Neuron` expression `X * W + b`. Here we're simulating a dot-product of matrix `X` (input features) and matrix `W` (neuron weights):\n\n![graph-2.png](./demo/src/assets/graph-2.png)\n\nOut of `Neurons`, we can build the `MLP` network class that consists of several `Layers` of `Neurons`. The computation graph in this case may look a bit complex to be displayed here, but a simplified version might look like this:\n\n![graph-3.png](./demo/src/assets/graph-3.png)\n\nThe main idea is that the computation graphs above \"know\" how to do automatic back propagation (in other words, how to calculate derivatives). This allows us to train the MLP network for several epochs and adjust the network weights in a way that reduces the ultimate loss:\n\n![training-1.gif](./demo/src/assets/training-1.gif)\n\n## Demo (online)\n\nTo see the online demo/playground, check the following link:\n\n🔗 [trekhleb.dev/micrograd-ts](https://trekhleb.dev/micrograd-ts)\n\n## Demo (local)\n\nIf you want to experiment with the code locally, follow the instructions below.\n\n### Setup\n\nClone the current repo locally.\n\nSwitch to the demo folder:\n\n```sh\ncd ./demo\n```\n\nSetup node v18 using [nvm](https://github.com/nvm-sh/nvm) (optional):\n\n```sh\nnvm use\n```\n\nInstall dependencies:\n\n```sh\nnpm i\n```\n\nLaunch demo app:\n\n```sh\nnpm run dev\n```\n\nThe demo app will be available at [http://localhost:5173/micrograd-ts](http://localhost:5173/micrograd-ts)\n\n### Playgrounds\n\nGo to the [./demo/src/demos/](./demo/src/demos/) to explore several playgrounds for the `Neuron`, `Layer`, and `MLP` classes.\n\n# Author\n\nThe TypeScript version of the [karpathy/micrograd](https://github.com/karpathy/micrograd) repo by [@trekhleb](https://trekhleb.dev)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrekhleb%2Fmicrograd-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrekhleb%2Fmicrograd-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrekhleb%2Fmicrograd-ts/lists"}