Classify x. Args: ys: Not used state: Not used x: (T, D). this should be a single sample without any padding ie batch size=1. Returns: logp: log probabilities over (n_classes,) state: None Assumes th
(self, ys, state, x)
| 77 | return output |
| 78 | |
| 79 | def score(self, ys, state, x): |
| 80 | """Classify x. |
| 81 | |
| 82 | Args: |
| 83 | ys: Not used |
| 84 | state: Not used |
| 85 | x: (T, D). this should be a single sample without |
| 86 | any padding ie batch size=1. |
| 87 | Returns: |
| 88 | logp: log probabilities over (n_classes,) |
| 89 | state: None |
| 90 | Assumes that x is a single unpadded sequence. |
| 91 | """ |
| 92 | hs_len = torch.tensor([x.shape[0]], dtype=torch.long).to(x.device) |
| 93 | logits = self.forward( |
| 94 | x.unsqueeze(0), |
| 95 | hs_len, |
| 96 | ) |
| 97 | logp = torch.nn.functional.log_softmax(logits, dim=-1) |
| 98 | # Fix blank, unk and sos/eos to -inf |
| 99 | minf_tensor = torch.tensor(float("-inf"), device=logp.device) |
| 100 | minf_tensor = minf_tensor.expand(*(logp.shape[:-1]), 1) |
| 101 | logp = torch.cat([minf_tensor, minf_tensor, logp, minf_tensor], dim=-1) |
| 102 | return logp.squeeze(0), None |
| 103 | |
| 104 | def output_size(self) -> int: |
| 105 | """Get the output size.""" |