Convert PyTorch neural network models into LTspice subcircuits (.subckt).
Project description
pytorch2ltspice
pytorch2ltspice converts PyTorch neural network models into LTspice-compatible subcircuits (.subckt).
By combining it with LTspicePowerSim, users can implement AI-based controllers directly in power electronics circuits such as DC-DC converters, inverters, and motor drivers.
This repository also provides example code where a neural network controls the PWM of a BUCK regulator, trained with behavior imitation and PPO.
📌 Features
- Converts PyTorch
nn.Sequentialmodels to LTspice-compatible.subcktformat - Supported layers:
- Linear:
nn.Linear
- Activations:
nn.ReLUnn.Sigmoidnn.Tanh
- Cells:
nn.RNNCellnn.GRUCellnn.LSTMCell
- Linear:
- Outputs a netlist using behavioral voltage sources (
Belements) - Recurrent cells are implemented with
.machineblocks (LO/LATCH/HI states, CLK pin auto-added) - Auto-generates LTspice node names (
NNIN1,NNIN2, ...,NNOUT1, ...) - Easy integration into LTspice testbenches
- Utilities for signal generation, model scaffolding, and sampling (
siggen,modelgen,sampling) - Several example models are included:
- MLP:
Linear → ReLU → Linear → ReLU → Linear → Sigmoid - GRUCell:
GRUCell → Linear → Tanh - LSTMCell:
LSTMCell → Linear → Tanh - RNNCell:
RNNCell → Linear → Tanh - Hybrid:
Linear → ReLU → (GRUCell/LSTMCell) → Linear → Tanh - Multi-cell LSTM: stacked
LSTMCelllayers
- MLP:
🧠 Motivation
Neural networks trained in Python (with PyTorch) can now be exported and tested directly in LTspice circuit simulations.
This allows for:
- Closed-loop simulation with NN controllers
- Verification of inference logic inside switching power supplies
- Observation of behavior under nonlinear and dynamic conditions
🚀 Installation
Option A: Install from PyPI (recommended)
pip install pytorch2ltspice
Option B: Install from source (GitHub)
git clone https://github.com/kosokno/pytorch2ltspice.git
cd pytorch2ltspice
pip install -e .
⚡ Quick Start
1. Define a model in PyTorch
import torch.nn as nn
model = nn.Sequential(
nn.Linear(20, 32),
nn.ReLU(),
nn.Linear(32, 16),
nn.ReLU(),
nn.Linear(16, 1)
)
model.eval()
2. Export as LTspice .subckt file
from pytorch2ltspice import export_model_to_ltspice
export_model_to_ltspice(
model,
filename="TEST_MODEL_SUBCKT.SP",
subckt_name="TESTACTORSUBCKT"
)
3. Include it in LTspice
- Add the following directive in LTspice:
.include TEST_MODEL_SUBCKT.SP
- Wire
NNIN*pins to your signals and readNNOUT*as the inference output.
🧰 Utilities
Helper utilities are available under pytorch2ltspice.utils:
siggen: signal generators for LTspice testbenchesmodelgen: minimal model scaffolding for experimentssampling: simple data sampling helpers
Note: example code now uses these utilities. Developer tools moved from tools/modelgen to tools/utils_test.
📂 Output Example
The resulting LTspice subcircuit will look like:
.SUBCKT TESTACTORSUBCKT NNIN1 NNIN2 ... NNIN20 NNOUT1
* LAYER 1: LINEAR
B1_1 L1_1 0 V=V(NNIN1)*(-0.179081)+V(NNIN2)*(-0.068428)+...
...
* ACTIVATION LAYER 1: RELU
B_ACT1_1 L_ACT1_1 0 V=(IF(V(L1_1)>0,V(L1_1),0))
...
B_OUT NNOUT1 0 V=V(L_ACT2_1)
.ENDS TESTACTORSUBCKT
✨ Training Example
NN Controlled Voltage Mode Buck
📄 License
MIT License
🧩 Related Projects
- 🔗 LTspicePowerSim:
A Simulink-like power electronics simulation environment built on LTspice,
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytorch2ltspice-0.1.3.tar.gz.
File metadata
- Download URL: pytorch2ltspice-0.1.3.tar.gz
- Upload date:
- Size: 21.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0e1222a88b3a867d46c762731308e62c4bdc7aac9234145d27d6702c81df1a5
|
|
| MD5 |
cdf0f4e3968df8427b924f25eb57dcac
|
|
| BLAKE2b-256 |
4dd8e0c4f1c4e96619ec6ffca1cfd5709ca41e1b1d4b097910a9ab980e05e72c
|
File details
Details for the file pytorch2ltspice-0.1.3-py3-none-any.whl.
File metadata
- Download URL: pytorch2ltspice-0.1.3-py3-none-any.whl
- Upload date:
- Size: 20.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf3083e35f95b6009f9863354f05e88f1039b5879de6f16f259af8a56f15e2ed
|
|
| MD5 |
a95bed3c73f93c0b41c67a3017121f9e
|
|
| BLAKE2b-256 |
6e9b2a54c5f3b31d4724872d61e8141de868707e847a57045fbf8e6d06b8b2be
|