Skip to main content

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.

Overview


📌 Features

  • Converts PyTorch nn.Sequential models to LTspice-compatible .subckt format
  • Supported layers:
    • Linear:
      • nn.Linear
    • Activations:
      • nn.ReLU
      • nn.Sigmoid
      • nn.Tanh
    • Cells:
      • nn.RNNCell
      • nn.GRUCell
      • nn.LSTMCell
  • Outputs a netlist using behavioral voltage sources (B elements)
  • Recurrent cells are implemented with .machine blocks (LO/LATCH/HI states, CLK pin auto-added)
  • Auto-generates LTspice node names (NNIN1, NNIN2, ..., NNOUT1, ...)
  • Easy integration into LTspice testbenches
  • 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 LSTMCell layers

🧠 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 read NNOUT* as the inference output.

📂 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

Overview


📄 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

pytorch2ltspice-0.1.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytorch2ltspice-0.1.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file pytorch2ltspice-0.1.0.tar.gz.

File metadata

  • Download URL: pytorch2ltspice-0.1.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.4

File hashes

Hashes for pytorch2ltspice-0.1.0.tar.gz
Algorithm Hash digest
SHA256 92ce4c15ea7397d556c31f06e0b3d0777ccf6d1573e2c4de4e6770d02f5d5694
MD5 1990ec5555d21eaa73e7618af6278092
BLAKE2b-256 05fa3b7ef79c2930b3e511c153756210087cfd22f66a493fd18bcf5d086749f7

See more details on using hashes here.

File details

Details for the file pytorch2ltspice-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytorch2ltspice-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a4c4f912eaef6497e656a31086179049ce523c26e3a6bcd46f586a0a83157d1
MD5 313938e05346143dbb7ffb4c9aaceaca
BLAKE2b-256 53b58e26fffabacc68a20e36309973ea2402ad573dab0e0553620764511f57d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page