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torch
|
| 3 |
+
|
| 4 |
+
def discriminator_loss(generator, discriminator, mol_graph, adj, annot, batch_size, device, grad_pen, lambda_gp,z_edge,z_node):
|
| 5 |
+
|
| 6 |
+
# Compute loss with real molecules.
|
| 7 |
+
|
| 8 |
+
logits_real_disc = discriminator(mol_graph)
|
| 9 |
+
|
| 10 |
+
prediction_real = - torch.mean(logits_real_disc)
|
| 11 |
+
|
| 12 |
+
# Compute loss with fake molecules.
|
| 13 |
+
|
| 14 |
+
node, edge, node_sample, edge_sample = generator(z_edge, z_node)
|
| 15 |
+
|
| 16 |
+
graph = torch.cat((node_sample.view(batch_size, -1), edge_sample.view(batch_size, -1)), dim=-1)
|
| 17 |
+
|
| 18 |
+
logits_fake_disc = discriminator(graph.detach())
|
| 19 |
+
|
| 20 |
+
prediction_fake = torch.mean(logits_fake_disc)
|
| 21 |
+
|
| 22 |
+
# Compute gradient loss.
|
| 23 |
+
|
| 24 |
+
eps = torch.rand(mol_graph.size(0),1).to(device)
|
| 25 |
+
x_int0 = (eps * mol_graph + (1. - eps) * graph).requires_grad_(True)
|
| 26 |
+
|
| 27 |
+
grad0 = discriminator(x_int0)
|
| 28 |
+
d_loss_gp = grad_pen(grad0, x_int0)
|
| 29 |
+
|
| 30 |
+
# Calculate total loss
|
| 31 |
+
|
| 32 |
+
d_loss = prediction_fake + prediction_real + d_loss_gp * lambda_gp
|
| 33 |
+
|
| 34 |
+
return node, edge,d_loss
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def generator_loss(generator, discriminator, v, adj, annot, batch_size, penalty, matrices2mol, fps_r,submodel):
|
| 38 |
+
|
| 39 |
+
# Compute loss with fake molecules.
|
| 40 |
+
|
| 41 |
+
node, edge, node_sample, edge_sample = generator(adj, annot)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
graph = torch.cat((node_sample.view(batch_size, -1), edge_sample.view(batch_size, -1)), dim=-1)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
logits_fake_disc = discriminator(graph)
|
| 48 |
+
|
| 49 |
+
prediction_fake = - torch.mean(logits_fake_disc)
|
| 50 |
+
|
| 51 |
+
# Produce molecules.
|
| 52 |
+
|
| 53 |
+
g_edges_hat_sample = torch.max(edge_sample, -1)[1]
|
| 54 |
+
g_nodes_hat_sample = torch.max(node_sample , -1)[1]
|
| 55 |
+
|
| 56 |
+
fake_mol = [matrices2mol(n_.data.cpu().numpy(), e_.data.cpu().numpy(), strict=True)
|
| 57 |
+
for e_, n_ in zip(g_edges_hat_sample, g_nodes_hat_sample)]
|
| 58 |
+
g_loss = prediction_fake
|
| 59 |
+
# Compute penalty loss.
|
| 60 |
+
if submodel == "RL":
|
| 61 |
+
reward = penalty(fake_mol, fps_r)
|
| 62 |
+
|
| 63 |
+
# Reinforcement Loss
|
| 64 |
+
|
| 65 |
+
rew_fake = v(graph)
|
| 66 |
+
|
| 67 |
+
reward_loss = torch.mean(rew_fake) ** 2 + reward
|
| 68 |
+
|
| 69 |
+
# Calculate total loss
|
| 70 |
+
|
| 71 |
+
g_loss = prediction_fake + reward_loss * 1
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
return g_loss, fake_mol, g_edges_hat_sample, g_nodes_hat_sample, node, edge
|
| 75 |
+
|
| 76 |
+
def discriminator2_loss(generator, discriminator, mol_graph, adj, annot, batch_size, device, grad_pen, lambda_gp,akt1_adj,akt1_annot):
|
| 77 |
+
|
| 78 |
+
# Generate molecules.
|
| 79 |
+
|
| 80 |
+
dr_edges, dr_nodes = generator(adj,
|
| 81 |
+
annot,
|
| 82 |
+
akt1_adj,
|
| 83 |
+
akt1_annot)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
dr_edges_hat = dr_edges.view(batch_size, -1)
|
| 87 |
+
|
| 88 |
+
dr_nodes_hat = dr_nodes.view(batch_size, -1)
|
| 89 |
+
|
| 90 |
+
dr_graph = torch.cat((dr_nodes_hat, dr_edges_hat), dim=-1)
|
| 91 |
+
|
| 92 |
+
# Compute loss with fake molecules.
|
| 93 |
+
|
| 94 |
+
dr_logits_fake = discriminator(dr_graph.detach())
|
| 95 |
+
|
| 96 |
+
d2_loss_fake = torch.mean(dr_logits_fake)
|
| 97 |
+
|
| 98 |
+
# Compute loss with real molecules.
|
| 99 |
+
|
| 100 |
+
dr_logits_real2 = discriminator(mol_graph)
|
| 101 |
+
|
| 102 |
+
d2_loss_real = - torch.mean(dr_logits_real2)
|
| 103 |
+
|
| 104 |
+
# Compute gradient loss.
|
| 105 |
+
|
| 106 |
+
eps_dr = torch.rand(mol_graph.size(0),1).to(device)
|
| 107 |
+
x_int0_dr = (eps_dr * mol_graph + (1. - eps_dr) * dr_graph).requires_grad_(True)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
grad0_dr = discriminator(x_int0_dr)
|
| 111 |
+
d2_loss_gp = grad_pen(grad0_dr, x_int0_dr)
|
| 112 |
+
|
| 113 |
+
# Compute total loss.
|
| 114 |
+
|
| 115 |
+
d2_loss = d2_loss_fake + d2_loss_real + d2_loss_gp * lambda_gp
|
| 116 |
+
|
| 117 |
+
return d2_loss
|
| 118 |
+
|
| 119 |
+
def generator2_loss(generator, discriminator, v, adj, annot, batch_size, penalty, matrices2mol, fps_r,ak1_adj,akt1_annot, submodel):
|
| 120 |
+
|
| 121 |
+
# Generate molecules.
|
| 122 |
+
|
| 123 |
+
dr_edges_g, dr_nodes_g = generator(adj,
|
| 124 |
+
annot,
|
| 125 |
+
ak1_adj,
|
| 126 |
+
akt1_annot)
|
| 127 |
+
|
| 128 |
+
dr_edges_hat_g = dr_edges_g.view(batch_size, -1)
|
| 129 |
+
|
| 130 |
+
dr_nodes_hat_g = dr_nodes_g.view(batch_size, -1)
|
| 131 |
+
|
| 132 |
+
dr_graph_g = torch.cat((dr_nodes_hat_g, dr_edges_hat_g), dim=-1)
|
| 133 |
+
|
| 134 |
+
# Compute loss with fake molecules.
|
| 135 |
+
|
| 136 |
+
dr_g_edges_hat_sample, dr_g_nodes_hat_sample = torch.max(dr_edges_g, -1)[1], torch.max(dr_nodes_g, -1)[1]
|
| 137 |
+
|
| 138 |
+
g_tra_logits_fake2 = discriminator(dr_graph_g)
|
| 139 |
+
|
| 140 |
+
g2_loss_fake = - torch.mean(g_tra_logits_fake2)
|
| 141 |
+
|
| 142 |
+
# Reward
|
| 143 |
+
fake_mol_g = [matrices2mol(n_.data.cpu().numpy(), e_.data.cpu().numpy(), strict=True)
|
| 144 |
+
for e_, n_ in zip(dr_g_edges_hat_sample, dr_g_nodes_hat_sample)]
|
| 145 |
+
g2_loss = g2_loss_fake
|
| 146 |
+
if submodel == "RL":
|
| 147 |
+
reward2 = penalty(fake_mol_g, fps_r)
|
| 148 |
+
|
| 149 |
+
# Reinforcement Loss
|
| 150 |
+
|
| 151 |
+
rew_fake2 = v(dr_graph_g)
|
| 152 |
+
reward_loss2 = torch.mean(rew_fake2) ** 2 + reward2
|
| 153 |
+
|
| 154 |
+
# Calculate total loss
|
| 155 |
+
|
| 156 |
+
g2_loss = g2_loss_fake + reward_loss2 * 10
|
| 157 |
+
|
| 158 |
+
return g2_loss, fake_mol_g, dr_g_edges_hat_sample, dr_g_nodes_hat_sample#, reward2
|