{"id":20639241,"url":"https://github.com/alexdremov/deepswift","last_synced_at":"2025-06-12T17:07:23.687Z","repository":{"id":146181925,"uuid":"386999674","full_name":"alexdremov/DeepSwift","owner":"alexdremov","description":"Swift autograd library","archived":false,"fork":false,"pushed_at":"2021-07-20T13:37:04.000Z","size":70,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-17T19:50:18.079Z","etag":null,"topics":["algorithm","autograd","backpropagation","graph","matrix","ml","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","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/alexdremov.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-17T17:26:18.000Z","updated_at":"2021-11-07T18:56:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"f7dbde76-0fc6-4815-8cc8-549f86163f3a","html_url":"https://github.com/alexdremov/DeepSwift","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alexdremov/DeepSwift","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdremov%2FDeepSwift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdremov%2FDeepSwift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdremov%2FDeepSwift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdremov%2FDeepSwift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexdremov","download_url":"https://codeload.github.com/alexdremov/DeepSwift/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdremov%2FDeepSwift/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259509430,"owners_count":22868834,"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":["algorithm","autograd","backpropagation","graph","matrix","ml","swift"],"created_at":"2024-11-16T15:23:06.855Z","updated_at":"2025-06-12T17:07:23.645Z","avatar_url":"https://github.com/alexdremov.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DeepSwift\n[![codecov](https://codecov.io/gh/AlexRoar/DeepSwift/branch/main/graph/badge.svg?token=yq5czbVKrC)](https://codecov.io/gh/AlexRoar/DeepSwift)\n\nBuild dynamic computational graph with forward and backward propagation functionality\n\n## Example\n\nSimple SGD optimization:\nFind minimum of sum_{elements} |log(1 + x ^ 2)|\n```swift\nimport DeepSwift\n\nlet variable = Input\u003cFloat\u003e(Matrix\u003cFloat\u003e.random(dim: Shape(row: 5, col: 2), generator: {Float.random(in: -1...1)}), name: \"x\")\nlet function = (variable.pow(2) + ConstNode\u003cFloat\u003e(1)).log().abs()\nvar lossGraph = function.sum()\n            \nlet lr = 0.01\nvar loss = try! lossGraph.forward()\nfor _ in 0..\u003c500 {\n  loss = try! lossGraph.forward()\n  print(loss)\n  try! lossGraph.backward()\n  \n  variable.update(variable.value - lr * variable.grad!)\n  \n}\n\nassert(loss == Matrix(0))\n```\n## Matrix operations\n\n### Bradcasting\nElement-wise operations support broadcasting similarly to numpy\n\n```swift\n(Matrix\u003c1, 5\u003e -broadcasted-\u003e Matrix\u003c5, 5\u003e) * Matrix\u003c5, 5\u003e = Matrix\u003c5, 5\u003e\n\n(Matrix\u003c1, 1\u003e -broadcasted-\u003e Matrix\u003c5, 5\u003e) * Matrix\u003c5, 5\u003e = Matrix\u003c5, 5\u003e\n\n(Matrix\u003c5, 1\u003e -broadcasted-\u003e Matrix\u003c5, 5\u003e) * Matrix\u003c5, 5\u003e = Matrix\u003c5, 5\u003e\n```\n\n### Element-wise multiplication (Hadamard product)\n\n\n```swift\nlet a: Matrix\u003cInt\u003e = [\n            [1, 2, 3],\n            [4, 5, 6],\n            [7, 8, 9]\n]\n\nlet b: Matrix\u003cInt\u003e = [\n            [-1, 2, -3],\n            [4, -5, 6],\n            [-7, 8, -9]\n]\n\na * b == Matrix(internalData: [\n                [1 * -1, 2 * 2 , 3 * -3],\n                [4 * 4 , 5 * -5, 6 * 6 ],\n                [7 * -7, 8 * 8 , 9 * -9],\n            ])\n\n```\n\nIn the same manner supported:\n- Addition\n- Substraction\n- Division\n\n## Graph\n\nComputational graph has several restrictions:\n- Scalar values are 1x1 matrices\n- N-dimensional tensors are not supported\n- Must be directed \u0026 acyclic. You need to make sure that there is no cycles. In case of cyclic graph, forward and backward propagations will run infinitely.\n\n###  Building elements\n\nGraph consists of several types of elements: variables – `Input()`, constants – `ConstNode()`, and operations – `+, -, /, *, **, functions`.\n\nSimple example:\n\n```swift\nlet x = Input\u003cInt\u003e(Matrix(5), name:\"Input variable\")\n\nvar graph:Graph = x * x + 2 * x + 5\n// Integer literals are transformed to ConstNodes\n\nprint(try? graph.forward().as(Int.self) == Matrix\u003cInt\u003e(5 * 5 + 2 * 5 + 5))\n\nx.update(0)\n\nprint(try? graph.forward().as(Int.self) == Matrix\u003cInt\u003e(0 * 0 + 2 * 0 + 5))\n\n```\n\n###  Functions\n\nAlmost all needed function are implemented. The interface for introducig new functions is provoded.\n\nSupported element-wise functions:\n\n- tanh\n- sigmoid\n- abs\n- pow\n- sum\n- reduceMean\n- reduceSum\n- log\n- ReLU\n- ELU\n- LeReLU\n- exp\n\n### Computing gradient\n\nGradient is computed using backpropagation. Forward pass is required before executing backprop\n\n```swift\nlet x = Input\u003cInt\u003e(Matrix(5), name:\"Input variable\")\n\nvar graph:Graph = x * x + 2 * x + 5\n// Integer literals are transformed to ConstNodes\n\ntry? graph.forward()\ntry? graph.backward()\n\nprint(x.grad!.as(Int.self) == Matrix\u003cInt\u003e(2 * 5 + 2))\n// d/dx(x^2 + 2 * x + 5) = 2 * x + 2\n```\n\nPartial derivatives are supported \n\n```swift\nlet x = Input\u003cInt\u003e(Matrix(5), name:\"x\")\nlet y = Input\u003cInt\u003e(Matrix(7), name:\"y\")\n\nvar graph:Graph = x * y + ConstNode\u003cInt\u003e(2) * (x + y) + ConstNode\u003cInt\u003e(5) * y\n// Integer literals are transformed to ConstNodes\n\ntry? graph.forward()\ntry? graph.backward()\n\nprint(x.grad!.as(Int.self) == Matrix\u003cInt\u003e(7 + 2))\nprint(y.grad!.as(Int.self) == Matrix\u003cInt\u003e(5 + 2 + 5))\n// d/dx(x * y + 2 * (x + y) + 5 * y) = y + 2\n// d/dy(x * y + 2 * (x + y) + 5 * y) = x + 2 + 5\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexdremov%2Fdeepswift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexdremov%2Fdeepswift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexdremov%2Fdeepswift/lists"}