当前位置: 首页 > news >正文

神经生物学研究【20260008】

C++ Technical Documentation: Hodgkin-Huxley Neural Network for Low‑Pass Filtering

Project: HHMLP – Neuromorphic Low‑Pass Filter Using HH Neurons + Linear Readout
Language: C++17
Framework: LibTorch (PyTorch C++ API)
Platform: Ubuntu 24.04 / Fedora, CPU/GPU


1. Introduction

This document describes a complete C++ implementation of a neuromorphic low‑pass filter based on a recurrent network of Hodgkin‑Huxley (HH) neurons. The model learns to map a noisy square wave to its ideal low‑pass filtered version, demonstrating the applicability of biologically plausible spiking neural networks to signal processing tasks.

Key features:

  • Fully differentiable Hodgkin‑Huxley neuron model
  • Recurrent connections among 20 neurons (trainable weights)
  • Single linear readout layer with sigmoid activation
  • End‑to‑end training using LibTorch’s automatic differentiation
  • CPU and GPU support

2. System Architecture

The system consists of four main components:

ComponentDescriptionC++ Class
HH neuronImplements HH dynamics (V, m, h, n)HHNeuronImpl
Recurrent HH layerManages 20 neurons and recurrent weightsRecurrentHHLayerImpl
Full modelRecurrent layer + linear readout + sigmoidHHMLPFilterImpl
Data generationGenerates noisy square wave and filtered targetgenerate_data()

2.1 Hodgkin‑Huxley Neuron

State variables: membrane potentialV VV, sodium activationm mm, sodium inactivationh hh, potassium activationn nn. Dynamics:

C m d V d t = I ext − g Na m 3 h ( V − E Na ) − g K n 4 ( V − E K ) − g L ( V − E L ) C_m \frac{dV}{dt} = I_{\text{ext}} - g_{\text{Na}} m^3 h (V-E_{\text{Na}}) - g_{\text{K}} n^4 (V-E_{\text{K}}) - g_{\text{L}} (V-E_{\text{L}})CmdtdV=IextgNam3h(VENa)gKn4(VEK)gL(VEL)

Integration: forward Euler with (dt = 0.01) ms. All operations are performed ontorch::Tensor, allowing batching and automatic differentiation.

2.2 Recurrent HH Layer

  • Number of neurons: 20
  • Recurrent weight matrix:W rec ∈ R 20 × 20 W_{\text{rec}} \in \mathbb{R}^{20\times 20}WrecR20×20(trainable)
  • At each time step, input current for neuroni ii:
    I ext , i ( t ) = I external ( t ) + ∑ j W rec , j , i ⋅ s j ( t − 1 ) I_{\text{ext},i}(t) = I_{\text{external}}(t) + \sum_j W_{\text{rec},j,i} \cdot s_j(t-1)Iext,i(t)=Iexternal(t)+jWrec,j,isj(t1)
    wheres j ( t − 1 ) s_j(t-1)sj(t1)is the spike (0/1) from neuronj jjat previous step (threshold crossing detection).
  • Output: membrane potential sequence(batch, T, 20)

2.3 Readout Layer

  • Linear layer mapping 20‑dim membrane potential to a single scalar.
  • Sigmoid activation to ensure output in ([0,1]) (target normalized range).
  • Final output shape:(batch, T).

3. Core Implementation Details

3.1 HH Neuron (HHNeuronImpl)

