PyTorch如何在訓練過程中對多種不同損失函數的損失值進行反向傳播?

loss_a = criterion_a(output, target)
loss_b = criterion_b(output, target)
loss_c = criterion_c(output, target)

此時可有兩種作法:

loss = loss_a + loss_b + loss_c
loss.backward()
loss_a.backward()
loss_b.backward()
loss_c.backward()
loss_a.backward(retain_graph=True)
loss_b.backward(retain_graph=True) # 2022/04/24修正
loss_c.backward()

--

--

--

Machine Learning | Deep Learning | https://linktr.ee/yanwei

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yanwei Liu

Yanwei Liu

Machine Learning | Deep Learning | https://linktr.ee/yanwei

More from Medium

Understanding Autoencoders

Understanding Transfer Learning: Unlocking the power of pre-trained models

Face Mask Detection using Convolutional Neural Networks

Semi-supervised Learning — Fundamentals of Deep Learning — Idiot Developer