示例#1
0
            h_1, H) + (criterion_identity(h_2, H) * 0.5)
        identity_loss = (Gl2m_id_loss + Gm2h_id_loss * 0.67) * 0.5

        #total_loss
        loss_G = identity_loss * 0.5 + G_loss + cycle_loss * 10
        loss_G.backward()
        optimizer_G.step()
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        optimizer_Dm.zero_grad()
        #real
        Dm_loss = criterion_GAN(Dm(M), target_real)
        #fake
        m_1 = ReplayBuffer().push_and_pop(m_1)
        m_2 = ReplayBuffer().push_and_pop(m_2)
        m_3 = ReplayBuffer().push_and_pop(m_3)
        Dm_f_loss = criterion_GAN(Dm(m_1.detach()), target_fake)
        Dm_f_loss += criterion_GAN(Dm(m_2.detach()), target_fake) * 0.5
        Dm_f_loss += criterion_GAN(Dm(m_3.detach()), target_fake) * 0.25
        # Total loss
        loss_Dm = (Dm_f_loss + Dm_loss) * 0.5

        loss_Dm.backward()
        optimizer_Dm.step()
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        optimizer_Dh.zero_grad()
        #real
        Dh_loss = criterion_GAN(Dh(H), target_real)
        #fake
        h_1 = ReplayBuffer().push_and_pop(h_1)
        h_2 = ReplayBuffer().push_and_pop(h_2)
示例#2
0
        G_loss = (Gc2d_loss + Gd2c_loss) * 0.5
        #cycle_loss
        cdc_cycle_loss = criterion_cycle(cr, c)
        dcd_cycle_loss = criterion_cycle(dr, d)
        cycle_loss = (dcd_cycle_loss + cdc_cycle_loss) * 0.5
        #total_loss
        loss_G = cycle_loss * 10 + G_loss + identity_loss * 0.5
        loss_G.backward()
        optimizer_G.step()
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        optimizer_Dc.zero_grad()
        #real
        Dc_c_loss = criterion_GAN(Dc(c), target_real)
        #fake
        cf = ReplayBuffer().push_and_pop(cf)
        Dc_cf = Dc(cf.detach())
        Dc_cf_loss = criterion_GAN(Dc_cf, target_fake)
        # Total loss
        loss_Dc = (Dc_c_loss + Dc_cf_loss) * 0.5
        loss_Dc.backward()

        optimizer_Dc.step()
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        optimizer_Dd.zero_grad()
        #real
        Dd_d_loss = criterion_GAN(Dd(d), target_real)
        #fake
        df = ReplayBuffer().push_and_pop(df)
        Dd_df = Dd(df.detach())
        Dd_df_loss = criterion_GAN(Dd_df, target_fake)
        # Total loss