Modified from PyTorch MNIST official example. Recreated with TensorFlow under eager execution mode. With detailed comments & interactive interface.
pytorch/
train.py # Train the model
model.py # The defined model
app.py # Interactive predictor
model # Pretrained model, will be overriden when you start training
test_n.png # Sample images for the use of interactive predictor
tensorflow_eager/
train.py # Train the model
model.py # The defined model
app.py # Interactive predictor
checkpoint, ckpt-* # Pretrained model, the number after prefix is the final training step
test_n.png # Sample images for the use of interactive predictor
# clone project
$ git clone https://github.com/pyliaorachel/pytorch-mnist-interactive.git
$ cd MNIST-pytorch-tensorflow-eager-interactive
# install dependencies
$ pip3 install -r requirements.txt
# train & test model
$ python3 -m pytorch.train
# ...data will be fetched to ../data/
# ...trained model will be saved to ./pytorch/model
# or
$ python3 -m tensorflow_eager.train
# ...data will be fetched to somewhere
# ...trained model will be saved to ./tensorflow_eager/checkpoint & ./tensorflow_eager/ckpt-*
# test model interactively
$ python3 -m pytorch.app --image=<path-to-image>
# or
$ python3 -m tensorflow_eager.app --image=<path-to-image>
OS | CPU | Memory |
---|---|---|
MacOS 10.12.4 | 2 GHz Intel Core i5 | 16 GB 1867 MHz LPDDR3 |
TensorFlow Eager | PyTorch | |
---|---|---|
Time | real 6m4.446s user 13m42.909s sys 1m54.327s |
real 3m59.340s user 3m28.285s sys 0m57.395s |
Avg. Loss (Test) | 0.0610 | 0.0473 |
Accuracy | 9856/10000 (99%) | 9845/10000 (98%) |
Avg. Loss and Accuracy are expected to be more or less the same. PyTorch is half the time of TensorFlow's on CPU, while the code complexity is the same.