{"id":17283474,"url":"https://github.com/alejandro-isaza/braincore","last_synced_at":"2025-04-07T09:19:25.960Z","repository":{"id":56903968,"uuid":"44200363","full_name":"alejandro-isaza/BrainCore","owner":"alejandro-isaza","description":"The iOS and OS X neural network framework","archived":false,"fork":false,"pushed_at":"2017-03-11T19:56:30.000Z","size":262,"stargazers_count":380,"open_issues_count":6,"forks_count":49,"subscribers_count":35,"default_branch":"master","last_synced_at":"2024-04-14T00:21:12.853Z","etag":null,"topics":["ios","machine-learning","metal","swift"],"latest_commit_sha":null,"homepage":null,"language":"Swift","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/alejandro-isaza.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}},"created_at":"2015-10-13T19:42:35.000Z","updated_at":"2024-03-16T17:03:03.000Z","dependencies_parsed_at":"2022-08-21T01:50:53.359Z","dependency_job_id":null,"html_url":"https://github.com/alejandro-isaza/BrainCore","commit_stats":null,"previous_names":["aleph7/braincore"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alejandro-isaza%2FBrainCore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alejandro-isaza%2FBrainCore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alejandro-isaza%2FBrainCore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alejandro-isaza%2FBrainCore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alejandro-isaza","download_url":"https://codeload.github.com/alejandro-isaza/BrainCore/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247622983,"owners_count":20968575,"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":["ios","machine-learning","metal","swift"],"created_at":"2024-10-15T09:51:22.072Z","updated_at":"2025-04-07T09:19:25.937Z","avatar_url":"https://github.com/alejandro-isaza.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BrainCore\n\n[![CocoaPods Compatible](https://img.shields.io/cocoapods/v/BrainCore.svg)](https://img.shields.io/cocoapods/v/BrainCore.svg)\n[![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n\nBrainCore is a simple but fast neural network framework written in Swift. It uses [Metal](https://developer.apple.com/metal/) which makes it screamin' fast. If you want to see it in action check out [InfiniteMonkeys](https://github.com/craigomac/InfiniteMonkeys)—an app that uses a recursive neural network to generate poems.\n\n\n## Features\n\n- [x] Inner product layers\n- [x] Linear rectifier (ReLU) layers\n- [x] Sigmoid layers\n- [x] LSTM layers\n- [x] L2 Loss layers\n\n\n## Requirements\n\n- iOS 8.0+ / Mac OS X 10.11+\n- Xcode 7.2+\n- A device that supports Metal (doesn't work on the iOS simulator)\n\n## Usage\n\n### Network Definition\n\nBefore you build your network, start by building all the layers. This is as simple as calling each constructor:\n\n```swift\nlet dataLayer = MyDataLayer()\nlet lstmLayer = LSTMLayer(weights: lstmWeights, biases: lstmBiases)\nlet ipLayer = InnerProductLayer(weights: ipWeights, biases: ipBiases)\nlet reluLayer = ReLULayer(size: ipBiases.count)\nlet sinkLayer = MySinkLayer()\n```\n\n**BrainCore** uses overloaded operators to make network definitions more concise. To connect layers together simply use the `=\u003e` operator inside a `Net.build {}` closure: \n\n```swift\nlet net = Net.build {\n    dataLayer =\u003e lstmLayer =\u003e ipLayer =\u003e reluLayer =\u003e sinkLayer\n}\n```\n\nIf you need to concatenate the output of two layers put them inside square brackets:\n\n```swift\nlet net = Net.build {\n    [dataLayer1, dataLayer2] =\u003e lstmLayer =\u003e ipLayer =\u003e reluLayer =\u003e sinkLayer\n}\n```\n\nSimilarly, if you need to split the output of one layer put its target layers in square brackets:\n\n```swift\nlet net = Net.build {\n    dataLayer =\u003e lstmLayer =\u003e ipLayer =\u003e reluLayer =\u003e [sinkLayer1, sinkLayer2]\n}\n```\n\nWhen splitting, the `inputSize` of the target layers will determine where to split. If the sum of the target layers' `inputSize`s doesn't match the source layer's `outputSize` and error will be thrown.\n\n\nIf you want to continue on separate branches after a split you have to split the definition into separate lines:\n```swift\nlet net = Net.build {\n    dataLayer =\u003e lstmLayer =\u003e [ipLayer1, ipLayer2]\n    ipLayer1 =\u003e reluLayer1 =\u003e sinkLayer1\n    ipLayer2 =\u003e reluLayer2 =\u003e sinkLayer2\n}\n```\n\nFinally if you want send multiple copies of the output of a layer to different layers use the `=\u003e\u003e` operator:\n```swift\nlet net = Net.build {\n    dataLayer =\u003e lstmLayer\n    lstmLayer =\u003e\u003e ipLayer1 =\u003e reluLayer1 =\u003e sinkLayer1\n    lstmLayer =\u003e\u003e ipLayer2 =\u003e reluLayer2 =\u003e sinkLayer2\n}\n```\n\n### Evaluating\n\nCurrently **BrainCore** only supports executing pre-trained networks. Ideally you would train your network on a server using one of the well-established neural network frameworks and import the trained weights into BrainCore. We are working on implementing solvers so that you can do everything inside **BrainCore**, stay posted.\n\nLet's start by creating the layers.\n\n```swift\n// Load weights and biases from a pre-trained network\nlet lstmWeights = ...\nlet lstmBiases = ...\nlet ipWeights = ...\nlet ipBiases = ...\n\n// Create layers\nlet dataLayer = MyDataLayer()\nlet lstmLayer = LSTMLayer(weights: lstmWeights, biases: lstmBiases)\nlet ipLayer = InnerProductLayer(weights: ipWeights, biases: ipBiases)\nlet reluLayer = ReLULayer(size: ipBiases.count)\nlet sinkLayer = MySinkLayer()\n```\n\nNext we'll build the net.\n\n```swift\nlet net = Net.build {\n    dataLayer =\u003e lstmLayer =\u003e ipLayer =\u003e reluLayer =\u003e sinkLayer\n}\n```\n\nAnd finally execute! You need to provide a Metal device to the runner which is usually just the default device. \n\n```swift\nguard let device = MTLCreateSystemDefaultDevice() else {\n    fatalError(\"Failed to create a Metal device.\")\n}\n\nlet evaluator: Evaluator\ndo {\n    evaluator = try Evaluator(net: net, device: device)\n} catch let e {\n    fatalError(\"Failed to create an Evaluator: \\(e)\")\n}\n\nevaluator.evaluate { snapshot in\n    print(\"Feed-forward pass complete!\")\n}\n```\n\nThe evaluator may fail to build if there is any problem creating the buffers or initializing all the Metal code, that's why there is a `try`.\n\nCalling `evaluate()` will execute a single forward pass, but you can call this as often as you want. In fact you will want to call `evaluate()` multiple times before you get any results back so that you maximise the GPU bandwidth. You can also increase the batch size to execute multiple passes in parallel.\n\nYour data layer will most likely want to provide new data every time you call `evaluate()`. So your code may look something like\n\n```swift\nwhile !shouldStop {\n    dataLayer.gather()\n    evaluator.evaluate(completion)\n}\n```\n\n**Note:** both the sink layer's `consume()` function and the completion closure will be called from a background thread. Make sure you synchronize access to the data as needed and try not to block on either of those calls for too long.\n\n\n---\n\n## License\n\nUpsurge is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falejandro-isaza%2Fbraincore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falejandro-isaza%2Fbraincore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falejandro-isaza%2Fbraincore/lists"}