fastai / fastbook

The fastai book, published as Jupyter Notebooks
Other
22.03k stars 8.49k forks source link

Cannot add in loss function when creating learner #631

Open MilesTidmarsh opened 10 months ago

MilesTidmarsh commented 10 months ago

Issue

loss function added in when creating learner

learn = vision_learner(dls, resnet18, metrics=error_rate, loss_func = nn.CrossEntropyLoss(weight=tensor(2.,1.)))

learn.summary reveals no modified loss function in use

learn.summary()

Optimizer used: <function Adam at 0x79df4959c790> Loss function: CrossEntropyLoss()

The workaround

loss function added in when creating learner

learn = vision_learner(dls, resnet18, metrics=error_rate, loss_func = nn.CrossEntropyLoss(weight=tensor(2.,1.)))

After learner is created I add in the loss function

learn.loss_func.func = nn.CrossEntropyLoss(weight=tensor(2.,1.))

summary reveals the function actually updated this time

learn.summary()

Optimizer used: <function Adam at 0x79df4959c790> Loss function: CrossEntropyLoss( (func): CrossEntropyLoss() )

forum posts that reveal the same issue

https://forums.fast.ai/t/changing-default-loss-functions/28981