| 72 | |
| 73 | |
| 74 | class pairwiseloss(torch.nn.Module): |
| 75 | def __init__(self): |
| 76 | super(pairwiseloss, self).__init__() |
| 77 | |
| 78 | def forward(self, embed, target, chunck_size, Expand=10): |
| 79 | Expand = np.float64(Expand * 1.0) |
| 80 | embed_split = torch.split(embed, chunck_size, dim=0) |
| 81 | target_split = torch.split(target, chunck_size, dim=0) |
| 82 | Phi = cosine_similarity(embed_split[0], embed_split[1]) * Expand |
| 83 | soft_phi = softplus(Phi) |
| 84 | if device == "cuda": |
| 85 | mask = torch.sum(torch.mul(target_split[0], target_split[1]), dim=1).type(torch.cuda.FloatTensor) |
| 86 | else: |
| 87 | mask = torch.sum(torch.mul(target_split[0], target_split[1]), dim=1).type(torch.FloatTensor) |
| 88 | # print(mask.type()) |
| 89 | # print(Phi.type()) |
| 90 | mask_phi = torch.mul(mask, Phi) |
| 91 | pairwiseloss = torch.sub(soft_phi, mask_phi).mean() |
| 92 | return pairwiseloss |
| 93 | |
| 94 | |
| 95 | class pairwiseloss_global(torch.nn.Module): |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…