Commit
·
c4eddb4
1
Parent(s):
84ddf97
Upload BERT_Arch
Browse files- model.py +48 -0
- pytorch_model.bin +1 -1
model.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModel, AutoConfig
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import BertPreTrainedModel, AutoModel, PreTrainedModel
|
| 4 |
+
from model_config import PragFormerConfig
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class BERT_Arch(PreTrainedModel): #(BertPreTrainedModel):
|
| 9 |
+
config_class = PragFormerConfig
|
| 10 |
+
|
| 11 |
+
def __init__(self, config):
|
| 12 |
+
super().__init__(config)
|
| 13 |
+
print(config.bert)
|
| 14 |
+
self.bert = AutoModel.from_pretrained(config.bert['_name_or_path'])
|
| 15 |
+
|
| 16 |
+
# dropout layer
|
| 17 |
+
self.dropout = nn.Dropout(config.dropout)
|
| 18 |
+
|
| 19 |
+
# relu activation function
|
| 20 |
+
self.relu = nn.ReLU()
|
| 21 |
+
|
| 22 |
+
# dense layer 1
|
| 23 |
+
self.fc1 = nn.Linear(self.config.bert['hidden_size'], config.fc1)
|
| 24 |
+
# self.fc1 = nn.Linear(768, 512)
|
| 25 |
+
|
| 26 |
+
# dense layer 2 (Output layer)
|
| 27 |
+
self.fc2 = nn.Linear(config.fc1, config.fc2)
|
| 28 |
+
|
| 29 |
+
# softmax activation function
|
| 30 |
+
self.softmax = nn.LogSoftmax(dim = config.softmax_dim)
|
| 31 |
+
|
| 32 |
+
# define the forward pass
|
| 33 |
+
def forward(self, input_ids, attention_mask):
|
| 34 |
+
# pass the inputs to the model
|
| 35 |
+
_, cls_hs = self.bert(input_ids, attention_mask = attention_mask, return_dict=False)
|
| 36 |
+
|
| 37 |
+
x = self.fc1(cls_hs)
|
| 38 |
+
|
| 39 |
+
x = self.relu(x)
|
| 40 |
+
|
| 41 |
+
x = self.dropout(x)
|
| 42 |
+
|
| 43 |
+
# output layer
|
| 44 |
+
x = self.fc2(x)
|
| 45 |
+
|
| 46 |
+
# apply softmax activation
|
| 47 |
+
x = self.softmax(x)
|
| 48 |
+
return x
|
pytorch_model.bin
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 500230377
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:778121a06acaa0709aa43c0495453496f40c3099838ed65d71545c47e6a84399
|
| 3 |
size 500230377
|