Skip to main content

Convert PyTorch neural network models into LTspice subcircuits (.subckt).

Project description

pytorch2ltspice

PyPI License Python

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
  • 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 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.

🧰 Utilities

Helper utilities are available under pytorch2ltspice.utils:

  • siggen: signal generators for LTspice testbenches
  • modelgen: minimal model scaffolding for experiments
  • sampling: 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

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.2.tar.gz (19.0 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.2-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytorch2ltspice-0.1.2.tar.gz
  • Upload date:
  • Size: 19.0 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.2.tar.gz
Algorithm Hash digest
SHA256 f42fee85ec6a3837e7ecd383a760a26d7d93d7c73f9dcc269b16b9198fc392e1
MD5 e20103f5024c99ebde202ca0cfcc104b
BLAKE2b-256 b712c1e3a76f96dcf1603278c5e0d2fb5eec6d54478e271de3ee6cf7019606f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytorch2ltspice-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a5e66a7d0353d16259d5de37a06e7401fea0480069bccfbe8714cf9cb360caed
MD5 cf62b35749f31d95b3ceca3c76a54f20
BLAKE2b-256 759d8453a2a009881c595ccf980e5ea629fd9b3b0b4da851b22dfeb622880797

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