Training Loop made easy

mobadara: <p><img src="https://discuss.pytorch.org/images/emoji/apple/fire.png?v=14" title=":fire:" class="emoji" alt=":fire:" loading="lazy" width="20" height="20"> Introducing Torchflow — A Lightweight Library to Streamline PyTorch Training, Tuning, and Callbacks</p>
<p>Hello PyTorch Community <img src="https://discuss.pytorch.org/images/emoji/apple/waving_hand.png?v=14" title=":waving_hand:" class="emoji" alt=":waving_hand:" loading="lazy" width="20" height="20">,</p>
<p>I’m excited to share my first open-source project, <strong>Torchflow</strong>, now available on <strong>PyPI</strong>!<br>
After months of learning, iterating, and debugging (and many nights without stable power <img src="https://discuss.pytorch.org/images/emoji/apple/high_voltage.png?v=14" title=":high_voltage:" class="emoji" alt=":high_voltage:" loading="lazy" width="20" height="20">), I’ve built this library to make <strong>training and experimentation in PyTorch</strong> a lot easier — especially for learners, researchers, and data scientists working on rapid prototyping.</p>
<hr>
<h3><a name="p-476011-what-is-torchflow-1" class="anchor" href="#p-476011-what-is-torchflow-1"></a><img src="https://discuss.pytorch.org/images/emoji/apple/light_bulb.png?v=14" title=":light_bulb:" class="emoji" alt=":light_bulb:" loading="lazy" width="20" height="20"> What is Torchflow?</h3>
<p><strong>Torchflow</strong> is a modular library designed to help you:</p>
<ul>
<li>
<p>Simplify <strong>training loops</strong> in PyTorch</p>
</li>
<li>
<p>Implement and customize <strong>callbacks</strong> for logging, early stopping, and checkpoints</p>
</li>
<li>
<p>Integrate <strong>hyperparameter tuning</strong> with minimal setup</p>
</li>
<li>
<p>Maintain a clean, readable, and reproducible experiment structure</p>
</li>
</ul>
<p>It’s meant to reduce boilerplate code while keeping the full flexibility of native PyTorch.</p>
<hr>
<h3><a name="p-476011-quick-install-2" class="anchor" href="#p-476011-quick-install-2"></a><img src="https://discuss.pytorch.org/images/emoji/apple/rocket.png?v=14" title=":rocket:" class="emoji" alt=":rocket:" loading="lazy" width="20" height="20"> Quick Install</h3>
<pre data-code-wrap="bash"><code class="lang-bash">pip install torchflow-core

</code></pre>
<hr>
<h3><a name="p-476011-learn-more-3" class="anchor" href="#p-476011-learn-more-3"></a><img src="https://discuss.pytorch.org/images/emoji/apple/books.png?v=14" title=":books:" class="emoji" alt=":books:" loading="lazy" width="20" height="20"> Learn More</h3>
<ul>
<li>
<p><strong>GitHub Repo:</strong> <a href="https://github.com/mobadara/torchflow" rel="noopener nofollow ugc">https://github.com/mobadara/torchflow</a></p>
</li>
<li>
<p><strong>PyPI:</strong> <a href="https://pypi.org/project/torchflow-core" rel="noopener nofollow ugc">https://pypi.org/project/torchflow-core</a></p>
</li>
</ul>
<p>This snippet shows how a user can define a model, use Torchflow’s training utilities, and integrate callbacks — all in just a few lines.</p>
<hr>
<pre data-code-wrap="python"><code class="lang-python">import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset

# Import Torchflow Trainer
from torchflow import Trainer, EarlyStopping, ModelCheckpoint

# 1️⃣ Simple dataset
X = torch.randn(500, 10)
y = torch.randint(0, 2, (500,))
dataset = TensorDataset(X, y)
loader = DataLoader(dataset, batch_size=32, shuffle=True)

# 2️⃣ Simple model
class SimpleNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc = nn.Sequential(
            nn.Linear(10, 16),
            nn.ReLU(),
            nn.Linear(16, 2)
        )

    def forward(self, x):
        return self.fc(x)

model = SimpleNet()

# 3️⃣ Define training setup
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=1e-3)

# 4️⃣ Initialize Trainer with callbacks
trainer = Trainer(
    model=model,
    criterion=criterion,
    optimizer=optimizer,
    callbacks=[
        EarlyStopping(patience=3),
        ModelCheckpoint(save_best=True)
    ]
)

# 5️⃣ Train and evaluate
trainer.fit(loader, epochs=10)
trainer.evaluate(loader)

</code></pre>
<h3><a name="p-476011-get-involved-4" class="anchor" href="#p-476011-get-involved-4"></a><img src="https://discuss.pytorch.org/images/emoji/apple/handshake.png?v=14" title=":handshake:" class="emoji" alt=":handshake:" loading="lazy" width="20" height="20"> Get Involved</h3>
<p>I’d love your feedback, ideas, or contributions!<br>
Check out the <strong><code>CONTRIBUTION.md</code></strong> file in the repo to see how you can help improve Torchflow.</p>
<hr>
<p>This project grew out of my experience as a <strong>Tech4Dev Developer Foundry Fellow (AI/ML Track)</strong> — a program that helped me turn knowledge into impactful projects. I hope it helps others in the same way.</p>
<p>Thanks to the PyTorch community for being such an inspiring and supportive space. <img src="https://discuss.pytorch.org/images/emoji/apple/folded_hands.png?v=14" title=":folded_hands:" class="emoji" alt=":folded_hands:" loading="lazy" width="20" height="20"></p>