{"id":20041005,"url":"https://github.com/lovesaroha/lnn","last_synced_at":"2026-06-06T18:31:14.646Z","repository":{"id":219217029,"uuid":"407934350","full_name":"lovesaroha/lnn","owner":"lovesaroha","description":"This is a generalized neural network package with clean and transparent API for the Go language.","archived":false,"fork":false,"pushed_at":"2021-09-26T12:23:13.000Z","size":24,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-12T19:23:23.438Z","etag":null,"topics":["golang","golang-package","gradient-descent","machine-learning","neural-network","sequential-models","tensor"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lovesaroha.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}},"created_at":"2021-09-18T18:11:46.000Z","updated_at":"2021-09-26T12:23:15.000Z","dependencies_parsed_at":"2024-01-26T05:14:19.027Z","dependency_job_id":"3a8aea93-2d63-41e1-808f-35134018072e","html_url":"https://github.com/lovesaroha/lnn","commit_stats":null,"previous_names":["lovesaroha/lnn"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lovesaroha%2Flnn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lovesaroha%2Flnn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lovesaroha%2Flnn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lovesaroha%2Flnn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lovesaroha","download_url":"https://codeload.github.com/lovesaroha/lnn/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241470364,"owners_count":19968041,"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":["golang","golang-package","gradient-descent","machine-learning","neural-network","sequential-models","tensor"],"created_at":"2024-11-13T10:44:52.966Z","updated_at":"2026-06-06T18:31:14.171Z","avatar_url":"https://github.com/lovesaroha.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lnn\nThis is a generalized neural network package with clean and transparent API for the Go language. Also available for javascript [github/lovesaroha/lnn.js](https://github.com/lovesaroha/lnn.js) \n\n## Features\n- Lightweight and Fast.\n- Native Go implementation.\n- Tensor Operations.\n- Sequential Models.\n- Support loss functions like (Mean Square Error).\n- Opitmization algorithms like (Gradient Descent).\n\n## Requirements\n- Go 1.9 or higher. We aim to support the 3 latest versions of Go.\n\n## Installation\nSimple install the package to your [$GOPATH](https://github.com/golang/go/wiki/GOPATH \"GOPATH\") with the [go tool](https://golang.org/cmd/go/ \"go command\") from shell:\n```bash\ngo get -u github.com/lovesaroha/lnn\n```\nMake sure [Git is installed](https://git-scm.com/downloads) on your machine and in your system's `PATH`.\n\n## Tensor Usage\n\n### Create Tensor\n\n```Golang\n  // Create tensor of given shape.\n  tensor := lnn.Tensor([]int{3, 4})\n  // Print values.\n  tensor.Print()\n```\n![image](https://raw.githubusercontent.com/lovesaroha/gimages/main/53.png)\n\n### Random Tensor\n\n```Golang\n  // Create tensor of given shape and (minimum, maximum).\n  tensor := lnn.Tensor([]int{3, 4} , -1 , 1)\n  // Print values.\n  tensor.Print()\n  // Scalar tensor.\n  stensor := lnn.Tensor([]int{} , -1 , 1)\n  // Print values.\n  stensor.Print()\n```\n![image](https://raw.githubusercontent.com/lovesaroha/gimages/main/54.png)\n\n### Convert Slice Or Values Into Tensor\n\n```Golang\n    // Slice of int to tensor and print values.\n    lnn.ToTensor([]int{1, 2, 3}).Print()\n    // 2d slice of int to tensor and print values.\n    lnn.ToTensor([][]int{[]int{1, 2},[]int{3, 4}}).Print()\n    // Value to tensor and print values.\n    lnn.ToTensor(5).Print()\n```\n![image](https://raw.githubusercontent.com/lovesaroha/gimages/main/55.png)\n\n### Tensor Element Wise Operations (Add, Subtract, Multiply, Divide) \n\n```Golang\n  // Create a random tensor.\n  tensor := lnn.Tensor([]int{3, 4} , 10 , 20)\n  tensorB := lnn.Tensor([]int{3, 4} , 0 , 10)\n\n  // Add and print values.\n  tensor.Add(tensorB).Print()\n  // Subtract and print values.\n  tensor.Sub(tensorB).Print()\n  // Multiply and print values.\n  tensor.Mul(tensorB).Print()\n  // Divide and print values.\n  tensor.Sub(tensorB).Print()\n```\n![image](https://raw.githubusercontent.com/lovesaroha/gimages/main/56.png)\n\n### Tensor Element Wise Operations With Scalar Value (Add, Subtract, Multiply, Divide) \n\n```Golang\n  // Create a random tensor.\n  tensor := lnn.Tensor([]int{3, 4} , 10 , 20)\n  tensorB := lnn.ToTensor(4)\n\n  // Add and print values.\n  tensor.Add(tensorB).Print()\n  // Subtract and print values.\n  tensor.Sub(tensorB).Print()\n  // Multiply and print values.\n  tensor.Mul(tensorB).Print()\n  // Divide and print values.\n  tensor.Sub(tensorB).Print()\n```\n![image](https://raw.githubusercontent.com/lovesaroha/gimages/main/57.png)\n\n### Tensor Dot Product \n\n```Golang\n  // Create a random tensor.\n  tensor := lnn.Tensor([]int{3, 4} , 10 , 20)\n  tensorB := lnn.Tensor([]int{4, 3} , 0 , 10)\n\n  // Dot product and print values.\n  tensor.Dot(tensorB).Print()\n```\n![image](https://raw.githubusercontent.com/lovesaroha/gimages/main/58.png)\n\n### Tensor Transpose\n```Golang\n  // Create a random tensor.\n  tensor := lnn.Tensor([]int{3, 1} , 10 , 20)\n\n  // Print values.\n  tensor.Print()\n  // Transpose and print values.\n  tensor.Transpose().Print()\n```\n![image](https://raw.githubusercontent.com/lovesaroha/gimages/main/59.png)\n\n### Add All Column Values\n```Golang\n  // Create a random tensor.\n  tensor := lnn.Tensor([]int{3, 3} , 10 , 20)\n\n  // Print values.\n  tensor.Print()\n  // Add columns and print values.\n  tensor.AddCols().Print()\n```\n![image](https://raw.githubusercontent.com/lovesaroha/gimages/main/60.png)\n\n### Change Tensor Values (Map)\n```Golang\n  // Create a random tensor.\n  tensor := lnn.Tensor([]int{3, 3} , 0, 10)\n\n  // Print values.\n  tensor.Print()\n  // Square and print values.\n  tensor.Map(func (value float64) float64 {\n    return value * value\n  }).Print()\n```\n![image](https://raw.githubusercontent.com/lovesaroha/gimages/main/61.png)\n\n## Model Usage\n\n### Create Model\n```golang\n  // Create a model.\n  model := lnn.Model()\n```\n\n### Add Layers In Model\n```golang\n  // Add layer to model with 4 units , Input shape (2) and activation function relu.\n  model.AddLayer(lnn.LayerConfig{InputShape: []int{2}, Units: 4, Activation: \"relu\"})\n  // Add another layer to model with 1 unit and activation function sigmoid.\n  model.AddLayer(lnn.LayerConfig{Units: 1})\n```\n\n### Model Configuration\n```golang\n  // Makes the model with given values of loss function , optimizer and learning rate.\n  model.Make(lnn.ModelConfig{Loss: \"meanSquareError\", Optimizer: \"sgd\", LearningRate: 0.2})\n```\n\n### Train Model\n```golang\n  // Trains the model with given configuration.\n  model.Train(inputs, outputs, lnn.TrainConfig{Epochs: 1000, BatchSize: 4, Shuffle: true})\n```\n\n### Predict Output\n```golang\n  model.Predict(inputs)\n```\n## Examples\n\n### XOR Gate Model Training\n\n```golang\n  // Create a model.\n  model := lnn.Model()\n  \n  // Add layer to model with 4 units , Input shape (2) and activation function relu.\n  model.AddLayer(lnn.LayerConfig{InputShape: []int{2}, Units: 4, Activation: \"relu\"})\n  \n  // Add another layer to model with 1 unit and activation function sigmoid.\n  model.AddLayer(lnn.LayerConfig{Units: 1})\n  \n  // Makes the model with given values of loss function , optimizer and learning rate.\n  model.Make(lnn.ModelConfig{Loss: \"meanSquareError\", Optimizer: \"sgd\", LearningRate: 0.2})\n  \n  // Inputs and outputs as a tensor object.\n  inputs := lnn.ToTensor([][]float64{[]float64{1, 1, 0, 0}, []float64{1, 0, 1, 0}})\n  outputs := lnn.ToTensor([][]float64{[]float64{0, 1, 1, 0}}) \n  \n  // Trains the model with given configuration.\n  model.Train(inputs, outputs, lnn.TrainConfig{Epochs: 5000, BatchSize: 4, Shuffle: true})\n  \n  // Print values.\n  model.Predict(inputs).Print() \n```\n\n![image](https://raw.githubusercontent.com/lovesaroha/gimages/main/62.png)\n\n### Logistic Regression (OR Gate)  With Tensor\n```Golang \n  // Learning rate.\n  learningRate := lnn.ToTensor(0.2)\n  size := lnn.ToTensor(4)\n  \n  // Inputs and outputs as a tensor object.\n  inputs := lnn.ToTensor([][]float64{[]float64{1, 1, 0, 0}, []float64{1, 0, 1, 0}})\n  outputs := lnn.ToTensor([][]float64{[]float64{1, 1, 1, 0}}) \n\n  // Weights and bias.\n  weights := lnn.Tensor([]int{2, 1} , -1, 1)\n  bias := lnn.Tensor([]int{}, -1, 1)\n\n  // Train weights and bias (epochs 1000).\n  for i := 0; i \u003c 1000; i++ {\n    // Use lmath package for Sigmoid(wx + b).\n    prediction := weights.Transpose().Dot(inputs).Add(bias).Map(lmath.Sigmoid)\n    dZ := prediction.Sub(outputs)\n    weights = weights.Sub(inputs.Dot(dZ.Transpose()).Divide(size).Mul(learningRate))\n    bias = bias.Sub(dZ.AddCols().Divide(size).Mul(learningRate))\n  }\n\n  // Show prediction.\n  weights.Transpose().Dot(inputs).Add(bias).Map(lmath.Sigmoid).Print()\n\n```\n![image](https://raw.githubusercontent.com/lovesaroha/gimages/main/63.png)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flovesaroha%2Flnn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flovesaroha%2Flnn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flovesaroha%2Flnn/lists"}