File size: 3,643 Bytes
c52fe04 257d747 c52fe04 257d747 c52fe04 |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
from transformers import BertPreTrainedModel, AutoModel, PretrainedConfig
import sys
sys.path.append("..")
import torch.nn as nn
from Classifier.pragformer_config import PragFormerConfig
class BERT_Arch(BertPreTrainedModel):
config_class = PragFormerConfig
def __init__(self, config):
super().__init__(config)
self.bert = AutoModel.from_pretrained(config.bert['_name_or_path'])
# dropout layer
self.dropout = nn.Dropout(config.dropout)
# relu activation function
self.relu = nn.ReLU()
# dense layer 1
self.fc1 = nn.Linear(self.config.bert['hidden_size'], config.fc1)
# self.fc1 = nn.Linear(768, 512)
# dense layer 2 (Output layer)
self.fc2 = nn.Linear(config.fc1, config.fc2)
# softmax activation function
self.softmax = nn.LogSoftmax(dim = config.softmax_dim)
# define the forward pass
def forward(self, input_ids, attention_mask):
# pass the inputs to the model
_, cls_hs = self.bert(input_ids, attention_mask = attention_mask, return_dict=False)
x = self.fc1(cls_hs)
x = self.relu(x)
x = self.dropout(x)
# output layer
x = self.fc2(x)
# apply softmax activation
x = self.softmax(x)
return x
# class BERT_Arch_new(BertPreTrainedModel):
# def __init__(self, config):
# super().__init__(config)
# self.bert = AutoModel.from_pretrained('/home/talkad/Desktop/pragformer/PragFormer/DeepSCC-RoBERTa')
# # dropout layer
# self.dropout = nn.Dropout(0.2)
# # relu activation function
# self.relu = nn.ReLU()
# # dense layer 1
# self.fc1 = nn.Linear(self.config.hidden_size, 512)
# # self.fc1 = nn.Linear(768, 512)
# # dense layer 2 (Output layer)
# self.fc2 = nn.Linear(512, 2)
# # softmax activation function
# self.softmax = nn.LogSoftmax(dim = 1)
# # define the forward pass
# def forward(self, input_ids, attention_mask):
# # pass the inputs to the model
# _, cls_hs = self.bert(input_ids, attention_mask = attention_mask, return_dict=False)
# x = self.fc1(cls_hs)
# x = self.relu(x)
# x = self.dropout(x)
# # output layer
# x = self.fc2(x)
# # apply softmax activation
# x = self.softmax(x)
# return x
# class BERT_Arch(nn.Module):
# def __init__(self, bert):
# super(BERT_Arch, self).__init__()
# self.bert = bert
# # dropout layer
# self.dropout = nn.Dropout(0.2)
# # relu activation function
# self.relu = nn.ReLU()
# # dense layer 1
# self.fc1 = nn.Linear(768, 512)
# # dense layer 2 (Output layer)
# self.fc2 = nn.Linear(512, 2)
# # softmax activation function
# self.softmax = nn.LogSoftmax(dim = 1)
# # define the forward pass
# def forward(self, input_ids, attention_mask):
# # pass the inputs to the model
# _, cls_hs = self.bert(input_ids, attention_mask = attention_mask, return_dict=False)
# x = self.fc1(cls_hs)
# x = self.relu(x)
# x = self.dropout(x)
# # output layer
# x = self.fc2(x)
# # apply softmax activation
# x = self.softmax(x)
# return x
# def save_pretrained_model(self, path="", push=False, repo_name=""):
# if not push:
# self.bert.save_pretrained(path, repo_url=repo_name)
# else:
# self.bert.push_to_hub(repo_name)
|