-
Notifications
You must be signed in to change notification settings - Fork 2
/
llmprop_model.py
54 lines (46 loc) · 1.88 KB
/
llmprop_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
T5 finetuning on materials property prediction using materials text description
"""
# Import packages
import torch
import torch.nn as nn
import torch.nn.functional as F
class T5Predictor(nn.Module):
def __init__(
self,
base_model,
base_model_output_size,
n_classes=1,
drop_rate=0.5,
freeze_base_model=False,
bidirectional=True,
pooling='cls'
):
super(T5Predictor, self).__init__()
D_in, D_out = base_model_output_size, n_classes
self.model = base_model
self.dropout = nn.Dropout(drop_rate)
self.pooling = pooling
# instantiate a linear layer
self.linear_layer = nn.Sequential(
nn.Dropout(drop_rate),
nn.Linear(D_in, D_out)
)
# Instantiate a more complex network with an additional hidden layer
# self.linear_layer = nn.Sequential(
# nn.Linear(D_in, 512), # Additional hidden layer with 512 units
# nn.ReLU(), # Activation function
# nn.Dropout(drop_rate),
# nn.Linear(512, D_out) # Output layer
# )
def forward(self, input_ids, attention_masks):
hidden_states = self.model(input_ids, attention_masks)
last_hidden_state = hidden_states.last_hidden_state # [batch_size, input_length, D_in]
if self.pooling == 'cls':
input_embedding = last_hidden_state[:,0,:] # [batch_size,tokens,Dimension]-->[batch_size, D_in] -- [CLS] pooling
elif self.pooling == 'mean':
input_embedding = last_hidden_state.mean(dim=1) # [batch_size, D_in] -- mean pooling
elif self.pooling == 'max':
input_embedding = last_hidden_state.max(dim=1).values # [batch_size, D_in] -- max pooling
outputs = self.linear_layer(input_embedding) # [batch_size, D_out]
return input_embedding, outputs