Download src/model/encoder.py from Venkatdatta/Neuro-Symbolic-Reasoning-Engine: direct link, hf CLI and curl.
- Browser
- Download file 736 Bytes
-
https://huggingface.co/spaces/Venkatdatta/Neuro-Symbolic-Reasoning-Engine/resolve/main/src/model/encoder.py
- Command line
-
hf download hf://spaces/Venkatdatta/Neuro-Symbolic-Reasoning-Engine/src/model/encoder.py
-
curl -L -o encoder.py https://huggingface.co/spaces/Venkatdatta/Neuro-Symbolic-Reasoning-Engine/resolve/main/src/model/encoder.py
736 Bytes
| import torch | |
| import torch.nn as nn | |
| from transformers import T5EncoderModel, AutoTokenizer | |
| class FrozenT5Encoder(nn.Module): | |
| def __init__(self, model_name: str = "t5-base"): | |
| super().__init__() | |
| self.encoder = T5EncoderModel.from_pretrained(model_name) | |
| self.hidden_size = self.encoder.config.d_model | |
| # Freeze all encoder parameters | |
| for param in self.encoder.parameters(): | |
| param.requires_grad = False | |
| def forward(self, input_ids, attention_mask=None): | |
| with torch.no_grad(): | |
| outputs = self.encoder( | |
| input_ids=input_ids, | |
| attention_mask=attention_mask, | |
| ) | |
| return outputs.last_hidden_state # (B, S, d_model) | |