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

Voltage Mode Buck controled with Nerural Network


📄 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.3.tar.gz (21.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.3-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

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

Hashes for pytorch2ltspice-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a0e1222a88b3a867d46c762731308e62c4bdc7aac9234145d27d6702c81df1a5
MD5 cdf0f4e3968df8427b924f25eb57dcac
BLAKE2b-256 4dd8e0c4f1c4e96619ec6ffca1cfd5709ca41e1b1d4b097910a9ab980e05e72c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytorch2ltspice-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bf3083e35f95b6009f9863354f05e88f1039b5879de6f16f259af8a56f15e2ed
MD5 a95bed3c73f93c0b41c67a3017121f9e
BLAKE2b-256 6e9b2a54c5f3b31d4724872d61e8141de868707e847a57045fbf8e6d06b8b2be

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