基于PyTorch的神经形态建模框架
项目描述
康奈尔大学计算生理学实验室的项目。
请查看在线文档以获取完整文档。
基于PyTorch的神经形态建模框架。
Sapicore是一个框架,它提供了高级抽象,帮助使用pytorch编写神经形态模型。Sapicore本身不包含任何具体的模型,而是每个模型可能都有自己的仓库,该仓库实现了模型使用的Sapicore组件。
遵循此方法将允许Sapicore被多个模型独立使用,而不会因为不同的实现细节或要求而相互污染。
一个实验室共同使用的Sapicore模型可以放在框架之外的一个包中,供希望使用这些通用模型的其他项目重用。
Sapicore 支持使用 tree-config 包进行用户配置模型。同样,Sapicore 支持为属性和缓冲区添加注释,以便将日志记录到例如 tensorboardx 以进行实时显示,或使用基于 nixio HDF5 的文件进行后续分析和调试。
安装
Sapicore 的要求很少。它需要
Python 3.7+
Pytorch 1.5+(请参阅 PyTorch 安装)。
科学库(请参阅 setup.py 中的列表)。
Tensorboard 和 tensorboardx(可选)。
最简单的方法是使用 conda 安装它们,如下所示
conda install -c conda-forge numpy tqdm pandas ruamel.yaml tensorboard tensorboardx
或者使用 pip,简单地(pip 会自动安装其余的依赖项)
python -m pip install tensorboard tensorboardx
一旦安装了依赖项,就可以在当前的 conda/pip 环境中安装 Sapicore
用户安装
您可以使用以下方式安装最新稳定的 Sapicore
pip install sapicore
要从 github 安装最新版本的 Sapicore,请执行
pip install https://github.com/cplab/sapicore/archive/refs/heads/main.zip
开发安装
要为开发安装 Sapicore 并编辑 Sapicore 本身
从 github 克隆 sapicore
git clone https://github.com/cplab/sapicore.git
进入 sapicore 目录
cd sapicore
以可编辑安装方式安装它
pip install -e .
示例模型
以下是一个简短的、可运行的示例。一个类似但更完整的示例,包括配置和日志记录,可以在 sapicore/examples 下找到。
import torch
from sapicore.model import SapicoreModel
from sapicore.neuron.analog import AnalogNeuron
from sapicore.synapse import SapicoreSynapse
from sapicore.pipeline import PipelineBase
from sapicore.learning import SapicoreLearning
class SimpleNeuron(AnalogNeuron):
"""Represents a neuron or matrix of neurons."""
activation: torch.Tensor
def forward(self, data: torch.tensor) -> torch.tensor:
self.activation = torch.clip(data, -2, 2)
return self.activation
class SimpleSynapse(SapicoreSynapse):
"""Represents a synapse connecting a neuron or matrix of neurons."""
weight: torch.Tensor
def __init__(self, **kwargs):
super().__init__(**kwargs)
# register weight as part of the state so it is saved with the model
self.register_buffer("weight", torch.zeros(0))
self.weight = torch.normal(0, 1, size=(5, ))
def forward(self, data: torch.tensor) -> torch.tensor:
return data * self.weight
class SimpleLearning(SapicoreLearning):
"""Learns the synapse weight based on pre-post activation."""
def apply_learning(
self, pre_neuron: SimpleNeuron, synapse: SimpleSynapse,
post_neuron: SimpleNeuron, **kwargs):
synapse.weight *= torch.abs(pre_neuron.activation) * \
torch.abs(post_neuron.activation)
class MyModel(SapicoreModel):
"""Network model that contains neurons/synapses."""
neuron_1: SimpleNeuron
synapse: SimpleSynapse
neuron_2: SimpleNeuron
learning: SimpleLearning
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.neuron_1 = SimpleNeuron()
self.synapse = SimpleSynapse()
self.neuron_2 = SimpleNeuron()
self.add_neuron(None, 'entry_neuron', self.neuron_1)
self.add_synapse(self.neuron_1, 'synapse', self.synapse)
self.add_neuron(self.synapse, 'exit_neuron', self.neuron_2)
self.learning = SimpleLearning()
self.add_learning_rule('learning', self.learning)
def initialize_learning_state(self) -> None:
self.learning.initialize_state()
def forward(self, data: torch.tensor) -> torch.tensor:
data = self.neuron_1(data)
data = self.synapse(data)
data = self.neuron_2(data)
return data
def apply_learning(self, **kwargs) -> None:
self.learning.apply_learning(self.neuron_1, self.synapse, self.neuron_2)
class SimplePipeline(PipelineBase):
"""Training pipeline."""
def run(self) -> None:
use_cuda = torch.cuda.is_available()
cuda_device = torch.device("cuda:0" if use_cuda else "cpu")
model = MyModel()
model.initialize_state()
model.initialize_learning_state()
model.to(cuda_device)
print('Pre-learning weight: ', model.synapse.weight.cpu().numpy())
# these models don't use gradients
with torch.no_grad():
for i in range(3):
# fake data
data = torch.normal(0, 1, size=(5, ))
# pass it through the model
model.forward(data)
# apply model learning
model.apply_learning()
print('Post-learning weight: ', model.synapse.weight.cpu().numpy())
if __name__ == '__main__':
# create and run the model
pipeline = SimplePipeline()
pipeline.run()
当运行时,此程序会打印
Pre-learning weight: [-0.95982265 -0.2735969 0.6473335 -0.37592512 0.05847792] Post-learning weight: [-6.0495706e-09 -8.3768668e-08 3.3906079e-05 -3.3586942e-09 1.3144294e-32]
项目详情
下载文件
下载适合您平台的项目文件。如果您不确定要选择哪个,请了解有关 安装包 的更多信息。