from 法泰.vision.all import *


## 催化剂代码

from migrating_catalyst import *


data = 数据加载器s(loaders['train'], loaders['valid']).cuda()


### 使用回调

@before_batch_cb
def cb(self, xb, yb): return (xb[0].view(xb[0].size(0), -1),),yb


metrics=[准确性,top_k_accuracy]
learn = 学习者(data, model, loss_func=F.cross_entropy, opt_func=Adam,
metrics=metrics, cbs=cb)


learn.fit_one_cycle(1, 0.02)


0 0.230145 0.292590 0.924700 0.995000 00:13

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.

### 改变模型

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:

model = nn.Sequential(
Flatten(),
torch.nn.Linear(28 * 28, 10))


learn = 学习者(data, model, loss_func=F.cross_entropy, opt_func=Adam, metrics=metrics)
learn.fit_one_cycle(1, 0.02)


0 0.230051 0.292577 0.924800 0.995100 00:11