pytorch2ltspice
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.
Features
- Export PyTorch
nn.Sequentialmodels as LTspice.subcktnetlists. - Support
nn.Linear,nn.ReLU,nn.Sigmoid,nn.Tanh,nn.RNNCell,nn.GRUCell, andnn.LSTMCell. - Generate behavioral-source (
B) based netlists for feed-forward layers. - Generate recurrent-cell implementations using LTspice
.machineblocks with an auto-addedCLKpin. - Support final-output postprocessing via
output_activationand selective output exposure viaoutput_mask. - Include helper utilities under
pytorch2ltspice.utilsfor 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.SPto 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 fromnn.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 usingGRUCell.
Related Projects
License
MIT
Metadata
Release files for pytorch2ltspice 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytorch2ltspice-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for pytorch2ltspice-0.1.4-py3-none-any.whl |
Release files / pytorch2ltspice-0.1.4-py3-none-any.whl
| Download URL | pytorch2ltspice-0.1.4-py3-none-any.whl |
|---|---|
| Size | 20.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
79d35e7f6c87809fa28f6b4f15798b1de77faf139acc7772ac4b3fcf2b74e9bb
|
|
BLAKE2b-256 checksum How to use checksums |
5ec3915d554c5e16466f8e211f5b53b326f2b4b83703b4c936f50c2eb17ad9e7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|
Security for pytorch2ltspice 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.