Spaces:
Sleeping
Sleeping
Upload llmprop_model.py with huggingface_hub
Browse files- llmprop_model.py +43 -0
llmprop_model.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
T5 finetuning on materials property prediction using materials text description
|
| 3 |
+
"""
|
| 4 |
+
# Import packages
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
|
| 9 |
+
class T5Predictor(nn.Module):
|
| 10 |
+
def __init__(
|
| 11 |
+
self,
|
| 12 |
+
base_model,
|
| 13 |
+
base_model_output_size,
|
| 14 |
+
n_classes=1,
|
| 15 |
+
drop_rate=0.1,
|
| 16 |
+
pooling='cls'
|
| 17 |
+
):
|
| 18 |
+
super(T5Predictor, self).__init__()
|
| 19 |
+
D_in, D_out = base_model_output_size, n_classes
|
| 20 |
+
self.model = base_model
|
| 21 |
+
self.dropout = nn.Dropout(drop_rate)
|
| 22 |
+
self.pooling = pooling
|
| 23 |
+
|
| 24 |
+
# instantiate a linear regressor
|
| 25 |
+
self.linear_regressor = nn.Sequential(
|
| 26 |
+
nn.Dropout(drop_rate),
|
| 27 |
+
nn.Linear(D_in, D_out)
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
def forward(self, input_ids, attention_masks):
|
| 31 |
+
|
| 32 |
+
hidden_states = self.model(input_ids, attention_masks)
|
| 33 |
+
|
| 34 |
+
last_hidden_state = hidden_states.last_hidden_state # [batch_size, input_length, D_in]
|
| 35 |
+
|
| 36 |
+
if self.pooling == 'cls':
|
| 37 |
+
input_embedding = last_hidden_state[:,0,:] # [batch_size, D_in] -- [CLS] pooling
|
| 38 |
+
elif self.pooling == 'mean':
|
| 39 |
+
input_embedding = last_hidden_state.mean(dim=1) # [batch_size, D_in] -- mean pooling
|
| 40 |
+
|
| 41 |
+
outputs = self.linear_regressor(input_embedding) # [batch_size, D_out]
|
| 42 |
+
|
| 43 |
+
return input_embedding, outputs
|