# A Chinese Grammatical Error Correction Method Based on Iterative Training and Sequence Tagging
## 3 Methodology
### Training with Joint Focal Loss  
Using softmax direct prediction gives the best tagging result for each token, while CRF is more concerned with the quality of the overall tagging result. To improve the capability of the model, direct prediction and CRF are combined for better results. In the grammatical error detector, there are only two tags in the tag set: correct and incorrect. The activity diagram of the loss function is shown in Figure 3. We employ the cross-entropy error as the loss function for direct prediction:  
\[dL_{\text{sm}}=\frac{1}{N}\sum_{i=1}^{N}-[t_{i}\cdot\log{(p_{i})}+(1-t_{i}) \cdot\log{(1-p_{i})}] \tag{7}\]  
where \(p_{i}\) is the probability that the token at position \(i\) is tagged as correct. In addition, \(t_{i}\) is the tag of the token at position \(i\), which has a value of \(1\) when the label is correct and has a value of \(0\) when the label is incorrect. The grammatical error corrector is similar to the detector, except that there are more tags in the corrector and we also use a cross-entropy loss function for direct prediction:  
\[cL_{\text{sm}}=\frac{1}{N}\sum_{i=1}^{N}\sum_{c=1}^{L}t_{ic}\log{(p_{ic})} \tag{8}\]  
Figure 2: Each point represents a label possibility, the line between the points indicates the association between the labels and each of the labeled outcomes corresponds to a complete path on the diagram.  
where \(L\) is the number of tags and \(t_{i}c\) is 1 if the true tag of the \(i\)-th token is equal to the \(c\)-th tag in the tag set and 0 otherwise. In addition, \(p_{ic}\) is the predicted probability that the \(i\)-th token is labeled as the \(c\)-th tag in the tag set.  
The grammatical error corrector and the detector use a similar objective function for the CRF model, both calculated via maximum likelihood estimation. The optimization objective is:  
\[dL_{\text{crf}}=-\log P_{\text{crf}}(T\mid H) \tag{9}\]  
\[cL_{\text{crf}}=-\log P^{{}^{\prime}}_{\text{crf}}(T\mid H) \tag{10}\]  
In the formulas above, \(P_{\text{crf}}\) and \(P^{{}^{\prime}}_{\text{crf}}\) are obtained through Equation (6). Then, the final loss function is:  
\[L=dL_{\text{sm}}+dL_{\text{crf}}+cL_{\text{sm}}+cL_{\text{crf}} \tag{11}\]  
Since the number of correct tokens in a sentence is much higher than the number of incorrect tokens, the error detector labels more correct tags than incorrect tags, and the error corrector labels far more KEEP tags than other tags. Category imbalance is essentially a reflection of the difference in classification difficulty. If a label is highly dominant in the training data, the model will prefer to use the highly dominant label during inference. In order to solve this category imbalance problem, this paper uses a focused penalty strategy [6] to ensure the model pays less attention to the more dominant labels. The loss function after using focal loss is:  
\[dL_{\text{sm}}^{\text{fl}}=-\frac{1}{N}\sum_{i=1}^{N}(1-p_{i})^{\gamma}[t_{i} \cdot\log{(p_{i})}+(1-t_{i})\cdot\log{(1-p_{i})}]\]  
\[cL_{\text{sm}}^{\text{fl}}=\frac{1}{N}\sum_{i=1}^{N}\sum_{c=1}^{L}(1-p_{i}c)^ {\gamma}t_{ic}\log{(p_{ic})}\]  
\[dL_{\text{crf}}^{\text{fl}}=-(1-P_{\text{crf}}(T\mid H))^{\gamma}\log{P_{ \text{crf}}}(T\mid H)\]  
\[cL_{\text{crf}}^{\text{fl}}=-\Big{(}1-P^{{}^{\prime}}_{\text{crf}}(T\mid H) \Big{)}^{\gamma}\log{P^{\prime}}_{\text{crf}}(T\mid H)\]  
\[L_{\text{sm}}^{\text{fl}}=dL_{\text{sm}}^{\text{fl}}+cL_{\text{sm}}^{\text{fl }}+dL_{\text{crf}}^{\text{fl}}+cL_{\text{crf}}^{\text{fl}}\]  
Figure 3: The activity diagram of the joint focal loss function.  
where \(\gamma\) is a hyperparameter that controls the weight of the penalty.