-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from mim-solutions/add-multiclass-support
Add multiclass support
- Loading branch information
Showing
31 changed files
with
2,283 additions
and
2,774 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ __pycache__/ | |
.idea | ||
|
||
venv | ||
.venv | ||
|
||
belt_nlp.egg-info | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Optional | ||
|
||
from torch import argmax, Tensor | ||
from torch.nn import Module, Softmax | ||
from transformers import PreTrainedTokenizerBase | ||
|
||
from belt_nlp.bert_truncated import BertBaseTruncated | ||
|
||
|
||
class BertClassifierTruncated(BertBaseTruncated): | ||
def __init__( | ||
self, | ||
num_labels: int, | ||
batch_size: int, | ||
learning_rate: float, | ||
epochs: int, | ||
accumulation_steps: int = 1, | ||
tokenizer: Optional[PreTrainedTokenizerBase] = None, | ||
neural_network: Optional[Module] = None, | ||
pretrained_model_name_or_path: Optional[str] = "bert-base-uncased", | ||
device: str = "cuda:0", | ||
many_gpus: bool = False, | ||
): | ||
super().__init__( | ||
num_labels=num_labels, | ||
batch_size=batch_size, | ||
learning_rate=learning_rate, | ||
epochs=epochs, | ||
accumulation_steps=accumulation_steps, | ||
tokenizer=tokenizer, | ||
neural_network=neural_network, | ||
pretrained_model_name_or_path=pretrained_model_name_or_path, | ||
device=device, | ||
many_gpus=many_gpus, | ||
) | ||
additional_classifier_params = { | ||
"num_labels": self.num_labels, | ||
} | ||
self._params.update(additional_classifier_params) | ||
|
||
def predict(self, x: list[str], batch_size: Optional[int] = None) -> Tensor: | ||
"""Returns classes.""" | ||
logits = super()._predict_logits(x, batch_size) | ||
classes = argmax(logits, dim=1) | ||
return classes | ||
|
||
def predict_scores(self, x: list[str], batch_size: Optional[int] = None) -> Tensor: | ||
"""Returns classification probabilities.""" | ||
logits = super()._predict_logits(x, batch_size) | ||
softmax = Softmax(dim=1) | ||
|
||
probabilities = softmax(logits) | ||
return probabilities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Optional | ||
|
||
from torch import argmax, Tensor | ||
from torch.nn import Module, Softmax | ||
from transformers import PreTrainedTokenizerBase | ||
|
||
from belt_nlp.bert_with_pooling import BertBaseWithPooling | ||
|
||
|
||
class BertClassifierWithPooling(BertBaseWithPooling): | ||
def __init__( | ||
self, | ||
num_labels: int, | ||
batch_size: int, | ||
learning_rate: float, | ||
epochs: int, | ||
chunk_size: int, | ||
stride: int, | ||
minimal_chunk_length: int, | ||
pooling_strategy: str = "mean", | ||
accumulation_steps: int = 1, | ||
maximal_text_length: Optional[int] = None, | ||
tokenizer: Optional[PreTrainedTokenizerBase] = None, | ||
neural_network: Optional[Module] = None, | ||
pretrained_model_name_or_path: Optional[str] = "bert-base-uncased", | ||
device: str = "cuda:0", | ||
many_gpus: bool = False, | ||
): | ||
super().__init__( | ||
num_labels=num_labels, | ||
batch_size=batch_size, | ||
learning_rate=learning_rate, | ||
epochs=epochs, | ||
chunk_size=chunk_size, | ||
stride=stride, | ||
minimal_chunk_length=minimal_chunk_length, | ||
pooling_strategy=pooling_strategy, | ||
accumulation_steps=accumulation_steps, | ||
maximal_text_length=maximal_text_length, | ||
tokenizer=tokenizer, | ||
neural_network=neural_network, | ||
pretrained_model_name_or_path=pretrained_model_name_or_path, | ||
device=device, | ||
many_gpus=many_gpus, | ||
) | ||
additional_classifier_params = { | ||
"num_labels": self.num_labels, | ||
} | ||
self._params.update(additional_classifier_params) | ||
|
||
def predict(self, x: list[str], batch_size: Optional[int] = None) -> Tensor: | ||
"""Returns classes.""" | ||
logits = super()._predict_logits(x, batch_size) | ||
classes = argmax(logits, dim=1) | ||
return classes | ||
|
||
def predict_scores(self, x: list[str], batch_size: Optional[int] = None) -> Tensor: | ||
"""Returns classification probabilities.""" | ||
logits = super()._predict_logits(x, batch_size) | ||
softmax = Softmax(dim=1) | ||
|
||
probabilities = softmax(logits) | ||
return probabilities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Optional | ||
|
||
from torch import Tensor | ||
from torch.nn import Module | ||
from transformers import PreTrainedTokenizerBase | ||
|
||
from belt_nlp.bert_truncated import BertBaseTruncated | ||
|
||
|
||
class BertRegressorTruncated(BertBaseTruncated): | ||
def __init__( | ||
self, | ||
batch_size: int, | ||
learning_rate: float, | ||
epochs: int, | ||
accumulation_steps: int = 1, | ||
tokenizer: Optional[PreTrainedTokenizerBase] = None, | ||
neural_network: Optional[Module] = None, | ||
pretrained_model_name_or_path: Optional[str] = "bert-base-uncased", | ||
device: str = "cuda:0", | ||
many_gpus: bool = False, | ||
): | ||
super().__init__( | ||
num_labels=1, | ||
batch_size=batch_size, | ||
learning_rate=learning_rate, | ||
epochs=epochs, | ||
accumulation_steps=accumulation_steps, | ||
tokenizer=tokenizer, | ||
neural_network=neural_network, | ||
pretrained_model_name_or_path=pretrained_model_name_or_path, | ||
device=device, | ||
many_gpus=many_gpus, | ||
) | ||
|
||
def predict(self, x: list[str], batch_size: Optional[int] = None) -> Tensor: | ||
"""Returns regression scores.""" | ||
logits = super()._predict_logits(x, batch_size) | ||
return logits |
Oops, something went wrong.