Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,58 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
---
|
| 4 |
+
|
| 5 |
+
First we define a class T5RegressionModel:
|
| 6 |
+
```python
|
| 7 |
+
from transformers import (
|
| 8 |
+
T5Config,
|
| 9 |
+
T5EncoderModel,
|
| 10 |
+
T5Tokenizer,
|
| 11 |
+
PreTrainedModel,
|
| 12 |
+
TrainingArguments,
|
| 13 |
+
Trainer,
|
| 14 |
+
DataCollatorWithPadding,
|
| 15 |
+
)
|
| 16 |
+
class T5RegressionModel(PreTrainedModel):
|
| 17 |
+
|
| 18 |
+
config_class = T5Config
|
| 19 |
+
|
| 20 |
+
def __init__(self, config, d_model=None):
|
| 21 |
+
super().__init__(config)
|
| 22 |
+
|
| 23 |
+
self.encoder = T5EncoderModel.from_pretrained("Rostlab/prot_t5_xl_uniref50")
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
hidden_dim = d_model if d_model is not None else config.d_model
|
| 27 |
+
self.regression_head = nn.Linear(hidden_dim, 1)
|
| 28 |
+
|
| 29 |
+
def forward(
|
| 30 |
+
self,
|
| 31 |
+
input_ids=None,
|
| 32 |
+
attention_mask=None,
|
| 33 |
+
labels=None,
|
| 34 |
+
**kwargs
|
| 35 |
+
):
|
| 36 |
+
encoder_outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
|
| 37 |
+
hidden_states = encoder_outputs.last_hidden_state
|
| 38 |
+
|
| 39 |
+
mask = attention_mask.unsqueeze(-1)
|
| 40 |
+
pooled_output = (hidden_states * mask).sum(dim=1) / mask.sum(dim=1)
|
| 41 |
+
logits = self.regression_head(pooled_output).squeeze(-1) # [batch_size]
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
loss = None
|
| 45 |
+
if labels is not None:
|
| 46 |
+
labels = labels.float()
|
| 47 |
+
loss = nn.MSELoss()(logits, labels)
|
| 48 |
+
|
| 49 |
+
return {
|
| 50 |
+
"loss": loss,
|
| 51 |
+
"logits": logits
|
| 52 |
+
}
|
| 53 |
+
```
|
| 54 |
+
Then we load our pretrained model
|
| 55 |
+
```python
|
| 56 |
+
tokenizer = T5Tokenizer.from_pretrained("jiaxie/DeepProtT5-Stability", do_lower_case=False)
|
| 57 |
+
model = T5RegressionModel.from_pretrained("jiaxie/DeepProtT5-Stability", torch_dtype=torch.bfloat16).to("cuda")
|
| 58 |
+
```
|