# Few-Shot Knowledge Graph Completion Model Based on Relation Learning
## 4 Model
### Matching Process Computation  
After being processed by the Transformer learner, each entity in the reference set and query set obtains its corresponding relation representation. Similar to previous methods [20], FRL-KGC calculates the semantic similarity between the query triplet and the reference set using a metric-based approach, and selects the triplet with the highest similarity score as the model's prediction. The main structure of the matching process computation is illustrated in Figure 6.  
Figure 5: The main structure of a learning framework composed of a Transformer and an LSTM.  
However, during the matching computation, each reference set contains different information [30], and it is important to dynamically learn the representation of the reference relation \(r\) for different query triplets. Inspired by the work in reference [31], FRL-KGC generates a dynamic query relation representation \(\mathbf{r_{m}}\) for each query entity pair. The computation process is shown in Equations (18) and (19):  
\[\mathbf{r_{m}}=\sum_{\mathbf{S_{r_{i}}}\in\mathcal{S_{r}}}\lambda_{i}\cdot\mathbf{S_{r_{i}}} \tag{18}\]  
\[\lambda_{i}=softmax(\cos(\mathbf{S_{r_{i}}},q_{r}))=\frac{\exp(\cos(\mathbf{S_{r_{i}}}, q_{r}))}{\sum_{\mathbf{S_{r_{i}}}\in\mathcal{S_{r}}}\exp(\cos(\mathbf{S_{r_{i}}},q_{r}))} \tag{19}\]  
where \(\lambda_{i}\) denotes the \(softmax(\cdot)\) attention weights for the representation of each reference relation; \(\mathbf{S_{r_{i}}}\) represents the relation representation of the \(i\)-\(th\) entity pair in the reference set obtained through the Transformer learner (Equation (18)); \(q_{r}\) represents the relation representation of the query entity pair obtained through the Transformer learner (Equation (18)); \(\cos(\cdot)\) represents the cosine similarity; and \(j\) represents the size of the reference set.  
Next, the dynamic query relation representation \(\mathbf{r_{m}}\) and the semantic similarity \(score(\mathbf{r_{m}},q_{r})\) between \(\mathbf{r_{m}}\) and the query \(q_{r}\) are computed as shown in Equation (20):  
\[score(\mathbf{r_{m}},q_{r})=\mathbf{r_{m}}\cdot q_{r} \tag{20}\]  
where the higher the value of \(score(\cdot)\), the greater the semantic similarity between the query entity pair \((h,t)\) under the reference relation \(r\) and the few-shot reference set, indicating a higher possibility of the query triplet being valid. Conversely, a lower \(score(\cdot)\) suggests a smaller possibility.  
However, in translational models like TransE [10], it is important to consider the translational property for the expectation of \(h+r\approx t\). According to Equation (18), \(\left(z_{h}^{3},z_{r}^{3},z_{t}^{3}\right)\) represents the output sequence of the query entity pair \(\left(h_{q},t_{q}\right)\) after passing through the Transformer learner. The translation score \(s\) is defined as shown in Equation (21):  
\[s(h_{q},t_{q})=\|z_{h}^{3}+z_{r}^{3}-z_{t}^{3}\|_{2} \tag{21}\]  
Figure 6: The schematic diagram of matching process calculation.  
where \(\left\|z_{i}^{3}\right\|_{2}\) represents the \(L_{2}\) norm of vector \(z_{i}^{3}\), and \(s\) represents the distance between \(z_{h}^{3}+z_{r}^{3}\) and \(z_{t}^{3}\). Therefore, the smaller the value of \(s\), the higher the possibility of the query triplet being valid.  
Therefore, considering both the values of \(\mathit{score}(\cdot)\) and \(s(\cdot)\), the calculation of the final matching \(\mathit{score}^{\prime}(\cdot)\) for the query entity pair \(\big{(}h_{q},t_{q}\big{)}\) is determined as shown in Equation (22):  
\[\mathit{score}^{\prime}\big{(}h_{q},t_{q}\big{)}=\mathit{score}(\mathbf{r_{m}},q _{r})-\delta s\big{(}h_{q},t_{q}\big{)} \tag{22}\]  
where \(\delta\) is an adjustment factor. The final \(\mathit{score}^{\prime}(\cdot)\) is calculated for all the query triplets, and sorted accordingly.