Download positional_encoding.py from drixo/realtime-tts: direct link, hf CLI and curl.
- Browser
- Download file 646 Bytes
-
https://huggingface.co/drixo/realtime-tts/resolve/main/positional_encoding.py
- Command line
-
hf download hf://drixo/realtime-tts/positional_encoding.py
-
curl -L -o positional_encoding.py https://huggingface.co/drixo/realtime-tts/resolve/main/positional_encoding.py
646 Bytes
| import math | |
| import torch | |
| import torch.nn as nn | |
| class PositionalEncoding(nn.Module): | |
| def __init__(self, d_model, max_len=5000): | |
| super().__init__() | |
| pe = torch.zeros(max_len, d_model) | |
| position = torch.arange(0, max_len).unsqueeze(1) | |
| div_term = torch.exp( | |
| torch.arange(0, d_model, 2) * | |
| (-math.log(10000.0) / d_model) | |
| ) | |
| pe[:, 0::2] = torch.sin(position * div_term) | |
| pe[:, 1::2] = torch.cos(position * div_term) | |
| pe = pe.unsqueeze(0) | |
| self.register_buffer("pe", pe) | |
| def forward(self, x): | |
| x = x + self.pe[:, :x.size(1)] | |
| return x | |