add model
Browse files- config.json +50 -0
- configuration_cbert.py +59 -0
- model_cbert.py +49 -0
- pytorch_model.bin +3 -0
config.json
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"BertSentiment"
|
4 |
+
],
|
5 |
+
"attention_probs_dropout_prob": 0.1,
|
6 |
+
"auto_map": {
|
7 |
+
"AutoConfig": "configuration_cbert.BertCustomConfig",
|
8 |
+
"AutoModelForSequenceClassification": "model_cbert.BertSentiment"
|
9 |
+
},
|
10 |
+
"classifier_dropout": null,
|
11 |
+
"hidden_act": "gelu",
|
12 |
+
"hidden_dropout_prob": 0.1,
|
13 |
+
"hidden_size": 768,
|
14 |
+
"hyperparams": {
|
15 |
+
"batch_size": 32,
|
16 |
+
"lr_bert": 1.3381477872420105e-05,
|
17 |
+
"lr_dense": 1.1619234627185892e-05,
|
18 |
+
"max_length": 512,
|
19 |
+
"model_name": "Transformer",
|
20 |
+
"n_epochs": 50,
|
21 |
+
"num_labels": 3,
|
22 |
+
"w_decay": 0.15190379301303872,
|
23 |
+
"warmup": 0.017012007455465432
|
24 |
+
},
|
25 |
+
"id2label": {
|
26 |
+
"0": "Neutral",
|
27 |
+
"1": "Hawkish",
|
28 |
+
"2": "Dovish"
|
29 |
+
},
|
30 |
+
"initializer_range": 0.02,
|
31 |
+
"intermediate_size": 3072,
|
32 |
+
"label2id": {
|
33 |
+
"negative": 2,
|
34 |
+
"neutral": 0,
|
35 |
+
"positive": 1
|
36 |
+
},
|
37 |
+
"layer_norm_eps": 1e-12,
|
38 |
+
"max_length": 512,
|
39 |
+
"max_position_embeddings": 512,
|
40 |
+
"model_type": "bert",
|
41 |
+
"num_attention_heads": 12,
|
42 |
+
"num_hidden_layers": 12,
|
43 |
+
"pad_token_id": 0,
|
44 |
+
"position_embedding_type": "absolute",
|
45 |
+
"torch_dtype": "float32",
|
46 |
+
"transformers_version": "4.17.0",
|
47 |
+
"type_vocab_size": 2,
|
48 |
+
"use_cache": true,
|
49 |
+
"vocab_size": 30873
|
50 |
+
}
|
configuration_cbert.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
|
4 |
+
import numpy as np
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
|
8 |
+
import torch.nn.functional as F
|
9 |
+
|
10 |
+
from transformers import PretrainedConfig
|
11 |
+
import torch.optim as optim
|
12 |
+
|
13 |
+
class BertCustomConfig(PretrainedConfig):
|
14 |
+
model_type = "bert"
|
15 |
+
|
16 |
+
def __init__(
|
17 |
+
self,
|
18 |
+
vocab_size=30873,
|
19 |
+
hidden_size=768,
|
20 |
+
num_hidden_layers=12,
|
21 |
+
num_attention_heads=12,
|
22 |
+
intermediate_size=3072,
|
23 |
+
hidden_act="gelu",
|
24 |
+
hidden_dropout_prob=0.1,
|
25 |
+
attention_probs_dropout_prob=0.1,
|
26 |
+
max_position_embeddings=512,
|
27 |
+
type_vocab_size=2,
|
28 |
+
initializer_range=0.02,
|
29 |
+
layer_norm_eps=1e-12,
|
30 |
+
pad_token_id=0,
|
31 |
+
position_embedding_type="absolute",
|
32 |
+
use_cache=True,
|
33 |
+
classifier_dropout=None,
|
34 |
+
max_length=512,
|
35 |
+
id2label={"0": "Neutral", "1": "Hawkish", "2": "Dovish"},
|
36 |
+
label2id={"positive": 1, "negative": 2, "neutral": 0},
|
37 |
+
hyperparams=None,
|
38 |
+
**kwargs
|
39 |
+
):
|
40 |
+
super().__init__(pad_token_id=pad_token_id, **kwargs)
|
41 |
+
self.vocab_size = vocab_size
|
42 |
+
self.hidden_size = hidden_size
|
43 |
+
self.num_hidden_layers = num_hidden_layers
|
44 |
+
self.num_attention_heads = num_attention_heads
|
45 |
+
self.hidden_act = hidden_act
|
46 |
+
self.intermediate_size = intermediate_size
|
47 |
+
self.hidden_dropout_prob = hidden_dropout_prob
|
48 |
+
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
49 |
+
self.max_position_embeddings = max_position_embeddings
|
50 |
+
self.type_vocab_size = type_vocab_size
|
51 |
+
self.initializer_range = initializer_range
|
52 |
+
self.layer_norm_eps = layer_norm_eps
|
53 |
+
self.position_embedding_type = position_embedding_type
|
54 |
+
self.use_cache = use_cache
|
55 |
+
self.classifier_dropout = classifier_dropout
|
56 |
+
self.max_length = max_length
|
57 |
+
self.id2label = id2label
|
58 |
+
self.label2id = label2id
|
59 |
+
self.hyperparams = hyperparams
|
model_cbert.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
|
4 |
+
import random
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
import pandas as pd
|
10 |
+
|
11 |
+
import torch.nn.functional as F
|
12 |
+
|
13 |
+
from transformers import BertModel, PreTrainedModel
|
14 |
+
from configuration_cbert import BertCustomConfig
|
15 |
+
|
16 |
+
import torch.optim as optim
|
17 |
+
|
18 |
+
class BertSentiment(PreTrainedModel):
|
19 |
+
config_class = BertCustomConfig
|
20 |
+
def __init__(self, config, weight_path=None):
|
21 |
+
super().__init__(config)
|
22 |
+
self.config = config
|
23 |
+
self.num_labels = self.config.hyperparams["num_labels"]
|
24 |
+
# self.bert = BertModel.from_pretrained('yiyanghkust/finbert-tone')
|
25 |
+
if weight_path:
|
26 |
+
self.bert = BertModel.from_pretrained(weight_path)
|
27 |
+
else:
|
28 |
+
self.bert = BertModel(self.config)
|
29 |
+
|
30 |
+
self.dropout = nn.Dropout(self.config.hidden_dropout_prob)
|
31 |
+
self.hidden = nn.Linear(self.config.hidden_size, self.config.hidden_size)
|
32 |
+
self.classifier = nn.Linear(self.config.hidden_size, self.config.hyperparams["num_labels"])
|
33 |
+
# self.classifier2 = nn.Linear(dense_size + meta_size, num_labels)
|
34 |
+
nn.init.xavier_normal_(self.hidden.weight)
|
35 |
+
nn.init.xavier_normal_(self.classifier.weight)
|
36 |
+
|
37 |
+
def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None, graphEmbeddings=None):
|
38 |
+
# _, pooled_output = self.bert(input_ids, token_type_ids, attention_mask, return_dict=False)
|
39 |
+
output, ctoken = self.bert(input_ids, token_type_ids, attention_mask, return_dict=False)
|
40 |
+
pooled_output = torch.mean(output, 1)
|
41 |
+
pooled_output = self.hidden(pooled_output)
|
42 |
+
pooled_output = self.dropout(pooled_output)
|
43 |
+
pooled_output = F.relu(pooled_output)
|
44 |
+
logits = self.classifier(pooled_output)
|
45 |
+
# dense1 = self.classifier(pooled_output)
|
46 |
+
# concatl = torch.cat((dense1, meta_data.float()), 1)
|
47 |
+
# logits = self.classifier2(concatl)
|
48 |
+
|
49 |
+
return logits
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b2d9417dfaf5df54bb639af002b149b1eef7986cf9ed2309778907a78decc6c1
|
3 |
+
size 441461673
|