from 法泰.vision.all import *
我们将使用官方PyTorch示例中的MNIST训练代码,对空间进行了略微重新格式化,从AdaDelta更新为AdamW,并从脚本转换为模块。有很多代码,因此我们将其放入migrating_pytorch.py!
from migrating_pytorch import *
We can entirely replace the custom training loop with 法泰's. That means you can get rid of train()
, test()
, and the 时代 loop in the original code, and replace it all with just this:
data = 数据加载器s(train_loader, test_loader)
learn = 学习者(data, Net(), loss_func=F.nll_loss, opt_func=Adam, metrics=准确性, cbs=CudaCallback)
我们还添加了 CudaCallback
为我们将模型和数据移至GPU。另外,您也可以使用Fastai 数据加载器
, 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).
法泰支持许多调度程序。我们建议配合1cycle训练:
learn.fit_one_cycle(时代s, lr)
如您所见,从纯PyTorch迁移可以删除大量代码,并且不需要更改任何现有的数据管道,优化器,损失函数,模型等。
进行此更改后,您就可以受益于fastai丰富的回调,转换,可视化等等。
请注意,fastai不仅仅是一个训练循环(尽管在此示例中我们仅使用训练循环)-它是一个完整的框架,包括GPU加速的转换,端到端推理,视觉,文本,表格过滤和协作过滤等等。您可以单独使用框架的任何部分,也可以将它们组合在一起,如 法纸.