Upload FupBERT
Browse files- config.json +24 -0
- fup_bert.py +46 -0
- fup_bert_config.py +49 -0
- fup_bert_model.py +175 -0
- positional_encoding.py +77 -0
- pytorch_model.bin +3 -0
config.json
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "../models/bert_saved",
|
3 |
+
"architectures": [
|
4 |
+
"FupBERT"
|
5 |
+
],
|
6 |
+
"auto_map": {
|
7 |
+
"AutoConfig": "fup_bert_config.FupBERTConfig",
|
8 |
+
"AutoModel": "fup_bert.FupBERT"
|
9 |
+
},
|
10 |
+
"cls_idx": 1,
|
11 |
+
"dropout": 0.1,
|
12 |
+
"edge_idx": 2,
|
13 |
+
"model_type": "FupBERT",
|
14 |
+
"nhead": 12,
|
15 |
+
"nhid": 3072,
|
16 |
+
"ninp": 768,
|
17 |
+
"nlayers": 12,
|
18 |
+
"ntoken": 608,
|
19 |
+
"num_out": 1,
|
20 |
+
"padding_idx": 0,
|
21 |
+
"token_reduction": "mean",
|
22 |
+
"torch_dtype": "float32",
|
23 |
+
"transformers_version": "4.30.2"
|
24 |
+
}
|
fup_bert.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
© Battelle Memorial Institute 2023
|
3 |
+
Made available under the GNU General Public License v 2.0
|
4 |
+
|
5 |
+
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
6 |
+
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
7 |
+
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
8 |
+
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
9 |
+
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
10 |
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
11 |
+
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
12 |
+
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
13 |
+
REPAIR OR CORRECTION.
|
14 |
+
"""
|
15 |
+
|
16 |
+
import torch
|
17 |
+
from transformers import PreTrainedModel
|
18 |
+
|
19 |
+
from .fup_bert_config import FupBERTConfig
|
20 |
+
from .fup_bert_model import FupBERTModel
|
21 |
+
|
22 |
+
|
23 |
+
class FupBERT(PreTrainedModel):
|
24 |
+
"""Hugging Face Wrapper"""
|
25 |
+
config_class = FupBERTConfig
|
26 |
+
|
27 |
+
def __init__(self, config):
|
28 |
+
super().__init__(config)
|
29 |
+
self.model = FupBERTModel(ntoken=config.ntoken,
|
30 |
+
ninp=config.ninp,
|
31 |
+
nhead=config.nhead,
|
32 |
+
nhid=config.nhid,
|
33 |
+
nlayers=config.nlayers,
|
34 |
+
token_reduction=config.token_reduction,
|
35 |
+
padding_idx=config.padding_idx,
|
36 |
+
cls_idx=config.cls_idx,
|
37 |
+
edge_idx=config.edge_idx,
|
38 |
+
num_out=config.num_out,
|
39 |
+
dropout=config.dropout,
|
40 |
+
)
|
41 |
+
|
42 |
+
def forward(self, src):
|
43 |
+
return self.model(src)
|
44 |
+
|
45 |
+
def load_params(self, pt_file):
|
46 |
+
self.model.load_state_dict(torch.load(pt_file))
|
fup_bert_config.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
© Battelle Memorial Institute 2023
|
3 |
+
Made available under the GNU General Public License v 2.0
|
4 |
+
|
5 |
+
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
6 |
+
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
7 |
+
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
8 |
+
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
9 |
+
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
10 |
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
11 |
+
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
12 |
+
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
13 |
+
REPAIR OR CORRECTION.
|
14 |
+
"""
|
15 |
+
|
16 |
+
from transformers import PretrainedConfig
|
17 |
+
|
18 |
+
|
19 |
+
class FupBERTConfig(PretrainedConfig):
|
20 |
+
model_type = "FupBERT"
|
21 |
+
|
22 |
+
def __init__(
|
23 |
+
self,
|
24 |
+
ntoken=608,
|
25 |
+
ninp=768,
|
26 |
+
nhead=12,
|
27 |
+
nhid=3072,
|
28 |
+
nlayers=12,
|
29 |
+
token_reduction='mean',
|
30 |
+
padding_idx=0,
|
31 |
+
cls_idx=1,
|
32 |
+
edge_idx=2,
|
33 |
+
num_out=1,
|
34 |
+
dropout=0.1,
|
35 |
+
**kwargs):
|
36 |
+
|
37 |
+
# Store the input parameters
|
38 |
+
self.ntoken = ntoken
|
39 |
+
self.ninp = ninp
|
40 |
+
self.nhead = nhead
|
41 |
+
self.nhid = nhid
|
42 |
+
self.nlayers = nlayers
|
43 |
+
self.token_reduction = token_reduction
|
44 |
+
self.padding_idx = padding_idx
|
45 |
+
self.cls_idx = cls_idx
|
46 |
+
self.edge_idx = edge_idx
|
47 |
+
self.num_out = num_out
|
48 |
+
self.dropout = dropout
|
49 |
+
super().__init__(**kwargs)
|
fup_bert_model.py
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
© Battelle Memorial Institute 2023
|
3 |
+
Made available under the GNU General Public License v 2.0
|
4 |
+
|
5 |
+
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
6 |
+
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
7 |
+
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
8 |
+
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
9 |
+
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
10 |
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
11 |
+
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
12 |
+
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
13 |
+
REPAIR OR CORRECTION.
|
14 |
+
"""
|
15 |
+
|
16 |
+
import torch
|
17 |
+
import torch.nn as nn
|
18 |
+
|
19 |
+
from .positional_encoding import PositionalEncoding
|
20 |
+
|
21 |
+
|
22 |
+
class FupBERTModel(nn.Module):
|
23 |
+
"""
|
24 |
+
A class that extends torch.nn.Module that implements a custom Transformer
|
25 |
+
encoder model to create a single embedding for Fup prediction.
|
26 |
+
"""
|
27 |
+
|
28 |
+
def __init__(
|
29 |
+
self,
|
30 |
+
ntoken,
|
31 |
+
ninp,
|
32 |
+
nhead,
|
33 |
+
nhid,
|
34 |
+
nlayers,
|
35 |
+
token_reduction,
|
36 |
+
padding_idx,
|
37 |
+
cls_idx,
|
38 |
+
edge_idx,
|
39 |
+
num_out,
|
40 |
+
dropout=0.1,
|
41 |
+
):
|
42 |
+
"""
|
43 |
+
Initializes a FubBERT object.
|
44 |
+
|
45 |
+
Parameters
|
46 |
+
----------
|
47 |
+
ntoken : int
|
48 |
+
The maximum number of tokens the embedding layer should expect. This
|
49 |
+
is the same as the size of the vocabulary.
|
50 |
+
ninp : int
|
51 |
+
The hidden dimension that should be used for embedding and input
|
52 |
+
to the Transformer encoder.
|
53 |
+
nhead : int
|
54 |
+
The number of heads to use in the Transformer encoder.
|
55 |
+
nhid : int
|
56 |
+
The size of the hidden dimension to use throughout the Transformer
|
57 |
+
encoder.
|
58 |
+
nlayers : int
|
59 |
+
The number of layers to use in a single head of the Transformer
|
60 |
+
encoder.
|
61 |
+
token_reduction : str
|
62 |
+
The type of token reduction to use. This can be either 'mean' or
|
63 |
+
'cls'.
|
64 |
+
padding_idx : int
|
65 |
+
The index used as padding for the input sequences.
|
66 |
+
cls_idx : int
|
67 |
+
The index used as the cls token for the input sequences.
|
68 |
+
edge_idx : int
|
69 |
+
The index used as the edge token for the input sequences.
|
70 |
+
num_out : int
|
71 |
+
The number of outputs to predict with the model.
|
72 |
+
dropout : float, optional
|
73 |
+
The fractional dropout to apply to the model. The default is 0.1.
|
74 |
+
|
75 |
+
Returns
|
76 |
+
-------
|
77 |
+
None.
|
78 |
+
|
79 |
+
"""
|
80 |
+
super(FupBERTModel, self).__init__()
|
81 |
+
# Store the input parameters
|
82 |
+
self.ntoken = ntoken
|
83 |
+
self.ninp = ninp
|
84 |
+
self.nhead = nhead
|
85 |
+
self.nhid = nhid
|
86 |
+
self.nlayers = nlayers
|
87 |
+
self.token_reduction = token_reduction
|
88 |
+
self.padding_idx = padding_idx
|
89 |
+
self.cls_idx = cls_idx
|
90 |
+
self.edge_idx = edge_idx
|
91 |
+
self.num_out = num_out
|
92 |
+
self.dropout = dropout
|
93 |
+
# Set the model parameters
|
94 |
+
self.model_type = "Transformer Encoder"
|
95 |
+
self.embedding = nn.Embedding(
|
96 |
+
self.ntoken, self.ninp, padding_idx=self.padding_idx
|
97 |
+
)
|
98 |
+
self.pos_encoder = PositionalEncoding(self.ninp, self.dropout)
|
99 |
+
encoder_layers = nn.TransformerEncoderLayer(
|
100 |
+
self.ninp,
|
101 |
+
self.nhead,
|
102 |
+
self.nhid,
|
103 |
+
self.dropout,
|
104 |
+
activation="gelu",
|
105 |
+
batch_first=True,
|
106 |
+
)
|
107 |
+
self.transformer_encoder = nn.TransformerEncoder(encoder_layers, self.nlayers)
|
108 |
+
self.pred_head = nn.Linear(self.ninp, self.num_out)
|
109 |
+
|
110 |
+
def _generate_src_key_mask(self, src):
|
111 |
+
mask = src == self.padding_idx
|
112 |
+
mask = mask.type(torch.bool)
|
113 |
+
|
114 |
+
return mask
|
115 |
+
|
116 |
+
def forward(self, src):
|
117 |
+
"""
|
118 |
+
Perform a forward pass of the module.
|
119 |
+
|
120 |
+
Parameters
|
121 |
+
----------
|
122 |
+
src : tensor
|
123 |
+
The input tensor. The shape should be (batch size, sequence length).
|
124 |
+
|
125 |
+
Returns
|
126 |
+
-------
|
127 |
+
output : tensor
|
128 |
+
The output tensor. The shape will be (batch size, num_out).
|
129 |
+
|
130 |
+
"""
|
131 |
+
src = self.get_embeddings(src)
|
132 |
+
output = self.pred_head(src)
|
133 |
+
|
134 |
+
return output
|
135 |
+
|
136 |
+
def get_embeddings(self, src):
|
137 |
+
"""
|
138 |
+
Perform a forward pass of the module excluding the classification layers. This
|
139 |
+
will return the embeddings from the encoder.
|
140 |
+
|
141 |
+
Parameters
|
142 |
+
----------
|
143 |
+
src : tensor
|
144 |
+
The input tensor. The shape should be (batch size, sequence length).
|
145 |
+
|
146 |
+
Returns
|
147 |
+
-------
|
148 |
+
embeds : tensor
|
149 |
+
The output tensor of sequence embeddings. The shape should be
|
150 |
+
(batch size, self.ninp)
|
151 |
+
"""
|
152 |
+
src_mask = self._generate_src_key_mask(src)
|
153 |
+
x = self.embedding(src)
|
154 |
+
x = self.pos_encoder(x)
|
155 |
+
x = self.transformer_encoder(x, src_key_padding_mask=src_mask)
|
156 |
+
# Mask the data based on the token reduction strategy
|
157 |
+
if self.token_reduction == "mean":
|
158 |
+
pad_mask = src == self.padding_idx
|
159 |
+
cls_mask = src == self.cls_idx
|
160 |
+
edge_mask = src == self.edge_idx
|
161 |
+
mask = torch.logical_or(pad_mask, cls_mask)
|
162 |
+
mask = torch.logical_or(mask, edge_mask)
|
163 |
+
# Apply the mask
|
164 |
+
x[mask[:, : x.shape[1]]] = torch.nan
|
165 |
+
# Take the mean of the embeddings
|
166 |
+
embeds = torch.nanmean(x, dim=1)
|
167 |
+
elif self.token_reduction == "cls":
|
168 |
+
embeds = x[:, 0, :]
|
169 |
+
else:
|
170 |
+
raise ValueError(
|
171 |
+
"Token reduction must be mean or cls. "
|
172 |
+
"Recieved {}".format(self.token_reduction)
|
173 |
+
)
|
174 |
+
|
175 |
+
return embeds
|
positional_encoding.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
© Battelle Memorial Institute 2023
|
3 |
+
Made available under the GNU General Public License v 2.0
|
4 |
+
|
5 |
+
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
6 |
+
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
7 |
+
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
8 |
+
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
9 |
+
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
10 |
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
11 |
+
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
12 |
+
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
13 |
+
REPAIR OR CORRECTION.
|
14 |
+
"""
|
15 |
+
|
16 |
+
import numpy as np
|
17 |
+
import torch
|
18 |
+
import torch.nn as nn
|
19 |
+
|
20 |
+
|
21 |
+
class PositionalEncoding(nn.Module):
|
22 |
+
"""
|
23 |
+
A class that extends torch.nn.Module that applies positional encoding
|
24 |
+
for use in the Transformer architecture.
|
25 |
+
"""
|
26 |
+
|
27 |
+
def __init__(self, d_model, dropout=0.1, max_len=5000):
|
28 |
+
"""
|
29 |
+
Initializes a PositionalEncoding object.
|
30 |
+
|
31 |
+
Parameters
|
32 |
+
----------
|
33 |
+
d_model : int
|
34 |
+
The size of the model's embedding dimension.
|
35 |
+
dropout : float, optional
|
36 |
+
The fractional dropout to apply to the embedding. The default is 0.1.
|
37 |
+
max_len : int, optional
|
38 |
+
The maximum potential input sequnce length. The default is 5000.
|
39 |
+
|
40 |
+
Returns
|
41 |
+
-------
|
42 |
+
None.
|
43 |
+
|
44 |
+
"""
|
45 |
+
super(PositionalEncoding, self).__init__()
|
46 |
+
# Create the dropout
|
47 |
+
self.dropout = nn.Dropout(p=dropout)
|
48 |
+
# Create the encoding
|
49 |
+
pe = torch.zeros(max_len, d_model)
|
50 |
+
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
|
51 |
+
div_term = torch.exp(
|
52 |
+
torch.arange(0, d_model, 2).float() * (-np.log(10000.0) / d_model)
|
53 |
+
)
|
54 |
+
pe[:, 0::2] = torch.sin(position * div_term)
|
55 |
+
pe[:, 1::2] = torch.cos(position * div_term)
|
56 |
+
pe = pe.unsqueeze(0)
|
57 |
+
self.register_buffer("pe", pe)
|
58 |
+
|
59 |
+
def forward(self, x):
|
60 |
+
"""
|
61 |
+
Perform a forward pass of the module.
|
62 |
+
|
63 |
+
Parameters
|
64 |
+
----------
|
65 |
+
x : tensor
|
66 |
+
The input tensor to apply the positional encoding to.
|
67 |
+
|
68 |
+
Returns
|
69 |
+
-------
|
70 |
+
tensor
|
71 |
+
The resulting tensor after applying the positional encoding to the
|
72 |
+
input.
|
73 |
+
|
74 |
+
"""
|
75 |
+
x = x + self.pe[:, : x.size(1)]
|
76 |
+
|
77 |
+
return self.dropout(x)
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b1725dfa40d0d322410ec72be0d99579481476620697d918753a129a01e71137
|
3 |
+
size 357497565
|