from itertools import cyclefor i, data in enumerate(zip(cycle(dataloaders1), dataloaders2)):
img_0, label_0 = data[0]
img_1, label_1 = data[1]
.
.
.
.
.
.
# 假設dataloaders1只有100張圖片;而dataloaders2可能有1000張圖片。
# 透過cycle(dataloaders1)的方式可以讓dataloaders1中的這100張圖片不斷地被循環抽出,直到dataloaders2的圖片被完整使用過一次,才會完成一整個epoch的訓練。
# 特別注意的是,因為batch size設定的原因,在訓練的最後一個mini-batch中,有可能會出現dataloaders1的數量(100張)比dataloaders2的圖片數量多的情形(假設dataloaders2只剩下90張)。在計算loss的時候,可能會出現shape不同的錯誤,因此要給dataloaders2設定drop_last=True的參數,才會避免這種情況發生。
for i, data in enumerate(zip(dataloaders1, dataloaders2)):
img_0, label_0 = data[0]
img_1, label_1 = data[1]
.
.
.
.
.
.
# 訓練過程中,將會受限於dataloaders1的100張圖片,dataloaders2的1000張圖片將不會被完整使用,每次利用完dataloaders1的圖片後,就會切換到下一個epoch

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()
loss_c.backward()

class GradReverse(torch.autograd.Function):
def __init__(self, lambd):
self.lambd = lambd
def forward(self, x):
return x.view_as(x)
def backward(self, grad_output):
return (grad_output * -self.lambd)
def grad_reverse(x, lambd=1.0):
return GradReverse(lambd)(x)
class GradReverse(torch.autograd.Function):
def __init__(self):
super(GradReverse, self).__init__()
@ staticmethod
def forward(ctx, x, lambda_):
ctx.save_for_backward(lambda_)
return x.view_as(x)
@ staticmethod
def backward(ctx, grad_output):
lambda_, = ctx.saved_variables
grad_input = grad_output.clone()
return - lambda_ * grad_input, None
def grad_reverse(x, lambd=1.0):
lam = torch.tensor(lambd)
return GradReverse.apply(x,lam)

model_conv = torchvision.models.resnet18(pretrained= …

import tensorboardX as tbx     # 本範例會使用到tensorboardX套件
writer = tbx.SummaryWriter( …

Yanwei Liu

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

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