# Few-Shot Knowledge Graph Completion Model Based on Relation Learning
## 4 Model
### Entity Encoder Based on Gate Mechanism  
In knowledge graphs, heterogeneous neighbors of entities have different impacts on the representation of the entities. Adaptively aggregating neighboring information according to the task relation \(r\) can improve the quality of the central entity's representation [18]. However, this method does not aggregate higher-order neighborhood entity information into the central entity and overlooks the effect of the ratio between useful neighbors and useless neighbors on the encoding of the central entity. Based on this, we designed a gated higher-order neighborhood entity encoder that extends the \"adaptive neighborhood encoder\" of the FAAN model and expands it to higher-order neighborhoods with the addition of a gating mechanism. This approach enhances the expressiveness of entities while reducing the impact of noisy neighbors on the updating of central entity encoding. The main structure of the gated higher-order neighborhood entity encoder is shown in Figure 3.  
Figure 1: An example of a five-shot KGC task. Given a task triplet \((h,r,t)\), assuming entity \(h\) is the target entity, its encoding is updated through the higher-order neighborhood entity encoder. The higher-order neighborhood of entity \(h\) is defined as \(\mathcal{N}_{h}^{o}=\big{\{}(r_{i}^{o},t_{i}^{o})\big{|}\big{(}h,r_{i}^{o},t_{i}^ {o})\in\mathcal{G}^{\prime}\big{\}}\), where \(\mathcal{G}^{\prime}\) is the background knowledge graph, and \(r_{i}^{o}\) and \(t_{i}^{o}\) represent the \(i\)-\(h\) higher-order neighbor relation and the corresponding tail entity of entity \(h\), respectively. To quantify the features of entity \(h\), we first use the metric function \(\varphi^{o}(\cdot)\) to calculate the similarity between the reference relation \(r\) and the adjacent higher order relation \(r_{i}^{o}\) of entity \(h\), as shown in Equation (1). \[\varphi^{o}\big{(}r,r_{i}^{o}\big{)}=r^{\textbf{T}}\big{(}Wr_{i}^{o}+b\big{)} \tag{1}\]  
Figure 3: The main structure of the high-order neighborhood entity encoder based on the gating mechanism. Figure 2: Overview of the FRL-KGC framework. where \(\mathbf{r}\) represents the initial feature of task relation \(r\); \(\mathbf{r_{i}^{o}}\) represents the pre-trained embedding of higher-order adjacent relation \(\mathbf{r_{i}^{o}}\in\mathcal{N}_{h}^{o}\); \(\mathbf{W}\in\mathbb{R}^{d\times d}\) and \(\mathbf{b}\in\mathbb{R}^{d\times 1}\) are the weight matrix and bias parameters, respectively; and \(\varphi^{o}(\cdot)\) is the bilinear dot product function. Next, based on the similarity score \(\varphi^{o}(\cdot)\) of the higher-order neighborhood encoding, the attention mechanism is employed to assign higher attention scores to those entities \(t_{i}^{o}\) with higher scores. To further improve the representation quality of entity \(h\) and reduce the impact of noisy neighbors, higher weights are allocated to more important neighbors. The \"Gating Mechanism\" is introduced in the process of calculating neighbor weights to adaptively compute weight \(\alpha_{i}\), as shown in Equations (2) and (3):  
\[G\big{(}h^{o},e_{i}^{o}\big{)}=\sum\limits_{o=1}^{n}\big{(}h^{o}\cdot e_{i}^{ o}\big{)}\cdot\mathbf{r_{i}^{o}} \tag{2}\]  
\[\alpha_{i}=softmax\big{(}G\big{(}h^{o},e_{i}^{o}\big{)}\big{)} \tag{3}\]  
The gating mechanism function \(G(\cdot)\) calculates the inner product of the central entity and neighboring entities, performs matrix multiplication using the task relation \(\mathbf{r_{i}^{o}}\), and sums the results. Here, \(n\) is the maximum neighboring order between the central entity \(h^{o}\) and neighboring entity \(e_{i}^{o}\), and \(o\) represents the order number. It takes the central entity \(h^{o}\) and related neighboring entities \(e_{i}^{o}\) as inputs, and outputs a metric representing the relevance between the neighborhood and the central entity. \(softmax(\cdot)\) converts the input into a probability distribution of neighbors.