我不确定是问这个问题的合适地点,请随时告诉我是否需要删除帖子。
我是pyTorch的新手,目前正在我的项目中使用CycleGAN(pyTorch实现),并且我了解大部分cycleGAN的实现。
我阅读了名为“ CycleGAN with Better Cycles”的论文,并尝试应用本文中提到的修改。修改之一是循环一致性权重衰减,我不知道该如何应用。
optimizer_G.zero_grad()
# Identity loss
loss_id_A = criterion_identity(G_BA(real_A), real_A)
loss_id_B = criterion_identity(G_AB(real_B), real_B)
loss_identity = (loss_id_A + loss_id_B) / 2
# GAN loss
fake_B = G_AB(real_A)
loss_GAN_AB = criterion_GAN(D_B(fake_B), valid)
fake_A = G_BA(real_B)
loss_GAN_BA = criterion_GAN(D_A(fake_A), valid)
loss_GAN = (loss_GAN_AB + loss_GAN_BA) / 2
# Cycle consistency loss
recov_A = G_BA(fake_B)
loss_cycle_A = criterion_cycle(recov_A, real_A)
recov_B = G_AB(fake_A)
loss_cycle_B = criterion_cycle(recov_B, real_B)
loss_cycle = (loss_cycle_A + loss_cycle_B) / 2
# Total loss
loss_G = loss_GAN +
lambda_cyc * loss_cycle + #lambda_cyc is 10
lambda_id * loss_identity #lambda_id is 0.5 * lambda_cyc
loss_G.backward()
optimizer_G.step()
我的问题是如何逐渐减小循环一致性损失的权重?
在实现此修改方面的任何帮助将不胜感激。
这是从论文中得出的: 周期一致性损失有助于在初期阶段稳定很多训练,但在后期阶段却成为获得逼真的图像的障碍。我们建议随着训练的进展逐渐降低循环一致性损失λ的权重。但是,我们仍应确保λ为 不会衰减为0,以使生成器不会受到限制并完全消失。
谢谢。
答案 0 :(得分:2)
下面是您可以使用的原型函数!
def loss (other params, decay params, initial_lambda, steps):
# compute loss
# compute cyclic loss
# function that computes lambda given the steps
cur_lambda = compute_lambda(step, decay_params, initial_lamdba)
final_loss = loss + cur_lambda*cyclic_loss
return final_loss
compute_lambda
函数可将50从10线性衰减到1e-5
def compute_lambda(step, decay_params):
final_lambda = decay_params["final"]
initial_lambda = decay_params["initial"]
total_step = decay_params["total_step"]
start_step = decay_params["start_step"]
if (step < start_step+total_step and step>start_step):
return initial_lambda + (step-start_step)*(final_lambda-initial_lambda)/total_step
elif (step < start_step):
return initial_lambda
else:
return final_lambda
# Usage:
compute_lambda(i, {"final": 1e-5, "initial":10, "total_step":50, "start_step" : 50})