# Pure PyTorch to fastai


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

``` python
from fastai.vision.all import *
```

We’re going to use the MNIST training code from the official PyTorch
examples, slightly reformatted for space, updated from AdaDelta to
AdamW, and converted from a script to a module. There’s a lot of code,
so we’ve put it into migrating_pytorch.py!

<div>

> **Note**
>
> The source script for `migrating_pytorch` is in the `examples`
> subdirectory of this folder if you checked out the `fastai` repo from
> git, or can be downloaded from
> [here](https://github.com/fastai/fastai/blob/master/nbs/examples/migrating_pytorch.py)
> if you’re using an online viewer such as Colab.

</div>

``` python
from migrating_pytorch import *
```

We can entirely replace the custom training loop with fastai’s. That
means you can get rid of `train()`, `test()`, and the epoch loop in the
original code, and replace it all with just this:

``` python
data = DataLoaders(train_loader, test_loader)
learn = Learner(data, Net(), loss_func=F.nll_loss, opt_func=Adam, metrics=accuracy)
```

Data is automatically moved to the GPU or CPU depending on what’s
available, without the need of extra Callbacks or overhead.

fastai supports many schedulers. We recommend fitting with one cycle
training:

``` python
learn.fit_one_cycle(epochs, lr)
```

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: left;">
<th data-quarto-table-cell-role="th">epoch</th>
<th data-quarto-table-cell-role="th">train_loss</th>
<th data-quarto-table-cell-role="th">valid_loss</th>
<th data-quarto-table-cell-role="th">accuracy</th>
<th data-quarto-table-cell-role="th">time</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0.130664</td>
<td>0.049394</td>
<td>0.984200</td>
<td>01:16</td>
</tr>
</tbody>
</table>

As you can see, migrating from pure PyTorch allows you to remove a lot
of code, and doesn’t require you to change any of your existing data
pipelines, optimizers, loss functions, models, etc.

Once you’ve made this change, you can then benefit from fastai’s rich
set of callbacks, transforms, visualizations, and so forth.

Note that fastai is much more than just a training loop (although we’re
only using the training loop in this example) - it is a complete
framework including GPU-accelerated transformations, end-to-end
inference, integrated applications for vision, text, tabular, and
collaborative filtering, and so forth. You can use any part of the
framework on its own, or combine them together, as described in the
[fastai paper](https://arxiv.org/abs/2002.04688).
