amg2118 commited on
Commit ·
e964ca4
1
Parent(s): 5a3af54
Upload model with LFS tracking
Browse files- README.md +0 -0
- config.json +25 -0
- model.safetensors +3 -0
- modelling_bert_regression.py +37 -0
README.md
ADDED
|
File without changes
|
config.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"BertRegressionModel"
|
| 4 |
+
],
|
| 5 |
+
"attention_probs_dropout_prob": 0.1,
|
| 6 |
+
"classifier_dropout": null,
|
| 7 |
+
"gradient_checkpointing": false,
|
| 8 |
+
"hidden_act": "gelu",
|
| 9 |
+
"hidden_dropout_prob": 0.1,
|
| 10 |
+
"hidden_size": 768,
|
| 11 |
+
"initializer_range": 0.02,
|
| 12 |
+
"intermediate_size": 3072,
|
| 13 |
+
"layer_norm_eps": 1e-12,
|
| 14 |
+
"max_position_embeddings": 512,
|
| 15 |
+
"model_type": "bert",
|
| 16 |
+
"num_attention_heads": 12,
|
| 17 |
+
"num_hidden_layers": 12,
|
| 18 |
+
"pad_token_id": 0,
|
| 19 |
+
"position_embedding_type": "absolute",
|
| 20 |
+
"torch_dtype": "float32",
|
| 21 |
+
"transformers_version": "4.53.2",
|
| 22 |
+
"type_vocab_size": 2,
|
| 23 |
+
"use_cache": true,
|
| 24 |
+
"vocab_size": 30522
|
| 25 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c467472adfb17f9cde1163d0822354399bc73151590d41df15a088ae0a4ddbf9
|
| 3 |
+
size 437955572
|
modelling_bert_regression.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import BertTokenizer, BertModel, BertPreTrainedModel, AutoModel
|
| 4 |
+
|
| 5 |
+
# Configuration
|
| 6 |
+
MODEL_NAME = "bert-base-uncased"
|
| 7 |
+
MAX_LENGTH = 128
|
| 8 |
+
BATCH_SIZE = 16
|
| 9 |
+
NUM_EPOCHS = 3
|
| 10 |
+
LEARNING_RATE = 2e-5
|
| 11 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
tokenizer = BertTokenizer.from_pretrained(MODEL_NAME)
|
| 15 |
+
|
| 16 |
+
def preprocess(arguments):
|
| 17 |
+
encoding = tokenizer(arguments, truncation=True, padding="max_length", max_length=MAX_LENGTH)
|
| 18 |
+
return encoding
|
| 19 |
+
|
| 20 |
+
class BertRegressionModel(BertPreTrainedModel):
|
| 21 |
+
def __init__(self, config):
|
| 22 |
+
super().__init__(config)
|
| 23 |
+
self.bert = BertModel(config)
|
| 24 |
+
self.regressor = nn.Sequential(
|
| 25 |
+
nn.Dropout(config.hidden_dropout_prob),
|
| 26 |
+
nn.Linear(config.hidden_size, 1),
|
| 27 |
+
nn.Sigmoid() # ensures output is in [0, 1]
|
| 28 |
+
)
|
| 29 |
+
self.init_weights()
|
| 30 |
+
|
| 31 |
+
def forward(self, input_ids, attention_mask, labels=None):
|
| 32 |
+
outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask)
|
| 33 |
+
pooled_output = outputs.pooler_output
|
| 34 |
+
preds = self.regressor(pooled_output).squeeze(-1)
|
| 35 |
+
return preds
|
| 36 |
+
|
| 37 |
+
AutoModel.register(BertRegressionModel)
|