(self, x, x_mask, refer, codes, infer=False)
| 781 | self.dims = dims |
| 782 | |
| 783 | def forward(self, x, x_mask, refer, codes, infer=False): |
| 784 | x = x.detach() |
| 785 | x = self.vq_proj(x * x_mask) * x_mask |
| 786 | g = self.ref_enc(refer, x_mask) |
| 787 | x = x + g |
| 788 | x = self.encoder(x * x_mask, x_mask) |
| 789 | x = self.out_proj(x * x_mask) * x_mask |
| 790 | logits = x.reshape(x.shape[0], self.n_q - 1, self.dims, x.shape[-1]).transpose( |
| 791 | 2, 3 |
| 792 | ) |
| 793 | target = codes[1:].transpose(0, 1) |
| 794 | if not infer: |
| 795 | logits = logits.reshape(-1, self.dims) |
| 796 | target = target.reshape(-1) |
| 797 | loss = torch.nn.functional.cross_entropy(logits, target) |
| 798 | return loss |
| 799 | else: |
| 800 | _, top10_preds = torch.topk(logits, 10, dim=-1) |
| 801 | correct_top10 = torch.any(top10_preds == target.unsqueeze(-1), dim=-1) |
| 802 | top3_acc = 100 * torch.mean(correct_top10.float()).detach().cpu().item() |
| 803 | |
| 804 | print("Top-10 Accuracy:", top3_acc, "%") |
| 805 | |
| 806 | pred_codes = torch.argmax(logits, dim=-1) |
| 807 | acc = 100 * torch.mean((pred_codes == target).float()).detach().cpu().item() |
| 808 | print("Top-1 Accuracy:", acc, "%") |
| 809 | |
| 810 | return pred_codes.transpose(0, 1) |
| 811 | |
| 812 | |
| 813 | class SynthesizerTrn(nn.Module): |
nothing calls this directly
no outgoing calls
no test coverage detected