Spaces:
Runtime error
Runtime error
Download bert_pytorch/model/embedding/position.py from MukeshKapoor25/logbert_processor: direct link, hf CLI and curl.
- Browser
- Download file 710 Bytes
-
https://huggingface.co/spaces/MukeshKapoor25/logbert_processor/resolve/main/bert_pytorch/model/embedding/position.py
- Command line
-
hf download hf://spaces/MukeshKapoor25/logbert_processor/bert_pytorch/model/embedding/position.py
-
curl -L -o position.py https://huggingface.co/spaces/MukeshKapoor25/logbert_processor/resolve/main/bert_pytorch/model/embedding/position.py
710 Bytes
| import torch.nn as nn | |
| import torch | |
| import math | |
| class PositionalEmbedding(nn.Module): | |
| def __init__(self, d_model, max_len=512): | |
| super().__init__() | |
| # Compute the positional encodings once in log space. | |
| pe = torch.zeros(max_len, d_model).float() | |
| pe.require_grad = False | |
| position = torch.arange(0, max_len).float().unsqueeze(1) | |
| div_term = (torch.arange(0, d_model, 2).float() * -(math.log(10000.0) / d_model)).exp() | |
| 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): | |
| return self.pe[:, :x.size(1)] | |