Spaces:
Sleeping
Sleeping
Download src/classifier_model.py from AstraTeam/astra: direct link, hf CLI and curl.
- Browser
- Download file 1.55 kB
-
https://huggingface.co/spaces/AstraTeam/astra/resolve/refs%2Fpr%2F2/src/classifier_model.py
- Command line
-
hf download hf://spaces/AstraTeam/astra@refs/pr/2/src/classifier_model.py
-
curl -L -o classifier_model.py https://huggingface.co/spaces/AstraTeam/astra/resolve/refs%2Fpr%2F2/src/classifier_model.py
1.55 kB
| import torch | |
| import torch.nn as nn | |
| from .bert import BERT | |
| class BERTForClassification(nn.Module): | |
| """ | |
| Fine-tune Task Classifier Model | |
| """ | |
| def __init__(self, bert: BERT, vocab_size, n_labels): | |
| """ | |
| :param bert: BERT model which should be trained | |
| :param vocab_size: total vocab size | |
| :param n_labels: number of labels for the task | |
| """ | |
| super().__init__() | |
| self.bert = bert | |
| self.linear = nn.Linear(self.bert.hidden, n_labels) | |
| def forward(self, x, segment_label): | |
| x = self.bert(x, segment_label) | |
| return self.linear(x[:, 0]) | |
| class BERTForClassificationWithFeats(nn.Module): | |
| """ | |
| Fine-tune Task Classifier Model | |
| BERT embeddings concatenated with features | |
| """ | |
| def __init__(self, bert: BERT, n_labels, feat_size=9): | |
| """ | |
| :param bert: BERT model which should be trained | |
| :param vocab_size: total vocab size | |
| :param n_labels: number of labels for the task | |
| """ | |
| super().__init__() | |
| self.bert = bert | |
| # self.linear1 = nn.Linear(self.bert.hidden+feat_size, 128) | |
| self.linear = nn.Linear(self.bert.hidden+feat_size, n_labels) | |
| # self.RELU = nn.ReLU() | |
| # self.linear2 = nn.Linear(128, n_labels) | |
| def forward(self, x, segment_label, feat): | |
| x = self.bert(x, segment_label) | |
| x = torch.cat((x[:, 0], feat), dim=-1) | |
| # x = self.linear1(x) | |
| # x = self.RELU(x) | |
| # return self.linear2(x) | |
| return self.linear(x) |