# Ignite with fastai


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

We’re going to use the MNIST training code from Ignite’s examples
directory (as at August 2020), converted to a module.

<div>

> **Note**
>
> The source script for `migrating_ignite` 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_ignite.py)
> if you’re using an online viewer such as Colab.

</div>

``` python
from migrating_ignite import *

from fastai.vision.all import *
```

To use it in fastai, we first pull the DataLoaders from the module into
a [`DataLoaders`](https://docs.fast.ai/data.core.html#dataloaders)
object:

``` python
data = DataLoaders(*get_data_loaders(64, 128)).cuda()
```

We can now create a
[`Learner`](https://docs.fast.ai/learner.html#learner) and fit:

``` python
opt_func = partial(SGD, momentum=0.5)
learn = Learner(data, Net(), loss_func=nn.NLLLoss(), opt_func=opt_func, metrics=accuracy)
learn.fit_one_cycle(1, 0.01)
```

<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.999266</td>
<td>0.597913</td>
<td>0.856200</td>
<td>00:22</td>
</tr>
</tbody>
</table>

As you can see, migrating from Ignite allowed us to replace 52 lines of
code (in `run()`) with just 3 lines, 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 very different from Ignite, in that it 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).
