| import torch |
| import torch.nn as nn |
| from transformers import PreTrainedModel, PretrainedConfig |
|
|
| |
| class AITextClassifierConfig(PretrainedConfig): |
| model_type = "ai_text_classifier" |
| |
| def __init__( |
| self, |
| input_dim: int = 1024, |
| dropout_high: float = 0.25, |
| dropout_low: float = 0.1, |
| **kwargs |
| ): |
| super().__init__(**kwargs) |
| self.input_dim = input_dim |
| self.dropout_high = dropout_high |
| self.dropout_low = dropout_low |
|
|
|
|
| |
| class AITextClassifier(PreTrainedModel): |
| config_class = AITextClassifierConfig |
|
|
| |
| def __init__(self, config: AITextClassifierConfig): |
| super().__init__(config) |
|
|
| |
| self.network = nn.Sequential( |
| |
| nn.Linear(config.input_dim, 512), |
| nn.BatchNorm1d(512), |
| nn.GELU(), |
| nn.Dropout(config.dropout_high), |
|
|
| |
| nn.Linear(512, 256), |
| nn.BatchNorm1d(256), |
| nn.GELU(), |
| nn.Dropout(config.dropout_high), |
|
|
| |
| nn.Linear(256, 128), |
| nn.BatchNorm1d(128), |
| nn.GELU(), |
| nn.Dropout(config.dropout_low), |
|
|
| |
| nn.Linear(128, 1), |
| ) |
|
|
| def forward(self, x: torch.Tensor) -> torch.Tensor: |
| return self.network(x).squeeze(-1) |
|
|
| @torch.no_grad() |
| def predict_proba(self, x: torch.Tensor) -> torch.Tensor: |
| self.eval() |
| return torch.sigmoid(self.forward(x)) |