Skip to main content

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

Project description

pytorch2ltspice

PyPI License Python

pytorch2ltspice converts PyTorch nn.Sequential models into LTspice-compatible subcircuits (.subckt). It is intended for workflows where a controller is trained in PyTorch and then embedded directly into LTspice simulations for power electronics, control, and reinforcement learning experiments.

By combining it with LTspicePowerSim, you can evaluate neural-network controllers inside converter, inverter, and motor-drive simulations.

overview

Features

  • Export PyTorch nn.Sequential models as LTspice .subckt netlists.
  • Support nn.Linear, nn.ReLU, nn.Sigmoid, nn.Tanh, nn.RNNCell, nn.GRUCell, and nn.LSTMCell.
  • Generate behavioral-source (B) based netlists for feed-forward layers.
  • Generate recurrent-cell implementations using LTspice .machine blocks with an auto-added CLK pin.
  • Support final-output postprocessing via output_activation and selective output exposure via output_mask.
  • Include helper utilities under pytorch2ltspice.utils for signal generation, model scaffolding, and sampling.
  • Provide end-to-end example projects for buck-converter control with imitation learning and PPO.

Installation

Install from PyPI:

pip install pytorch2ltspice

Install from source:

git clone https://github.com/kosokno/pytorch2ltspice.git
cd pytorch2ltspice
pip install -e .

Quick Start

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()

Export it as an LTspice subcircuit:

from pytorch2ltspice import export_model_to_ltspice

export_model_to_ltspice(
    model,
    filename="TEST_MODEL_SUBCKT.SP",
    subckt_name="TESTACTORSUBCKT",
    output_activation=["tanh"],
)

Include the generated file in LTspice:

  • Add .include TEST_MODEL_SUBCKT.SP to your schematic.
  • Drive the NNIN* pins from your circuit.
  • Read the inference result from NNOUT*.

Utilities

Helper utilities are available under pytorch2ltspice.utils:

  • siggen: generate LTspice signal-source schematics and symbols.
  • modelgen: generate a PyTorch module class from nn.Sequential.
  • sampling: sample signals on a clock for analysis and data preparation.

Examples

Example projects are available under examples/:

  • BUCK_VM_BI: behavior-imitation controller for a voltage-mode buck converter.
  • BUCK_VM_PPO: PPO-based controller for a voltage-mode buck converter.
  • BUCK_VM_GRU8x1: recurrent controller examples using GRUCell.

Related Projects

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

pytorch2ltspice-0.1.4-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for pytorch2ltspice-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 79d35e7f6c87809fa28f6b4f15798b1de77faf139acc7772ac4b3fcf2b74e9bb
MD5 41a719bedbf44b454ab5d883766fbc1a
BLAKE2b-256 5ec3915d554c5e16466f8e211f5b53b326f2b4b83703b4c936f50c2eb17ad9e7

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