{"id":22668635,"url":"https://github.com/lanl/lca-pytorch","last_synced_at":"2025-10-26T04:13:54.352Z","repository":{"id":177356548,"uuid":"660273082","full_name":"lanl/lca-pytorch","owner":"lanl","description":"Sparse coding in PyTorch via the Locally Competitive Algorithm (LCA)","archived":false,"fork":false,"pushed_at":"2025-03-14T20:35:24.000Z","size":2385,"stargazers_count":8,"open_issues_count":1,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-19T16:25:50.863Z","etag":null,"topics":["lca","lcanets","locally-competitive-algorithm","pytorch","sparse-coding"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lanl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-06-29T16:16:49.000Z","updated_at":"2024-11-30T22:43:39.000Z","dependencies_parsed_at":"2025-04-12T11:08:50.682Z","dependency_job_id":"76b271af-4ca3-4ca3-82b4-ee5cd3fe593d","html_url":"https://github.com/lanl/lca-pytorch","commit_stats":{"total_commits":51,"total_committers":3,"mean_commits":17.0,"dds":0.4509803921568627,"last_synced_commit":"8cb85bea91d1f79a40a63747928277117a97f830"},"previous_names":["lanl/lca-pytorch"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lanl/lca-pytorch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lanl%2Flca-pytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lanl%2Flca-pytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lanl%2Flca-pytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lanl%2Flca-pytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lanl","download_url":"https://codeload.github.com/lanl/lca-pytorch/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lanl%2Flca-pytorch/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267503690,"owners_count":24098333,"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","status":"online","status_checked_at":"2025-07-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["lca","lcanets","locally-competitive-algorithm","pytorch","sparse-coding"],"created_at":"2024-12-09T15:16:04.640Z","updated_at":"2025-10-26T04:13:49.314Z","avatar_url":"https://github.com/lanl.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PyTorch Implementation of the LCA Sparse Coding Algorithm\n\n[![CI](https://github.com/lanl/lca-pytorch/actions/workflows/build.yml/badge.svg)](https://github.com/lanl/lca-pytorch/actions/workflows/build.yml)\n[![codecov](https://codecov.io/gh/lanl/lca-pytorch/branch/main/graph/badge.svg?token=XfMW3nzzj0)](https://codecov.io/gh/lanl/lca-pytorch)\n![CodeQL](https://github.com/lanl/lca-pytorch/workflows/CodeQL/badge.svg)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![License](https://img.shields.io/badge/License-BSD_3--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)\n\nLCA-PyTorch (lcapt) provides the ability to flexibly build single- or multi-layer convolutional sparse coding networks in PyTorch with the [Locally Competitive Algorithm (LCA)](https://bpb-us-e1.wpmucdn.com/blogs.rice.edu/dist/c/3448/files/2014/07/neco2008.pdf). LCA-Pytorch currently supports 1D, 2D, and 3D convolutional LCA layers, which maintain all the functionality and behavior of PyTorch convolutional layers. We currently do not support Linear (a.k.a. fully-connected) layers, but it is possible to implement the equivalent of a Linear layer with convolutions.\n\n## Installation  \n\n### Dependencies  \n\nRequired:\n* Python (\u003e= 3.8)\n\nRecommended:\n* GPU(s) with NVIDIA CUDA (\u003e= 11.0) and NVIDIA cuDNN (\u003e= v7)\n\n### Pip Installation\n\n```\npip install git+https://github.com/lanl/lca-pytorch.git\n```\n\n### Manual Installation\n\n```\ngit clone git@github.com:lanl/lca-pytorch.git\ncd lca-pytorch\npip install .\n```\n\n## Usage  \n\nLCA-PyTorch layers inherit all functionality of standard PyTorch layers.\n\n```python\nimport torch\nimport torch.nn as nn\n\nfrom lcapt.lca import LCAConv2D\n\n# create a dummy input\ninputs = torch.zeros(1, 3, 32, 32)\n\n# 2D conv layer in PyTorch\npt_conv = nn.Conv2d(\n  in_channels=3,\n  out_channels=64,\n  kernel_size=7,\n  stride=2,\n  padding=3\n)\npt_out = pt_conv(inputs)\n\n# 2D conv layer in LCA-PyTorch\nlcapt_conv = LCAConv2D(\n  out_neurons=64,\n  in_neurons=3,\n  kernel_size=7,\n  stride=2,\n  pad='same'\n)\nlcapt_out = lcapt_conv(inputs)\n```\n\n## Locally Competitive Algorithm (LCA)\n\nLCA solves the $\\ell_1$-penalized reconstruction problem\n\n\u003cdiv align=\"center\"\u003e\n\n$\\underset{a}\\min \\lvert|s - a * \\Phi \\rvert|_2^2 + \\lambda \\lvert| a \\rvert|_1$\n\n\u003cdiv align=\"left\"\u003e\n\nwhere $s$ is an input, $a$ is a sparse (i.e. mostly zeros) representation of $s$, $*$ is the convolution operation, $\\Phi$ is a dictionary of convolutional features, $a * \\Phi$ is the reconstruction of $s$, and $\\lambda$ determines the tradeoff between reconstruction fidelity and sparsity in $a$. The equation above is convex in $a$, and LCA solves it by implementing a dynamical system of leaky integrate-and-fire neurons\n\n\u003cdiv align=\"center\"\u003e\n\n$\\dot{u}(t) = \\frac{1}{\\tau} \\big[b(t) - u(t) - a(t) * G \\big]$\n\n\u003cdiv align=\"left\"\u003e\n\nin which each neuron's membrane potential, $u(t)$, is charged up or down by the bottom-up drive from the stimulus, $b(t) = s(t) * \\Phi$ and is leaky via the term $-u(t)$. $u(t)$ can also be inhibited or excited by active surrounding neurons via the term $-a(t) * G$, where $a(t)=\\Gamma_\\lambda (u(t))$ is the neuron's activation computed by applying a firing threshold $\\lambda$ to $u(t)$, and $G=\\Phi * \\Phi - I$. This means that a given neuron will modulate a neighboring neuron in proportion to the similarity between their receptive fields and how active it is at that time.\n\nBelow is a mapping between the variable names used in this implementation and those used in [Rozell et al.'s formulation](https://bpb-us-e1.wpmucdn.com/blogs.rice.edu/dist/c/3448/files/2014/07/neco2008.pdf) of LCA.\n\n\u003cdiv align=\"center\"\u003e\n\n| **LCA-PyTorch Variable** | **Rozell Variable** | **Description** |\n| --- | --- | --- |\n| input_drive | $b(t)$ | Drive from the inputs/stimulus |\n| states | $u(t)$ | Internal state/membrane potential |\n| acts | $a(t)$ | Code/Representation/External Communication |\n| lambda_ | $\\lambda$ | Transfer function threshold value |\n| weights | $\\Phi$ | Dictionary/Features |\n| inputs | $s(t)$ | Input data |\n| recons | $\\hat{s}(t)$ | Reconstruction of the input |\n| tau | $\\tau$ | LCA time constant |\n\n\u003c/div\u003e\n\n## Examples\n\n  * Dictionary Learning Using Built-In Update Method\n    * [Dictionary Learning on Cifar-10 Images](https://github.com/lanl/lca-pytorch/blob/main/examples/builtin_dictionary_learning_cifar.ipynb)\n    * [Fully-Connected Dictionary Learning on MNIST](https://github.com/lanl/lca-pytorch/blob/main/examples/builtin_dictionary_learning_mnist_fc.ipynb)\n  \n  * Dictionary Learning Using PyTorch Optimizer  \n    * [Dictionary Learning on Cifar-10 Images](https://github.com/lanl/lca-pytorch/blob/main/examples/pytorch_optim_dictionary_learning_cifar.ipynb)\n\n## License\nLCA-PyTorch is provided under a BSD license with a \"modifications must be indicated\" clause.  See [the LICENSE file](https://github.com/lanl/lca-pytorch/blob/main/LICENSE) for the full text. Internally, the LCA-PyTorch package is known as LA-CC-23-064.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flanl%2Flca-pytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flanl%2Flca-pytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flanl%2Flca-pytorch/lists"}