application_vnd.jupyter.stderr_output_2.png text_plain_output_3.png text_plain_output_1.png
import torch
import torch.nn as nn
from transformers import BertModel
class BertClassifier(nn.Module):
    def __init__(self, freeze_bert=False, version='mini'):
        super(BertClassifier, self).__init__()
        D_in = 256 if version == 'mini' else 758
        H = 50
        D_out = 2
        self.bert = AutoModel.from_pretrained('asafaya/bert-mini-arabic') if version == 'mini' else AutoModel.from_pretrained('asafaya/bert-base-arabic')
        self.classifier = nn.Sequential(nn.Linear(D_in, H), nn.ReLU(), nn.Dropout(0.5), nn.Linear(H, D_out))
        if freeze_bert:
            for param in self.bert.parameters():
                param.requires_grad = False
    def forward(self, input_ids, attention_mask):
        outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask)
        last_hidden_state_cls = outputs[0][:, 0, :]
        logits = self.classifier(last_hidden_state_cls)
        return logits