## 3 Methodology
### Problem Definition
#### 3.1.6 Adaptive Weighting  
We design an adaptive weighting method to balance the character prediction and pinyin prediction tasks during training, as well as the balance of consonant, rhyme, and tone prediction within pinyin prediction. Given an input sentence X, the character prediction task aims to match the predicted character \(\{g_{i}\}_{i=1}^{n}\) with the truth value \(\{y_{i}\}_{i=1}^{n}\), while the pinyin prediction task aims to match the predicted fine-grained character \(\{(\hat{\alpha}_{i},\hat{\beta}_{i},\hat{\gamma}_{i})\}_{i=1}^{n}\) with the ground truth value \(\{(\alpha_{i},\beta_{i},\gamma_{i})\}_{i=1}^{n}\) match. Where the character prediction task loss function is defined as:  
\[L_{i}^{c}=-logp(\hat{g}_{i}=y_{i}\mid X) \tag{9}\]  
Then, as we discussed in the previous introduction, the phonetic prediction task may provide different levels of benefit with different input characters. The more similar the pronunciation of the input and target characters, the more likely it is that spelling errors caused by phonetic similarity will occur. In this case, the pinyin prediction task may provide greater benefits and should be assigned greater weights. To compute such adaptive weights, we feed the target correct sentence \(Y\) to the encoder and subsequent pronunciation-specific projection layers. Then, we compute the cosine similarity \(cos(h_{x_{i}}^{p},h_{y_{i}}^{p})\) for each input character \(x_{i}\) and its target character \(y_{i}\) based on their pronunciation-specific feature representations \(h_{x_{i}}^{p},h_{y_{i}}^{p}\), and accordingly define the adaptive weights at the i position as:  
\[w_{i}^{p}=sin((\frac{cos(h_{x_{i}}^{p},h_{y_{i}}^{p})+1}{2})^{2}) \tag{10}\]  
The higher the cosine similarity, \(cos(h_{x_{i}}^{p},h_{y_{i}}^{p})\), the greater the weight \(w_{i}\) will be. Similarly, for the pinyin prediction task internally, consonants, rhymes, and tones provide different levels of benefit for different input characters. However, empirically, tones should be weaker than the previous two in comparison to consonants and rhymes, so their weight values should be smaller than the previous two when weighting. Therefore, the fine-grained pinyin prediction task loss function is defined as follows:  
\[L_{i}^{p}=-k_{1}w_{a}logp(\hat{x}_{i}=\alpha_{i}|X)-k_{2}w_{\beta}logp(\hat{ \beta}_{i}=\beta_{i}|X)-k_{3}w_{\gamma}logp(\hat{\gamma}_{i}=\gamma_{i}|X) \tag{11}\]  
where, \(w_{a}\), \(w_{\beta}\), \(w_{\gamma}\) re the weights of consonants, rhymes, and tones computed based on cosine similarity, and \(k_{1},k_{2},k_{3}\) hyperparameters. Finally, the total loss function is defined as the sum of the character prediction loss and the pinyin prediction loss, as well as the self-distillation loss:  
\[L=\frac{1}{n}\sum_{i=1}^{n}{(w_{i}^{c}L_{i}^{c}+w_{i}^{p}L_{i}^{p}+L_{H})} \tag{12}\]  
where \(w_{i}^{c}\) is the character prediction, and the target correct sentence \(Y\) is computed based on the cosine similarity. Two points are worth noting here: (1) The branch that encodes and maps the target sentence \(Y\) is used to compute the adaptive weights only in the forward passes, while it will be separated in the backward passes. (2) The pinyin prediction, the self-distillation module, and the adaptive weighting scheme are introduced only during training. During inference, we use the character prediction decoder alone for prediction.