A PyTorch differentiable RF simulation environment
Project description
Torchradio
Torchradio is a Python library for building differentiable RF simulations.
[!NOTE] Torchradio is not affiliated with the official PyTorch project.
Installation
pip install torchradio
Example
Below is a simple example that trains two radios via backpropagation to communicate over the same noisy channel:
# Define the training environment
from torchradio import Transmitter, Receiver
from torchradio.algorithm.example import DenseRadio
from torchradio.env.null import RandomAWGNEnvironment
n_radios = 2
radio0 = DenseRadio(n_input_bits=8, tx_length_per_bit=4)
radio1 = DenseRadio(n_input_bits=8, tx_length_per_bit=4)
env = RandomAWGNEnvironment(p_min=0, p_max=1)
env.place(
transmitters={"tx0": Transmitter(radio0.tx), "tx1": Transmitter(radio1.tx)},
receivers={"rx0": Receiver(radio0.rx), "rx1": Receiver(radio1.rx)},
)
# Evaluate the initial radios
import numpy as np
def evaluate():
simulation_logs = env.simulate(n_timesteps=10000, batch_size=10)
tx_bits = {k: v.metadata["bits"] for k, v in simulation_logs.tx.items()}
rx_bits = {k: v["bits"] for k, v in simulation_logs.rx.items()}
for i in range(n_radios):
print(f'radio{i} BER: {1 - float(np.mean((tx_bits[f"tx{i}"] == rx_bits[f"rx{i}"]).numpy())):.5f}')
evaluate()
# Define the training loop
import torch
from torch import nn
loss_fn = nn.BCELoss()
optimizer = torch.optim.Adam([*radio0.parameters(), *radio1.parameters()])
def train(
n_timesteps: int,
batch_size: int,
) -> float:
optimizer.zero_grad()
device_logs = env.simulate(n_timesteps, batch_size)
tx_bits = {k: v.metadata["bits"] for k, v in device_logs.tx.items()}
rx_outputs = {k: v["bit_probabilities"] for k, v in device_logs.rx.items()}
loss = sum([
loss_fn(rx_outputs[f"rx{i}"], tx_bits[f"tx{i}"].float())
for i in range(n_radios)
])
loss.backward()
optimizer.step()
return loss
# Train the radios
for i in range(1000):
loss = train(n_timesteps=64, batch_size=10)
if i % 100 == 0:
print(f"Loss at iteration {i}: {loss:5f}")
# Evaluate the trained radios
evaluate()
See our notebooks for more in-depth examples.
Assumptions
- All events take place at baseband.
- All devices have the same centre frequency and bandwidth.
- Torchradio is not intended to replace a high-fidelity simulation. Rather, it is a training ground for developing novel radios. If a radio looks promising, its parameters can be exported for testing in a high-fidelity simulation environment.
Contributing
New contributors are always welcome! If you would like to contribute, it is recommended you set up your development environment using the following instructions.
Create a new Python virtual environment using your method of choice (e.g., venv, conda, pyenv etc.). Clone this repository and install using
pip install -e .[dev]
The above command will install Torchradio along with its core dependencies, as well as dev-specific dependencies for formatting, linting and testing. The -e
flag installs Torchradio in editable mode, so you can quickly see the effects of local source code changes without reinstalling Torchradio. You can test that everything is working as expected by running
pytest
To save failing GitHub Actions due to styling issues, set up the project's git hooks using:
pre-commit install
pre-commit run --all-files
You can view the documentation locally anytime by running:
mkdocs serve
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
File details
Details for the file torchradio-0.0.3.tar.gz
.
File metadata
- Download URL: torchradio-0.0.3.tar.gz
- Upload date:
- Size: 806.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 119341d2043096d3a0c1e22878dd314a4b673cbb62433cd5f19104fa641bdc81 |
|
MD5 | 8b3785f4daf85eee384c660ca1e106bd |
|
BLAKE2b-256 | def23201556147f68200e760ec3680187891d2d5c5c46fa44528ed1c60c4c25e |
File details
Details for the file torchradio-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: torchradio-0.0.3-py3-none-any.whl
- Upload date:
- Size: 22.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d472218629b7362a3c0e93aecd84eb5d0d839d918d0d0d012b1cd1c226833b21 |
|
MD5 | 4ae3cf978ce0755ed9f4077a3eed1da0 |
|
BLAKE2b-256 | 08a7eef89594df62bf54285fa0aa3a397367fc0f16c272aa2859a9c3abeab5da |