PyTorch2LTspice enables the integration of neural networks into circuit simulations by converting PyTorch models into LTspice-compatible behavioral subcircuits (.subckt
).
This tool allows deep learning inference—including supervised learning models and reinforcement learning policies—to run directly within LTspice.
By combining with the LTspicePowerSim environment, users can evaluate neural network behavior across a wide range of power electronics applications, such as DC-DC converters, inverters, and motor drivers.
- Converts PyTorch
nn.Sequential
models to LTspice-compatible.subckt
format - Supports:
nn.Linear
nn.ReLU
nn.Sigmoid
- Outputs a netlist using behavioral voltage sources (
B
elements) - Auto-generates LTspice node names (
NNIN1
,NNIN2
, ...,NNOUT1
) - Easy integration into LTspice testbenches
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
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()
from PyTorch2LTspice import export_model_to_ltspice
export_model_to_ltspice(
model,
filename="TEST_MODEL_SUBCKT.SP",
subckt_name="TESTACTORSUBCKT"
)
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
You can include it in your LTspice schematic with .INCLUDE TEST_MODEL_SUBCKT.SP
, and wire it to your simulated environment.
- Python 3.7+
- PyTorch
- NumPy
Install with:
pip install torch numpy
MIT License
- 🔗 LTspicePowerSim:
A Simulink-like power electronics simulation environment built on LTspice,