d
Size: a a a
d
SD
SD
d
ДК
d
ИТ
text = 'I am a toxic AND grateful comment to the movie'
text = '[CLS] ' + text + ' [SEP']
ДК
E
text = 'I am a toxic AND grateful comment to the movie'
text = '[CLS] ' + text + ' [SEP']
ИТ
ИТ
E
E
ИТ
[CLS]
, который вы дообучили перед этимd
class TextClassifier(pl.LightningModule):
def __init__(self, hparams):
super().__init__()
self.hparams = hparams
self.loss_f = nn.BCEWithLogitsLoss()
self.class_names = hparams.class_names
self.bert = AutoModel.from_pretrained(hparams.bert_model)
self.decoder = nn.Sequential(
nn.Dropout(hparams.dropout_rate),
nn.Linear(hparams.hidden_size * 2, hparams.num_classes)
)
def forward(self, input_ids, attention_mask):
h, _ = self.bert(input_ids=input_ids,
attention_mask=attention_mask,
output_attentions=None)
h_max = torch.max(h[:, 1:], dim=1).values
h_avg = torch.mean(h[:, 1:], dim=1)
# h_cls = h[:, 0]
encoded = torch.cat([h_max, h_avg], dim=1)
# encoded_dropped = self.dropout(encoded)
logits = self.decoder(encoded).squeeze()
return logits
def training_step(self, batch, batch_idx):
logits = self.forward(
batch['input_ids'],
batch['attention_mask'],
)
loss = self.loss_f(logits, batch['labels'])
self.log('train_loss', loss, on_step=True, on_epoch=False, prog_bar=True, logger=True)
return loss
def configure_optimizers(self):
no_decay = ['bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in self.bert.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
{'params': [p for n, p in self.bert.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0},
{'params': self.decoder.parameters()}
]
opt = AdamW(
optimizer_grouped_parameters,
lr=self.hparams.learning_rate
)
return opt
d
d
d
E
E