(self, output, target, embed, chunck_size, global_center=None, beta=0.25, Expand=10)
| 152 | super(crossentropy_pairwise, self).__init__() |
| 153 | |
| 154 | def forward(self, output, target, embed, chunck_size, global_center=None, beta=0.25, Expand=10): |
| 155 | # cross entropy loss |
| 156 | _, target_cce = target.max(1) |
| 157 | # print(target_cce.size()) |
| 158 | # sys.exit() |
| 159 | loss = cross_entropy(output, target_cce) |
| 160 | # Pairwise loss |
| 161 | if embed.size()[0] > 4: # in case of test phase when batch_size is 1 |
| 162 | Expand = np.float64(Expand * 1.0) |
| 163 | embed_split = torch.split(embed, chunck_size, dim=0) |
| 164 | target_split = torch.split(target, chunck_size, dim=0) |
| 165 | Phi = cosine_similarity(embed_split[0], embed_split[1]) * Expand |
| 166 | soft_phi = softplus(Phi) |
| 167 | if device == "cuda": |
| 168 | mask = torch.sum(torch.mul(target_split[0], target_split[1]), dim=1).type(torch.cuda.FloatTensor) |
| 169 | else: |
| 170 | mask = torch.sum(torch.mul(target_split[0], target_split[1]), dim=1).type(torch.FloatTensor) |
| 171 | mask_phi = torch.mul(mask, Phi) |
| 172 | pairwiseloss = torch.sub(soft_phi, mask_phi).mean() |
| 173 | |
| 174 | # print("cross loss: % .3f" % cross_loss) |
| 175 | # print("pairwise loss: % .3f" % pairwiseloss) |
| 176 | loss += 0.25 * pairwiseloss |
| 177 | # Possible global loss |
| 178 | if global_center is not None: |
| 179 | # print("hello=================") |
| 180 | # global_center.to(device) |
| 181 | Expand = np.float64(Expand * 1.0) |
| 182 | sample_centers = torch.mm(target.type(torch.float64), global_center) |
| 183 | phi_1 = cosine_similarity(embed, sample_centers) * Expand |
| 184 | global_sim_loss = torch.sub(softplus(phi_1), phi_1).mean() |
| 185 | return loss + beta * global_sim_loss |
| 186 | else: |
| 187 | return loss |
nothing calls this directly
no outgoing calls
no test coverage detected