<jupyter_start><jupyter_text>Tutorial - Migrating from pure PyTorch> Incrementally adding fastai goodness to your PyTorch models 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!<jupyter_code>from migrating_pytorch import *<jupyter_output><empty_output><jupyter_text>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:<jupyter_code>data = DataLoaders(train_loader, test_loader)
learn = Learner(data, Net(), loss_func=F.nll_loss, opt_func=Adam, metrics=accuracy, cbs=CudaCallback)<jupyter_output><empty_output><jupyter_text>We also added `CudaCallback` to have the model and data moved to the GPU for us. Alternatively, you can use the fastai `DataLoader`, which provides a superset of the functionality of PyTorch's (with the same API), and can handle moving data to the GPU for us (see `migrating_ignite.ipynb` for an example of this approach). fastai supports many schedulers. We recommend fitting with 1cycle training:<jupyter_code>learn.fit_one_cycle(epochs, lr)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07116104868913857, 'fraction_numerical': 0.000749063670411985, 'mean_word_length': 6.590909090909091, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '23182', 'license': 'Apache-2.0', 'n_tokens_mistral': 405, 'n_tokens_neox': 396, 'n_words': 163, 'path': 'nbs/examples/migrating_pytorch.ipynb', 'repo_name': 'EtienneT/fastai', 'size': '1335'}