<jupyter_start><jupyter_text>Tutorial - Migrating from Catalyst> Incrementally adding fastai goodness to your Catalyst training  Catalyst code We're going to use the MNIST training code from Catalyst's README (as at August 2020), converted to a module.> Note: The source script for `migrating_catalyst` 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_catalyst.py) if you're using an online viewer such as Colab.<jupyter_code>from migrating_catalyst import *<jupyter_output><empty_output><jupyter_text>To use it in fastai, we first convert the Catalyst dict into a `DataLoaders` object:<jupyter_code>data = DataLoaders(loaders['train'], loaders['valid']).cuda()<jupyter_output><empty_output><jupyter_text>Using callbacks In the Catalyst code, a training loop is defined manually, which is where the input tensor is flattened. In fastai, there's no need to define your own training loop - you can insert your own code into any part of the training process by using a callback, which can even modify data, gradients, the loss function, or anything else in the training loop:<jupyter_code>@before_batch_cb
def cb(self, xb, yb): return (xb[0].view(xb[0].size(0), -1),),yb<jupyter_output><empty_output><jupyter_text>The Catalyst example also modifies the training loop to add metrics, but you can pass these directly to your `Learner` in fastai:<jupyter_code>metrics=[accuracy,top_k_accuracy]
learn = Learner(data, model, loss_func=F.cross_entropy, opt_func=Adam,
                metrics=metrics, cbs=cb)<jupyter_output><empty_output><jupyter_text>You can now fit your model. fastai supports many schedulers. We recommend using 1cycle:<jupyter_code>learn.fit_one_cycle(1, 0.02)<jupyter_output><empty_output><jupyter_text>As you can see, migrating from Catalyst allowed us to replace 17 lines of code (in `CustomRunner`) 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 Catalyst, 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).  Changing the model Instead of using callbacks, in this case you can also simply change the model. Here we pull the `view()` out of the training loop, and into the model, using fastai's `Flatten` layer:<jupyter_code>model = nn.Sequential(
    Flatten(),
    torch.nn.Linear(28 * 28, 10))<jupyter_output><empty_output><jupyter_text>We can now create a `Learner` and train without using any callbacks:<jupyter_code>learn = Learner(data, model, loss_func=F.cross_entropy, opt_func=Adam, metrics=metrics)
learn.fit_one_cycle(1, 0.02)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08145210494203783, 'fraction_numerical': 0.010677242220866382, 'mean_word_length': 6.006410256410256, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '96859', 'license': 'Apache-2.0', 'n_tokens_mistral': 1012, 'n_tokens_neox': 952, 'n_words': 388, 'path': 'nbs/examples/migrating_catalyst.ipynb', 'repo_name': 'shaannoor/fastai', 'size': '3278'}