classHHNeuronImpl:publictorch::nn::Module{public:HHNeuronImpl(doubledt=0.01);torch::Tensorforward(torch::Tensor I_ext,intstep=1);voidreset_state(int64_tbatch_size=1);private:doubledt,Cm,gNa,gK,gL,ENa,EK,EL,V_rest;torch::Tensor V,m,h,n;// gate rate functionstorch::Tensoralpha_m(torch::Tensor V);torch::Tensorbeta_m(torch::Tensor V);// ... similar for h, n};TORCH_MODULE(HHNeuron);

Theforwardmethod performs Euler integration forsteptime steps and updates internal state.

3.2 Recurrent HH Layer (RecurrentHHLayerImpl)

classRecurrentHHLayerImpl:publictorch::nn::Module{public:RecurrentHHLayerImpl(intn_neurons,doubledt=0.01);torch::Tensorforward(torch::Tensor input_current);private:intn_neurons;std::vector<HHNeuron>neurons;torch::Tensor W_rec;// trainable recurrent weights};TORCH_MODULE(RecurrentHHLayer);
  • input_currentshape:(batch, T, n_neurons)
  • Internal loop over time steps:
    • Compute recurrent current using previous spikes
    • Update each neuron one step
    • Detect spikes (V > 0) for next step
  • Returns membrane potentials for all time steps.

3.3 Full Model (HHMLPFilterImpl)

classHHMLPFilterImpl:publictorch::nn::Module{public:HHMLPFilterImpl(intn_neurons=20,doubledt=0.01);torch::Tensorforward(torch::Tensor x);private:RecurrentHHLayer hh_layer{nullptr};torch::nn::Linear fc{nullptr};};TORCH_MODULE(HHMLPFilter);

Forward pass:

  1. Expand input(batch, T)to(batch, T, n_neurons)(all neurons receive same external current)
  2. Pass throughhh_layer→ membrane potentials(batch, T, n_neurons)
  3. Apply linear layer and sigmoid along the neuron dimension →(batch, T)

3.4 Data Generation

Functiongenerate_datacreates synthetic training data:

  • Input: Square wave (1–10 Hz) + Gaussian noise + random impulse noise, clamped to ([-0.8, 0.8]).
  • Target: Low‑pass filtered version using a first‑order IIR filter (cutoff 5 Hz).
  • Normalization: Input scaled to ([-5, 5]) (suitable for HH neuron current sensitivity). Target remains in ([-0.8, 0.8]) (output sigmoid will be later mapped).

4. Training Pipeline

4.1 Hyperparameters

ParameterValue
Samples1500
Time steps50
Sampling frequency100 Hz
Batch size64
Epochs100
Learning rate1e-3
OptimizerAdam
LossMSE

4.2 Training Loop

for(intepoch=1;epoch<=epochs;++epoch){doubletotal_loss=0.0;for(intbatch=0;batch<n_batches;++batch){autox=inputs.slice(0,start,end);autoy=targets.slice(0,start,end);optimizer.zero_grad();autopred=model->forward(x);autoloss=torch::mse_loss(pred,y);loss.backward();optimizer.step();total_loss+=loss.item<double>()*(end-start);}if(epoch%10==0)std::cout<<"Epoch "<<epoch<<", Loss = "<<total_loss/n_samples<<std::endl;}

4.3 Model Saving

torch::save(model,"lowpass_model.pt");

Model can be reloaded for inference or further training.


5. Compilation and Execution

5.1 Prerequisites

  • LibTorch (CPU or GPU version) – Download
  • CMake ≥ 3.18
  • C++17 compiler (GCC ≥ 9, Clang ≥ 7)

5.2 CMake Configuration (CMakeLists.txt)

cmake_minimum_required(VERSION 3.18) project(LowpassTrain) set(CMAKE_CXX_STANDARD 17) set(CMAKE_PREFIX_PATH "/path/to/libtorch") # e.g., /home/user/libtorch_cpu find_package(Torch REQUIRED) add_executable(train_lowpass train_lowpass.cpp) target_link_libraries(train_lowpass ${TORCH_LIBRARIES})

5.3 Build & Run

g++-std=c++17 -I/home/x/pro/libtorch/include -I/home/x/pro/libtorch/include/torch/csrc/api/include -I/home/x/pro/BioSight/core -L/home/x/pro/libtorch/lib -Wl,-rpath,/home/x/pro/libtorch/lib train_lowpass.cpp../core/HHNeuron.cpp-ltorch-ltorch_cpu-lc10-otrain_lowpass
mkdirbuild&&cdbuild cmake..make./train_lowpass

Expected output:

Generating data... Data shape: inputs [1500, 50], targets [1500, 50] Training... Epoch 10/100, Loss = 0.0234 ... Epoch 100/100, Loss = 0.00612 Model saved to lowpass_model.pt Test sample MAE: 0.0456

6. Performance Considerations

  • Complexity: Each training epoch processes 1500 samples × 50 time steps × 20 neurons × ~10 floating‑point ops per neuron ≈ 15 million operations. On a modern CPU, 100 epochs take ~5 minutes.
  • Parallelization: LibTorch automatically uses OpenMP for tensor operations (e.g., matrix multiplication, element‑wise ops). The HH neuron loop itself is sequential; for larger neuron counts (e.g., 200), one may add#pragma omp parallel for– but for 20 neurons, overhead is negligible.
  • GPU acceleration: To use GPU, replacetorch::kCPUwithtorch::kCUDAfor tensors and link GPU‑enabled LibTorch. The same code works unchanged thanks to LibTorch’s device abstraction.

7. Extending to Fuzzy Control

The same architecture can be adapted to the fuzzy controller task (Mamdani approximation) by:

  • Modifying the data generation to produce(e, edot)inputs and normalized control output.
  • Changing the forward pass to accept two scalar inputs instead of a time series.
  • The recurrent HH layer still provides temporal dynamics (useful for smoothing).

Example adaptation: remove the time dimension, treatn_injectandn_freestages as in the Python version. The readout remains linear + sigmoid.


8. Conclusion

This C++/LibTorch implementation demonstrates that a small recurrent network of Hodgkin‑Huxley neurons with a single linear readout can learn a low‑pass filtering task end‑to‑end. The code is self‑contained, compiles with standard tools, and serves as a foundation for more complex neuromorphic signal processing and control applications.

Repository: https://gitee.com/waterruby/ANNA.git
License: Apache 2.0


Appendix: Complete Code Listing

The full source code (train_lowpass.cpp) is available in the repository undercpp-src/. It includes:

  • HHNeuronImpl,RecurrentHHLayerImpl,HHMLPFilterImplclass definitions
  • Data generation routine
  • Training loop with checkpointing (optional)
  • Model saving and evaluation

For the latest version, please refer to the online repository.

http://www.gsyq.cn/news/1510630.html

相关文章:

  • 不止前端!一文读懂Finanalyzer完整开源金融分析生态(续篇)
  • Python新手也能跑的三个爬虫:查高校排名、扒编程题、批量存网页图
  • SDU软件学院创新实训(六)
  • 延安市手表回收包包回收哪家店更好,2026甄选以下5家店铺排名前5 - 谊识预商务
  • 第7章:Retriever 检索器——从相似度搜索到精准召回
  • 2026江苏商户及市民高频选择的 5 家食品检测第三方机构实地测评整理 - 科信检测
  • VMware Workstation Pro 17免费激活终极指南:5000+密钥库的完整解决方案
  • 2026茂名市家里卫生间漏水、阳台漏水、楼顶漏水、阳台漏水、地下室渗水、阳光房漏水各种房屋漏水情况不用愁!本地防水补漏公司为您排忧解难!质保可查、售后无忧。 - 企业资讯
  • Motrix下载管理器:如何通过3个关键配置让下载速度翻倍
  • Barlow字体:54种样式如何解决现代设计的三大核心问题?
  • AI开发者管控实战:认知沙盒与意图锚点设计
  • 京东E卡绑定,京东滑块t30,京东滑块,京东验证码
  • DS4Windows:让PS5手柄在PC上重获新生的终极解决方案
  • 深入解析Kinetis K20 MCU:从Cortex-M4内核到外设选型实战指南
  • 高考残疾考生有特殊的作答方式,系统怎么处理他们的答案
  • 2026白银企业高频选择的 5 家高分子检测第三方机构实地测评整理 - 鉴安检测
  • MPC8540 PowerQUICC III:DMA、PCI与RapidIO协同设计解析
  • 2026宝鸡本地人认可的 5 家户外广告设施检测机构实地测评汇总+市民高频选择 - 中安检测集团
  • 多维聚合与数据操作:从SQL GROUP BY到空间智能计算
  • 成都市手表回收包包回收哪家店更好,2026甄选以下5家店铺排名前5 - 谊识预商务
  • NXP S32G GoldBox车载网关开发实战:从硬件解析到软件部署
  • 第8章:QueryEngine 查询引擎——把检索结果变成答案
  • 如何用3个步骤让Figma界面瞬间变中文?FigmaCN插件深度解析
  • 2026百色商户及市民高频选择的 5 家食品检测第三方机构实地测评整理 - 科信检测
  • 终极指南:如何一键备份你的QQ空间青春回忆
  • Manim数学动画引擎:5分钟学会制作专业级数学可视化视频
  • 办公被频繁弹窗打扰?教你关掉 Office 自动弹出的 AI 助手
  • Android Studio中文语言包终极指南:3步告别英文界面,提升开发效率30%
  • 富士Micrex-F系列PLC编程软件PC Programmer安装包(含中英文双语支持)
  • 革命性英雄联盟智能助手Seraphine:一站式战绩分析与BP优化解决方案