Views
No views yet
FacebookAI/roberta-large model, designed to detect and correct grammatical errors in English text. The model focuses on common grammatical mistakes such as verb tense, noun inflection, adjective usage, and more. It is particularly useful for language learners or applications requiring enhanced grammatical precision.FacebookAI/roberta-large1from dataclasses import dataclass
2from typing import Optional, Tuple
3
4import torch
5from torch import nn
6from torch.nn import CrossEntropyLoss
7from transformers import AutoConfig, AutoTokenizer
8from transformers.file_utils import ModelOutput
9from transformers.models.roberta.modeling_roberta import (
10 RobertaModel,
11 RobertaPreTrainedModel,
12)
13
14@dataclass
15class XGECToROutput(ModelOutput):
16 """
17 Output type of `XGECToRForTokenClassification.forward()`.
18 loss (`torch.FloatTensor`, optional)
19 logits_correction (`torch.FloatTensor`) : The correction logits for each token.
20 logits_detection (`torch.FloatTensor`) : The detection logits for each token.
21 hidden_states (`Tuple[torch.FloatTensor]`, optional)
22 attentions (`Tuple[torch.FloatTensor]`, optional)
23 """
24
25 loss: Optional[torch.FloatTensor] = None
26 logits_correction: torch.FloatTensor = None
27 logits_detection: torch.FloatTensor = None
28 hidden_states: Optional[Tuple[torch.FloatTensor]] = None
29 attentions: Optional[Tuple[torch.FloatTensor]] = None
30
31
32class XGECToRRoberta(RobertaPreTrainedModel):
33 """
34 This class overrides the GECToR model to include an error detection head in addition to the token classification head.
35 """
36
37 _keys_to_ignore_on_load_unexpected = [r"pooler"]
38 _keys_to_ignore_on_load_missing = [r"position_ids"]
39
40 def __init__(self, config):
41 super().__init__(config)
42 self.num_labels = config.num_labels
43 self.unk_tag_idx = config.label2id.get("@@UNKNOWN@@", None)
44
45 self.roberta = RobertaModel(config)
46
47 self.classifier = nn.Linear(config.hidden_size, config.num_labels)
48
49 if self.unk_tag_idx is not None:
50 self.error_detector = nn.Linear(config.hidden_size, 3)
51 else:
52 self.error_detector = nn.Linear(config.hidden_size, 2)
53
54 def forward(
55 self,
56 input_ids=None,
57 attention_mask=None,
58 token_type_ids=None,
59 position_ids=None,
60 inputs_embeds=None,
61 labels=None,
62 output_attentions=None,
63 output_hidden_states=None,
64 return_dict=None,
65 ):
66 r"""
67 labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
68 Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`.
69 """
70 return_dict = (
71 return_dict if return_dict is not None else self.config.use_return_dict
72 )
73
74 outputs = self.roberta(
75 input_ids,
76 attention_mask=attention_mask,
77 token_type_ids=token_type_ids,
78 position_ids=position_ids,
79 inputs_embeds=inputs_embeds,
80 output_attentions=output_attentions,
81 output_hidden_states=output_hidden_states,
82 return_dict=return_dict,
83 )
84
85 sequence_output = outputs[0]
86
87 logits_correction = self.classifier(sequence_output)
88 logits_detection = self.error_detector(sequence_output)
89
90 loss = None
91 if labels is not None:
92 loss_fct = CrossEntropyLoss()
93 loss = loss_fct(
94 logits_correction.view(-1, self.num_labels), labels.view(-1)
95 )
96
97 labels_detection = torch.ones_like(labels)
98 labels_detection[labels == 0] = 0
99 labels_detection[labels == -100] = -100 # ignore padding
100 if self.unk_tag_idx is not None:
101 labels_detection[labels == self.unk_tag_idx] = 2
102 loss_detection = loss_fct(
103 logits_detection.view(-1, 3), labels_detection.view(-1)
104 )
105 else:
106 loss_detection = loss_fct(
107 logits_detection.view(-1, 2), labels_detection.view(-1)
108 )
109
110 loss += loss_detection
111
112 if not return_dict:
113 output = (
114 logits_correction,
115 logits_detection,
116 ) + outputs[2:]
117 return ((loss,) + output) if loss is not None else output
118
119 return XGECToROutput(
120 loss=loss,
121 logits_correction=logits_correction,
122 logits_detection=logits_detection,
123 hidden_states=outputs.hidden_states,
124 attentions=outputs.attentions,
125 )
126
127 def get_input_embeddings(self):
128 return self.roberta.get_input_embeddings()
129
130 def set_input_embeddings(self, value):
131 self.roberta.set_input_embeddings(value)
132
133config = AutoConfig.from_pretrained("manred1997/roberta-large_lemon-spell_5k")
134tokenizer = AutoTokenizer.from_pretrained("manred1997/roberta-large_lemon-spell_5k")
135model = XGECToRRoberta.from_pretrained(
136 "manred1997/roberta-large_lemon-spell_5k", config=config
137)| Stage | Dataset(s) Used | Description |
|---|---|---|
| Stage 1 | Shuffled 9 million sentences from the PIE corpus (A1 part only) | 9 million shuffled sentences from the PIE corpus, focusing on A1-level sentences. |
| Stage 2 | Shuffled combination of NUCLE, FCE, Lang8, W&I + Locness datasets | Lang8 dataset contained 947,344 sentences, with 52.5% having different source and target sentences. |
| If using a newer Lang8 dump, consider sampling. | ||
| Stage 3 | Shuffled version of W&I + Locness datasets | Final shuffled version of the W&I + Locness datasets. |