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
  • 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.1.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.1-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytorch2ltspice-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 fe45fd7df34377e252c584dabf005ca2de728d4d736c59f6edafe8066c81e245
MD5 1ab4f2b5d7337347d9c6f322d52aaf62
BLAKE2b-256 28e989ededc16734d8a7124a9d58cbbacfe8fd4cf59727c8039402aafbfe458e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytorch2ltspice-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9c913e4cca6a98ee2344911c6648cac2327a36cb4aa4c0f8843cad7b6a525878
MD5 0a8f9eb366b45c05c9b217c337bd76af
BLAKE2b-256 a719928fea41d5337ce5027dbf7bdf65125a2b78534029e503d9d2454d501890

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