# torchkeras
**Repository Path**: Python_Ai_Road/torchkeras
## Basic Information
- **Project Name**: torchkeras
- **Description**: Pytorch❤️ Keras 😋😋
- **Primary Language**: Python
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 17
- **Forks**: 6
- **Created**: 2022-01-16
- **Last Updated**: 2026-03-14
## Categories & Tags
**Categories**: machine-learning
**Tags**: None
## README
# Pytorch❤️Keras
English | [简体中文](README.md)
The torchkeras library is a simple tool for training neural network in pytorch jusk in a keras style. 😋😋
## 1, Introduction
With torchkeras, You need not to write your training loop with many lines of code, all you need to do is just
like these two steps as below:
(i) create your network and wrap it and the loss_fn together with torchkeras.KerasModel like this:
`model = torchkeras.KerasModel(net,loss_fn=nn.BCEWithLogitsLoss())`.
(ii) fit your model with the training data and validate data.
The main code of use torchkeras is like below.
```python
import torch
import torchkeras
model = torchkeras.KerasModel(net,
loss_fn = nn.BCEWithLogitsLoss(),
optimizer= torch.optim.Adam(net.parameters(),lr = 0.001),
metrics_dict = {"acc":torchmetrics.Accuracy(task='binary')}
)
dfhistory=model.fit(train_data=dl_train,
val_data=dl_val,
epochs=20,
patience=3,
ckpt_path='checkpoint',
monitor="val_acc",
mode="max",
plot=True
)
```

Besides,You can use torchkeras.VLog to get the dynamic training visualization any where as you like ~
```python
import time
import math,random
from torchkeras import VLog
epochs = 10
batchs = 30
#0, init vlog
vlog = VLog(epochs, monitor_metric='val_loss', monitor_mode='min')
#1, log_start
vlog.log_start()
for epoch in range(epochs):
#train
for step in range(batchs):
#2, log_step (for training step)
vlog.log_step({'train_loss':100-2.5*epoch+math.sin(2*step/batchs)})
time.sleep(0.05)
#eval
for step in range(20):
#3, log_step (for eval step)
vlog.log_step({'val_loss':100-2*epoch+math.sin(2*step/batchs)},training=False)
time.sleep(0.05)
#4, log_epoch
vlog.log_epoch({'val_loss':100 - 2*epoch+2*random.random()-1,
'train_loss':100-2.5*epoch+2*random.random()-1})
# 5, log_end
vlog.log_end()
```
**This project seems somehow powerful, but the source code is very simple.**
**Actually, only about 200 lines of Python code.**
**If you want to understand or modify some details of this project, feel free to read and change the source code!!!**
```python
```
## 2, Features
The main features supported by torchkeras are listed below.
Versions when these features are introduced and the libraries which they used or inspired from are given.
|features| supported from version | used or inspired by library |
|:----|:-------------------:|:--------------|
|✅ training progress bar | 3.0.0 | use tqdm,inspired by keras|
|✅ training metrics | 3.0.0 | inspired by pytorch_lightning |
|✅ notebook visualization in traning | 3.8.0 |inspired by fastai |
|✅ early stopping | 3.0.0 | inspired by keras |
|✅ gpu training | 3.0.0 |use accelerate|
|✅ multi-gpus training(ddp) | 3.6.0 | use accelerate|
|✅ fp16/bf16 training| 3.6.0 | use accelerate|
|✅ tensorboard callback | 3.7.0 |use tensorboard |
|✅ wandb callback | 3.7.0 |use wandb |
|✅ VLog | 3.9.5 | use matplotlib|
```python
```
### 3, Basic Examples
You can follow these full examples to get started with torchkeras.
|example| read notebook code | run example in kaggle|
|:----|:-------------------------|:-----------:|
|①kerasmodel basic 🔥🔥| [**torchkeras.KerasModel example**](./01,kerasmodel_example.ipynb) |