(self, x, x_aug, batch_size)
| 119 | |
| 120 | |
| 121 | def get_loss(self, x, x_aug, batch_size): |
| 122 | |
| 123 | T = 0.2 |
| 124 | n_samples, _ = x.size() |
| 125 | x_abs = x.norm(dim=1) |
| 126 | x_aug_abs = x_aug.norm(dim=1) |
| 127 | if n_samples <= batch_size: |
| 128 | batch_size = n_samples |
| 129 | sim_matrix = torch.einsum('ik,jk->ij', x, x_aug) / torch.einsum('i,j->ij', x_abs, x_aug_abs) |
| 130 | sim_matrix = torch.exp(sim_matrix / T) |
| 131 | pos_sim = sim_matrix[range(batch_size), range(batch_size)] |
| 132 | loss = pos_sim / (sim_matrix.sum(dim=1) - pos_sim) |
| 133 | loss = - torch.log(loss).mean() |
| 134 | else: |
| 135 | n_loop = n_samples // batch_size + 1 |
| 136 | losses = [] |
| 137 | for i in range(n_loop): |
| 138 | start = i*batch_size |
| 139 | end = (i + 1)*batch_size if i != n_loop - 1 else n_samples |
| 140 | n_sim = batch_size if i != n_loop - 1 else end - start |
| 141 | sim_matrix = torch.einsum('ik,jk->ij', x[start:end], x_aug) / torch.einsum('i,j->ij', x_abs[start:end], x_aug_abs) |
| 142 | sim_matrix = torch.exp(sim_matrix / T) |
| 143 | pos_sim = sim_matrix[range(n_sim), range(n_sim)] |
| 144 | loss = pos_sim / (sim_matrix.sum(dim=1) - pos_sim) |
| 145 | losses.append(-torch.log(loss)) |
| 146 | |
| 147 | loss = torch.concat(losses).mean() |
| 148 | |
| 149 | return loss |
| 150 | |
| 151 | |
| 152 | class GraphInfoMax(nn.Module): |
nothing calls this directly
no outgoing calls
no test coverage detected