A PyTorch extension for easy skip connections and repeatable blocks
Project description
torch-circuit
A PyTorch extension for building neural networks with skip connections and repeatable blocks.
Features
- Named Skip Connections: Easily implement ResNet-style skip connections with named references
- Repeatable Blocks: Define blocks once and repeat them multiple times without code duplication
- Visualization: Generate circuit diagrams of your network architecture
- PyTorch Compatible: Works seamlessly with existing PyTorch code and training loops
Installation
From Source
git clone https://github.com/your-username/torch-circuit.git
cd torch-circuit
pip install -e .
From PyPI (when published)
pip install torch-circuit
Quick Start
import torch
import torch.nn as nn
from torch_circuit import Circuit, SaveInput, GetInput, StartBlock, EndBlock
# Create a simple ResNet-style model with skip connections
model = Circuit(
nn.Conv2d(3, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
# Repeatable ResNet block
StartBlock("resnet", num_repeats=3),
SaveInput("residual"),
nn.Conv2d(64, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, 3, padding=1),
nn.BatchNorm2d(64),
GetInput("residual", op=torch.add), # Add skip connection
nn.ReLU(),
EndBlock("resnet"),
nn.AdaptiveAvgPool2d((1, 1)),
nn.Flatten(),
nn.Linear(64, 10)
)
# Use like any PyTorch model
x = torch.randn(1, 3, 32, 32)
output = model(x)
# Visualize the architecture
model.visualize(save_path="resnet_example.pdf")
Key Components
Circuit
The main container class that supports skip connections and repeatable blocks.
SaveInput / GetInput
SaveInput(name): Save the input tensor with a given nameGetInput(name, op=torch.add): Retrieve saved tensor and combine it with the current tensor using an operation (e.g. addition, concatenation)
StartBlock / EndBlock
StartBlock(name, num_repeats=N): Mark the beginning of a repeatable blockEndBlock(name): Mark the end of a repeatable block
Examples
See the examples/ directory for a complete example demonstrating equivalence to standard PyTorch implementations:
examples/resnet_mnist.py: ResNet architecture on MNIST dataset
Architecture Visualization
torch-circuit can generate simple visual diagrams of your network architecture:
model.visualize(save_path="architecture.pdf")
Advanced Usage
Custom Operations
You can use custom operations for combining skip connections:
# Element-wise multiplication instead of addition
GetInput("residual", op=torch.mul)
# Custom lambda function
GetInput("residual", op=lambda x, y: x + 0.5 * y)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torch_circuit-0.9.0.tar.gz.
File metadata
- Download URL: torch_circuit-0.9.0.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60fa255d297b6499bd6b327da0b3ef088550a25e7a3fe15ae05ac24cad8f1069
|
|
| MD5 |
fb02959144fa0e61ce5e76c62068b9cc
|
|
| BLAKE2b-256 |
8cc361e9f9e68ec44d9183cd806aa05a608d97fe32526825421e13f4f226c24d
|
File details
Details for the file torch_circuit-0.9.0-py3-none-any.whl.
File metadata
- Download URL: torch_circuit-0.9.0-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87596d32c345bfa5fca99854ac77f3dce55d9b7362238f99a7f80370f1941575
|
|
| MD5 |
4f9f03d8d8cebaa4e106f90c69148321
|
|
| BLAKE2b-256 |
7815bd8e1f0e4e6d3724d805d1291df7ac783f6716318857b49a777638b03071
|