Knowledge graph embedding method and device based on multi-scale hole Transform
Through the multi-scale dilated Transformer architecture, combined with dilated convolution and structure-aware self-attention mechanism, it solves the problem of insufficient local feature extraction in knowledge graph embedding by the existing Transformer method, and improves the accuracy and feature learning ability of knowledge graph embedding.
Patent Information
- Application Number
- CN202410857124.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-28
- Publication Date
- 2024-10-01
AI Technical Summary
The existing Transformer-based knowledge graph embedding method ignores the connection information between entities and relationships in the process of converting the graph into a sequence as input, and the standard multi-head self-attention mechanism cannot fully extract the local features in the subgraph. This leads to insufficient feature learning ability.
It adopts the multi-scale hole Transformer architecture, learns local and global feature information by fusing the hole convolution and structure-aware self-attention mechanism, enhances the feature interaction in the multi-head attention calculation process, and builds a knowledge graph embedding method based on the multi-scale hole Transformer.
Effectively modeling the structural characteristics of the knowledge graph improves the feature learning ability and embedding accuracy of the model, and improves the execution performance of tasks such as link prediction and knowledge graph completion.
Smart Images

Figure CN118733788A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of computer storage technology, relates to the field of knowledge graph embedding, and relates to a method based on multi-scale hole Transforme r A knowledge graph embedding method and device. Background Art
[0002] Knowledge graph embedding methods can be used for a variety of tasks, such as link prediction, knowledge graph completion, and other tasks. Knowledge graph completion is a task applied to knowledge graphs. Its goal is to predict missing entities and relationships based on existing entities and relationships in the knowledge graph and then complete the knowledge graph. It has received widespread attention and research in academia. For example, in the field of network security, cyber attacks not only affect social stability but also national security. However, the number of qualified network security personnel is small, so the integration of network security knowledge graphs with attack and defense technologies is an inevitable trend. Network security knowledge graphs provide an intuitive modeling method for various attack and defense scenarios in the real world. The network security model based on knowledge graph embedding learning can capture the hidden relationships between entities in the knowledge graph, namely, attack names, vulnerability names, product names, and manufacturers. According to the vulnerabilities associated with a certain attack, a high-precision model can accurately infer a certain attack name. When the relationship is defense, what are the defense measures that can form the correct triples? This narrows the scope of testing and greatly reduces the workload of network security personnel. At the same time, it can imitate the thinking process of network security experts to obtain new knowledge, that is, the corresponding entities and relationships in the knowledge graph.
[0003] Knowledge graph is a basic technology of artificial intelligence technology, which represents a semantic network that describes the relationship between entities. Usually, the data in the knowledge graph is stored in the form of triples (h, r, t), indicating that there is a relationship r between the head entity h and the tail entity t. Knowledge graph is used to extract knowledge from massive data and has been widely used in information retrieval, semantic analysis, recommendation system and other fields. Although the knowledge graph already contains a large amount of data, existing knowledge graphs such as Freebas e , WordNet and YAGO are still incomplete; at the same time, most existing knowledge graphs are stored in symbolic and logical forms, while most knowledge graph-based systems require numerical calculations in continuous space. Knowledge graph embedding aims to project entities and relationships into continuous low-dimensional vector space to obtain effective embedding representations and use these embedding representations to predict missing entities or relationships in the knowledge graph.
[0004] According to the different inputs, knowledge graph embedding methods are mainly divided into two categories: one is the triple-based method, and the other is the method based on structural information. The triple-based method uses a single triple to learn the interactive information between entities and relations, which includes three subcategories: translation-based methods, semantic matching-based methods, and convolutional neural network-based methods. Among them, the translation-based method regards the relationship as a linear translation operation from the head entity to the tail entity, so that the vector representation of h and r is consistent with the vector representation of t, satisfying h+r≈t. The paper "Knowledge Graph Embedding by Translating on Hyperplanes" published at the 28th AAAI Artificial Intelligence Conference in 2014 models the relationship in the hyperplane and performs translation operations, which can effectively model reflexive, one-to-many, many-to-one and many-to-many relationships. The semantic matching method measures the possibility of a triple by evaluating the similarity of entities and relations in a low-dimensional vector space. The paper "A three-way model for collective learning on multi-relational data" published at the 28th International Conference on Machine Learning in 2011 marked the birth of the semantic matching model. It uses matrices to model relations and decomposes the matrices to generate low-dimensional embeddings for entities and relations. The convolutional neural network-based method uses multi-layer nonlinear features to predict the connection between entities and relations and generate more expressive feature representations. The paper "Convolutional 2D knowledgegraph embeddings" published at the top international artificial intelligence conference AAAI in 2018 first used convolutional neural networks to implement knowledge graph embedding. The vectors of the head entity and the relationship are stacked and reshaped into a two-dimensional tensor as the input of the convolution layer, and the output of the convolution layer is projected into the k-dimensional space using a fully connected layer to match the embedding of the candidate tail entity, and then the score of the triple is calculated. Although the above methods have achieved good performance, they all ignore the rich structural information of the knowledge graph. The method based on structural information can make full use of the structural features of the knowledge graph to obtain more accurate prediction results. Among them, the graph neural network-based method uses the message passing mechanism to iteratively update entity embedding. The paper "Modeling Relational Data with Graph Convolutional Networks" published at the 2018 European Semantic Web Conference aggregates the features of neighboring nodes by weighted summation, proving the practicality of structural information in knowledge graph embedding, marking the application of graph convolutional neural networks in knowledge graph embedding.Although graph neural network-based models have made great progress, their use of standard information transfer methods for the entire knowledge graph can lead to over-smoothing. In contrast, Transformer-based methods use Transformers to more effectively capture the interaction information between entity-relationships and entity-context. The paper "Hitter: Hierarchical transformers for knowledge graph embeddings" published at the top international artificial intelligence conference EMNLP in 2020 treats both entities and relationships as plain text tags to convert triples into sequences, and designs a hierarchical architecture for structural information, in which the bottom-level modules generate entity embeddings that depend on relationships, while the upper-level modules obtain contextual information from the subgraph structure to capture global information.
[0005] Although the above-mentioned Transformer-based methods have greatly improved the performance of knowledge graph embedding with their powerful feature extraction capabilities, the current Transformer-based models still have problems: ① In the process of converting graphs into sequences as Transformer input, the existing models treat entities and relationships as plain text tags, ignoring the connection information between entities and relationships; ② Due to the natural limitations of the Transformer itself, the standard multi-head self-attention mechanism is conducive to extracting global feature information, but cannot fully extract local features between nodes in the subgraph; ③ In the calculation process of the multi-head attention mechanism, there is no connection between the attention heads, and they are independent of each other, which is not conducive to model expression. Therefore, the feature learning ability of the Transformer-based knowledge graph embedding method is still insufficient.
[0006] Therefore, there is an urgent need for a method that can improve the accuracy of knowledge graph embedding to ensure the execution of tasks such as link prediction and knowledge graph completion. Summary of the invention
[0007] In view of this, the purpose of the present invention is to provide a knowledge graph embedding method and device based on a multi-scale hole Transformer to improve the performance of knowledge graph embedding.
[0008] In order to achieve the above object, the present invention provides the following technical solutions:
[0009] Scenario 1:
[0010] A knowledge graph embedding method based on multi-scale void Transformer is defined, where the knowledge graph G = {V, R, ε}, V is an entity set, R is a relationship set, ε is a triple set, and triple T = (h, r, t) ∈ ε, indicating that there is a certain relationship r ∈ R between the head entity h ∈ V and the tail entity t ∈ V, so that the fact is true. The method specifically includes the following steps:
[0011] S1: Sample collection phase: Input the knowledge graph dataset and use the random walk-based sampling method to sample the central triple T C The neighbor nodes are sampled and the sampling result T is obtained G ;
[0012] S2: Vector fusion stage: Subgraph T G Converted into a sequence and encoded to get input X = {x1, x2, ..., x n}∈R n×d , add the type vector t to each element in X e , t r ∈R d Used to distinguish the type of elements in the sequence and use linear projection to map X to query (Q), key (K), value (V);
[0013] S3: Model construction and training phase: Construct a deep learning model based on multi-scale dilated Transformer. This model is a two-stage Transformer architecture. In the first stage, multi-scale dilated attention fused with dilated convolution is used to learn complex local feature information. In the second stage, the multi-head structure fused with subgraph adjacency matrix is used to perceive attention, ensuring that the attention calculation score and subgraph structure are highly consistent, learning global information, and obtaining the encoding result of Transformer. m , O m Contains local and global feature information;
[0014] S4: Link prediction stage: Convert the Transformer-encoded feature vector Om into the score of the triple (h, r, t).
[0015] Further, step S2 specifically includes: for a given central triplet T C After sampling in step S1, T G After that, T G The relations in the graph are converted into relational nodes, and the entire graph becomes a directed graph without edge attributes. By converting the graph into a sequence and embedding the sequence, we can get the input X = {x1, x2, ..., x n}∈R n×d , where x i ∈R d, n and d are the number of elements in the sequence and the embedding dimension respectively; then add the type vector t to each element in X e , t r ∈R d To distinguish the type of elements in the sequence, where t e , t r Represent entities and relations respectively; project X into different linear spaces Get the corresponding query (Q), key (K), value (V) as the input of the attention layer, where d Q =d K =d V =d / N, where N represents the number of attention heads in multi-head attention.
[0016] Further, step S3 specifically includes the following steps:
[0017] S31: Integrating the dilated convolution (DConv) into the attention mechanism to construct the dilated attention mechanism (DA) as the first stage in the Transformer, all nodes in the input sequence can fully interact to obtain complex local information; the dilated convolution introduces the parameter "dilated rate" to modify the convolution kernel size when processing data, which can obtain a larger receptive field under the same parameters and computational complexity; in the attention calculation process, the matrix Q, K, V formed by the projection of the input X is used as the input of the attention module, and the result of K after dilated convolution is matrix multiplied with Q, and the adjacency matrix of the sampled subgraph is added to the calculation process as an attention bias item. After the Softmax operation, it is matrix multiplied with V after dilated convolution to obtain the output result of the dilated attention. The specific calculation process is as follows:
[0018] Q=XW Q , K = XW K , V = XW V
[0019]
[0020] Among them, DA represents the hole attention mechanism, l represents the hole rate used by the hole convolution, Θ represents the adjacency matrix of the sampled subgraph, and DConv lRepresents a dilated convolution with a dilated rate of ,; C1D represents a one-dimensional convolution, which is used to enhance the feature interaction in the multi-head attention calculation process. By inserting C1D before and after the Softmax operation, the low-rank distribution calculated by each attention head is superimposed to enhance the expression ability of the attention mechanism. In order to utilize the sparsity of the self-attention mechanism at different scales, a parallel strategy is adopted to set different dilated rates on different attention heads to execute the dilated attention mechanism (DA) to construct a multi-scale dilated attention mechanism (MSDA), learn feature information at different scales, fuse the outputs of different attention heads, and use a linear layer to aggregate these feature information to obtain a multi-scale feature vector Y. The feature vector is generated according to the following formula:
[0021] Y=MSDA(Q,K,V)=Linear(Concat[h1,...,h N ])
[0022] h i =DA(Q i , K i , V i , l i )
[0023] Among them, MSDA represents multi-scale hole attention mechanism, DA represents hole attention mechanism; Q i , K i , V i They represent the vector segments input to the i-th attention head respectively; Y is the feature vector obtained by using multi-scale hole attention in Transformer, which fully learns the local interaction information between each node in the subgraph;
[0024] S32: Based on the standard self-attention, the adjacency matrix of the subgraph is integrated to construct the structure-aware self-attention mechanism (SASA) as the second stage in the Transformer; the standard self-attention mechanism can effectively capture the global information in the input sequence and obtain the weight of each element. In the calculation process of the standard multi-head self-attention mechanism, the adjacency matrix of the subgraph is added as the attention bias item, which can provide the connection information between the nodes in the subgraph and ensure that the attention weight is consistent with the subgraph structure. The specific calculation process is as follows:
[0025] Q=YW Q , K=YW K , V=YW V
[0026]
[0027] Among them, SASA represents the structure-aware self-attention mechanism; C1D represents one-dimensional convolution, Θ∈R n×nRepresents the adjacency matrix of the sampled subgraph. If two nodes are connected, the value of the corresponding position in the matrix is 1, otherwise it is 0. In order to obtain information from different representation subspaces at different positions, SASA is performed in parallel in multiple attention heads, and then the outputs of these attention heads are connected in series and aggregated through linear projection to form a multi-head structure-aware self-attention mechanism (MHSA) to obtain the final output result Z, which is generated according to the following formula:
[0028] Z=MHSA(Q,K,V)=linear(concat[h1,h2,,..,h N ])
[0029] h i =SASA(Q i , K i , V i )
[0030] Among them, MHSA represents the multi-head structure-aware self-attention mechanism; Q i , K i , V i They respectively represent the input of the i-th attention head; the feature vector Z contains not only the local information obtained by the multi-scale void attention stage, but also the global feature information obtained by the multi-head structure-aware attention.
[0031] Further, step S4 specifically includes: obtaining a feature vector O after Transformer encoding m , which contains all the feature information of the head entity, the relationship, and the context, by using the bidirectional inner product calculation O m and the embedding of the tail entity to obtain the score of the triple:
[0032]
[0033] in, Yes m The transpose representation of t Embedding vector representing the tail entity;
[0034] Then use the nonlinear logic sigmod function to get the probability distribution of whether all candidate tail entities are correct:
[0035] p(t)=sigmod(φ(h,r,t))
[0036] During the model training process, in order to ensure that the correct triplet can obtain a higher score than other triplets, the binary cross entropy (BCE) loss is used to optimize the model. The specific calculation process of the loss function L is as follows:
[0037]
[0038] Among them, t i is a label, if t i If it is the correct tail entity, its corresponding value is 1, otherwise it is 0; |E| is the number of entities in the knowledge graph; the loss function takes (h, r) pairs and scores all candidate tail entities at the same time. The higher the score, the greater the possibility that the corresponding triple is established.
[0039] Scenario 2:
[0040] A knowledge graph embedding device based on a multi-scale hole Transformer, comprising:
[0041] The sample collection module is used to build an index for all entities and relationships in the knowledge graph, and use a random walk-based sampling method to obtain neighbor triplets of the central triplet;
[0042] The vector fusion module is used to encode entities and relations to obtain the embedding vectors of entities and relations, and use the embedding vectors of adjacent entities and relations as the context of the entity vector to obtain the fused embedding vector;
[0043] A model training module is used to train the initial knowledge graph embedding model based on the fused embedding vector to obtain a trained knowledge graph embedding model, wherein the initial knowledge graph embedding model is a deep learning model based on a multi-scale hole Transformer constructed in Scheme 1;
[0044] The link prediction module is used to combine entities and relations in the knowledge graph to obtain (h, r) pairs, and use them as inputs of the trained knowledge graph embedding model to obtain prediction results.
[0045] Furthermore, the sample collection module includes:
[0046] The index generation unit is used to generate indexes for all entities and relationships in the original knowledge graph;
[0047] Subgraph sampling unit, used to convert the central triplet T C As input, a random walk based sampling method is used to sample T C The neighbor nodes are sampled and the sampling result T is obtained G =T C ∪E context ∪R context , where E context and R context Respectively represent T C Adjacent entities and relations, taking the sampling results as sample data.
[0048] Furthermore, the vector fusion module includes:
[0049] A subgraph conversion unit is used to convert the relations in the sampling results into relation nodes, so that the entire sampling subgraph becomes a directed subgraph without edge attributes, and the directed subgraph is converted into a sequence;
[0050] The vector fusion unit is used to embed the sequence and add a type vector to each element in the sequence to distinguish whether the element is an entity or a relationship.
[0051] Furthermore, the model training module includes:
[0052] Multi-scale dilated attention unit, used to introduce dilated convolution in attention, and utilize the sparsity of self-attention mechanism at different scales. It adopts parallel strategy to set different dilated rates on different attention heads to perform dilated attention to construct multi-scale dilated attention, learn feature information of different scales, fuse the outputs of different attention heads and use linear layers to aggregate these feature information to obtain multi-scale feature vectors.
[0053] The multi-head structure-aware attention unit is used to add the adjacency matrix of the subgraph as an attention bias item in the calculation process of the standard multi-head self-attention mechanism, provide the connection information between the nodes in the subgraph, ensure that the attention calculation score and the subgraph structure are highly consistent, and obtain the encoding result after the Transformer;
[0054] The decoding unit is used to perform a bidirectional inner product calculation on the encoding result and the candidate entity embedding vector matrix to obtain the score of each candidate entity and output the prediction result;
[0055] The model optimization unit is used to calculate the loss function using binary cross entropy and optimize the model parameters through back propagation to obtain a trained knowledge graph embedding model.
[0056] The beneficial effect of the present invention is that the present invention obtains its context information by sampling the adjacent nodes of the central triplet, and on this basis, uses the independently constructed multi-scale void transformer (MSDT) to model the sampling results to learn the local information between nodes and the global structural information, which can effectively model the structural characteristics of the knowledge graph, solve the problem of insufficient local information extraction, enhance the feature interaction between the attention heads in the multi-head attention calculation process, and improve the feature learning ability of the model. The present invention can improve the embedding accuracy while ensuring that the number of parameters is not significantly increased, thereby ensuring the execution of tasks such as link prediction and knowledge graph completion, and has a high application value.
[0057] Other advantages, objectives and features of the present invention will be described in the following description to some extent, and to some extent, will be obvious to those skilled in the art based on the following examination and study, or can be taught from the practice of the present invention. The objectives and other advantages of the present invention can be realized and obtained through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0058] In order to make the purpose, technical solutions and advantages of the present invention more clear, the present invention will be described in detail below in conjunction with the accompanying drawings, wherein:
[0059] Figure 1 It is a schematic diagram of the knowledge graph embedding framework MSDT of the present invention;
[0060] Figure 2 It is a structural schematic diagram of an embodiment of a knowledge graph embedding device based on a multi-scale hole Transformer of the present invention. DETAILED DESCRIPTION
[0061] The following describes the embodiments of the present invention by specific examples, and those skilled in the art can easily understand other advantages and effects of the present invention from the contents disclosed in this specification. The present invention can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that the illustrations provided in the following embodiments only illustrate the basic concept of the present invention in a schematic manner, and the following embodiments and features in the embodiments can be combined with each other without conflict.
[0062] Among them, the drawings are only used for illustrative explanations, and they only represent schematic diagrams rather than actual pictures, and should not be understood as limitations on the present invention. In order to better illustrate the embodiments of the present invention, some parts of the drawings may be omitted, enlarged or reduced, and do not represent the size of actual products. For those skilled in the art, it is understandable that some well-known structures and their descriptions in the drawings may be omitted.
[0063] The same or similar numbers in the drawings of the embodiments of the present invention correspond to the same or similar parts; in the description of the present invention, it should be understood that if the terms "upper", "lower", "left", "right", "front", "rear", etc. indicate the orientation or position relationship, they are based on the orientation or position relationship shown in the drawings, which is only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operate in a specific orientation. Therefore, the terms describing the position relationship in the drawings are only used for illustrative purposes and cannot be understood as limiting the present invention. For ordinary technicians in this field, the specific meanings of the above terms can be understood according to specific circumstances.
[0064] See also Figure 1-2 , the present invention provides a knowledge graph embedding method based on multi-scale void Transformer, such as Figure 1 As shown, the specific steps are as follows:
[0065] S1: Input the knowledge graph dataset and use random walk sampling to sample the central triple T C The neighbor nodes are sampled and the sampling result T is obtained G =T C ∪E context ∪R context , E context and R context Respectively represent T C Adjacent entities and relationships;
[0066] S2: For a given triple T C After sampling in step S1, T G After that, T G The relations in the graph are converted into relational nodes, and the entire graph becomes a directed graph without edge attributes. By converting the graph into a sequence and embedding the sequence, we can get the input X = {x1, x2, ..., x n}∈R n×d , where x i ∈R d , n and d are the number of elements in the sequence and the embedding dimension respectively. Then add the type vector t to each element in X e , t r ∈R d To distinguish the type of elements in the sequence, where t e , t r Represent entities and relations respectively. Project X into different linear spaces The corresponding query (Q), key (K), and value (V) are used as the input of the Transformer layer, where d Q =d K =d V =d / N, where N represents the number of attention heads in multi-head attention.
[0067] S3: Design a two-stage Transformer architecture. In the first stage, multi-scale dilated attention fused with dilated convolution is used to learn complex local feature information. In the second stage, a multi-head structure fused with subgraph adjacency matrix is used to perceive attention, ensuring that the attention calculation score and subgraph structure are highly consistent, learning global information, and obtaining the encoding result of Transformer O. m , O m Contains local and global feature information;
[0068] S31: Integrating dilated convolutions (DCon) into the attention mechanism v ) Construct a dilated attention mechanism (DA) as the first stage in the Transformer so that all nodes in the input sequence can fully interact to obtain complex local information; the dilated convolution introduces the parameter "dilated rate" to modify the convolution kernel size when processing data, which can obtain a larger receptive field under the same parameters and calculation amount; in the attention calculation process, the matrix Q, K, V formed by the projection of the input X is used as the input of the attention module, and the result of K after dilated convolution is matrix multiplied with Q, and the adjacency matrix of the sampled subgraph is added to the calculation process as an attention bias item. After the Softmax operation, it is matrix multiplied with V after dilated convolution to obtain the output result of the dilated attention. The specific calculation process is as follows:
[0069] Q=XW Q , K = XW K , V = XW V
[0070]
[0071] Among them, l represents the hole rate used by the hole convolution, Θ represents the adjacency matrix of the sampled subgraph, and DConv l Represents the hole rate of the hole convolution, C1D represents the one-dimensional convolution, which can enhance the feature interaction in the multi-head attention calculation process. By inserting C1D before and after the Softmax operation, the low-rank distribution calculated by each attention head is superimposed to enhance the expression ability of the attention mechanism. In order to utilize the sparsity of the self-attention mechanism at different scales, a parallel strategy is adopted to set different hole rates on different attention heads to perform DA to construct a multi-scale hole attention mechanism (MSDA), learn feature information of different scales, fuse the outputs of different attention heads, and use linear layers to aggregate these feature information to obtain a multi-scale feature vector Y. The feature vector is generated according to the following formula:
[0072] Y=MSDA(Q,K,V)=Linear(Concat[h1,...,h N ])
[0073] h i =DA(Q i , K i , V i , l i )
[0074] Among them, Q i , K i , V i, respectively represent the vector fragments input to the i-th attention head; Y is the feature vector obtained by using multi-scale hole attention in Transformer, which fully learns the local interaction information between each node in the subgraph;
[0075] S32: Based on the standard self-attention, the adjacency matrix of the subgraph is integrated to construct the structure-aware self-attention mechanism (SASA) as the second stage in the Transformer; the standard self-attention mechanism can effectively capture the global information in the input sequence and obtain the weight of each element. In the calculation process of the standard multi-head self-attention mechanism, the adjacency matrix of the subgraph is added as the attention bias item, which can provide the connection information between the nodes in the subgraph and ensure that the attention weight is consistent with the subgraph structure. The specific calculation process is as follows:
[0076] Q=YW Q , K=YW K , V=YW V
[0077]
[0078] Among them, C1D represents one-dimensional convolution, Θ∈R n×n Represents the adjacency matrix of the sampled subgraph. If two nodes are connected, the value of the corresponding position in the matrix is 1, otherwise it is 0. In order to obtain information from different representation subspaces at different positions, SASA is performed in parallel in multiple attention heads, and then the outputs of these attention heads are connected in series and aggregated through linear projection to form a multi-head structure-aware self-attention mechanism (MHSA) to obtain the final output result Z. The feature vector is generated according to the following formula:
[0079] Z=MHSA(Q,K,V)=linear(concat[h1,h2,...,h N ])
[0080] h i =SASA(Q i , K i , V i )
[0081] Among them, Q i , K i , V i They represent the input of the i-th attention head respectively; the feature vector Z contains not only the local information obtained by the multi-scale hole attention stage, but also the global feature information obtained by the multi-head structure-aware attention;
[0082] S4: After Transformer encoding, the feature vector O is obtained m, which contains all the feature information of the head entity, the relationship, and the context, by using the bidirectional inner product calculation O m and the embedding of the tail entity to obtain the score of the triple:
[0083]
[0084] in, is the transposed representation of Om, e t Represents the embedding vector of the tail entity. Then use the nonlinear logistic sigmod function to get the probability distribution of whether all candidate tail entities are correct:
[0085] p(t)=sigmod(φ(h,r,t))
[0086] During the model training process, in order to ensure that the correct triplet can obtain a higher score than other triplets, the binary cross entropy (BCE) loss is used to optimize the model. The specific calculation process of the loss function L is as follows:
[0087]
[0088] Among them, t is a label vector, the element value is 1 when the relationship exists, otherwise it is 0; |E| is the number of entities in the knowledge graph; the loss function takes (h, r) pairs and scores all candidate entities at the same time. The higher the score, the more likely the triple is to be established.
[0089] Figure 2 The principle block diagram of the knowledge graph embedding device based on multi-scale void transformer corresponding to the above-mentioned knowledge graph embedding method based on multi-scale void transformer is shown. Figure 2 As shown in FIG, the knowledge graph embedding device based on the multi-scale hole transformer includes a sample collection module, a vector fusion module, a model training module and a link prediction module. The detailed description of each module is as follows:
[0090] The sample collection module is used to build an index for all entities and relationships in the knowledge graph, and use a random walk-based sampling method to sample and obtain neighbor triplets of the central triplet.
[0091] The vector fusion module is used to encode entities and relations to obtain embedding vectors of entities and relations, and use the embedding vectors of adjacent entities and relations as the context of the entity vector to obtain a fused embedding vector.
[0092] The model training module is used to train the initial knowledge graph embedding model based on the fused embedding vector to obtain a trained knowledge graph embedding model, wherein the initial knowledge graph embedding model is a deep learning model based on a multi-scale void Transformer.
[0093] The link prediction module is used to combine entities and relationships in the knowledge graph to obtain (h, r) pairs, and use them as inputs of the trained knowledge graph embedding model to perform predictions to obtain prediction results.
[0094] As a preferred embodiment, the sample collection module includes:
[0095] The index generation unit is used to generate indexes for all entities and relationships in the original knowledge graph.
[0096] Subgraph sampling unit, used to convert the central triplet T C As input, a random walk based sampling method is used to sample T C The neighbor nodes are sampled and the sampling result T is obtained G =T C ∪E context ∪R context , E context and R context Respectively represent T C Adjacent entities and relations, taking the sampling results as sample data.
[0097] As a preferred embodiment, the vector fusion module includes:
[0098] The subgraph conversion unit is used to convert the relationship in the sampling result into a relationship node, so that the entire sampling subgraph becomes a directed graph without edge attributes, and convert the directed subgraph into a sequence.
[0099] The vector fusion unit is used to embed the sequence and add a type vector to each element in the sequence to distinguish whether the element is an entity or a relationship.
[0100] As a preferred embodiment, the model training module includes:
[0101] The multi-scale atrous attention unit is used to introduce atrous convolution in attention, and utilize the sparsity of the self-attention mechanism at different scales. A parallel strategy is adopted to set different atrous rates on different attention heads to perform atrous attention to construct multi-scale atrous attention, learn feature information at different scales, fuse the outputs of different attention heads, and use linear layers to aggregate these feature information to obtain multi-scale feature vectors.
[0102] The multi-head structure-aware attention unit is used to add the adjacency matrix of the subgraph as an attention bias item in the calculation process of the standard multi-head self-attention mechanism, provide the connection information between the nodes in the subgraph, ensure that the attention calculation score and the subgraph structure remain highly consistent, and obtain the encoding result after the Transformer.
[0103] The decoding unit is used to perform a bidirectional inner product calculation on the encoding result and the candidate entity embedding vector matrix to obtain the score of each candidate entity and output a prediction result.
[0104] The model optimization unit is used to calculate the loss function using binary cross entropy and optimize the model parameters through back propagation to obtain a trained knowledge graph embedding model.
[0105] Verification experiment:
[0106] The results are verified by comparative experiments on FB15k-237 and WN18RR. These two public knowledge graph datasets are respectively from Freebase e and WordNet knowledge base, and each dataset is divided into training set, validation set, and test set. Table 1 lists their statistics.
[0107] Table 1 Statistics of different knowledge graph datasets
[0108] Dataset Number of relationships Number of entities Training set Validation set Test Set FB 15K-237 237 14541 272115 17535 20446 WN18RR 1 1 40943 86835 3034 3034
[0109] The following experiment takes WN18RR as an example to verify the knowledge graph embedding method based on MSDT proposed in this invention.
[0110] This experiment compares the most advanced knowledge graph embedding methods on the test set. The main comparisons are the mean reciprocal rank (MRR) and the top ten, top three, and first recall rates (Hits@10, Hits@3, Hits@1). S is the set of triples, |S| is the number of triples, rank i Represents the link prediction ranking of the i-th triple; Hits@N refers to the average proportion of triples ranked less than N in link prediction. The larger the values of MRR and Hits@N, the better the performance of the model. For each triple (h, r, t) in the test set, when predicting the tail entity t, all candidate tail entities are sorted according to the score. The higher the ranking, the greater the possibility that the triple is established. Therefore, whether the predicted tail entity ranks in the top ten, top three, and first can be used as an indicator to measure the quality of the model. Similarly, the same is true for the predicted head entity. It can be seen from the results in Table 2 that the method of the present invention (MSDT) has better performance than the current advanced methods.
[0111] Table 2 Performance comparison table
[0112]
[0113]
[0114] The knowledge graph embedding method of this embodiment can be used for a variety of tasks, such as link prediction, knowledge graph completion, etc. Therefore, the method of the present invention can improve the embedding accuracy while ensuring that the number of parameters is not significantly increased, thereby ensuring the execution of tasks such as link prediction and knowledge graph completion, and has a high application value.
[0115] Finally, it should be noted that the above embodiments are only used to illustrate the technical solution of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solution of the present invention can be modified or replaced by equivalents without departing from the purpose and scope of the technical solution, which should be included in the scope of the claims of the present invention.
Claims
1. A knowledge graph embedding method based on multi-scale holey Transformer, characterized in that: The method specifically comprises the following steps: S1: Sample collection phase: Input the knowledge graph dataset and use the random walk-based sampling method to sample the central triple T C The neighbor nodes are sampled and the sampling result T is obtained G ; S2: Vector fusion stage: T G Convert to a sequence and encode it to get the input X. Add a type vector to each element in X to distinguish the element type in the sequence, and use linear projection to map X to query Q, key K, value V; S3: Model construction and training phase: Construct a deep learning model based on multi-scale dilated Transformer. This model is a two-stage Transformer architecture. In the first stage, multi-scale dilated attention fused with dilated convolution is used to learn complex local feature information. In the second stage, the multi-head structure fused with subgraph adjacency matrix is used to perceive attention, ensuring that the attention calculation score and subgraph structure are highly consistent, learning global information, and obtaining the encoding result of Transformer. m , O m Contains local and global feature information; S4: Link prediction stage: Transformer-encoded feature vector O m Converted to the score of the triple (h, r, t).
2. The knowledge graph embedding method according to claim 1, characterized in that: Step S2 specifically includes: defining a knowledge graph G = {V, R, ε}, where V is an entity set, R is a relationship set, ε is a triple set, and triple T = (h, r, t) ∈ ε, indicating that there is a certain relationship r ∈ R between the head entity h ∈ V and the tail entity t ∈ V, so that the fact is true; For a given central triplet T C After sampling in step S1, T G After that, T G The relations in the graph are converted into relation nodes, and the entire graph becomes a directed graph without edge attributes. By converting the graph into a sequence and embedding the sequence, we can get the input X = {x1, x2, ..., x n }∈R m×d , where x i ∈R d , n and d are the number of elements in the sequence and the embedding dimension respectively; then add the type vector t to each element in X e ,t r ∈R d To distinguish the type of elements in the sequence, where t e ,t r Represent entities and relations respectively; project X into different linear spaces Get the corresponding query Q, key K, value V as the input of the attention layer, where d Q =d K =d V =d / N, where N represents the number of attention heads in multi-head attention.
3. The knowledge graph embedding method according to claim 2, characterized in that: Step S3 specifically includes the following steps: S31: In the attention mechanism, the atrous convolution is integrated to construct DA as the first stage in the Transformer, so that all nodes in the input sequence can fully interact to obtain complex local information; the atrous convolution introduces the parameter "atrous rate" to modify the convolution kernel size when processing data, which can obtain a larger receptive field under the same parameters and calculation amount; in the attention calculation process, the matrix Q, K, V formed by the projection of the input X is used as the input of the attention module, and the result of K after atrous convolution is matrix multiplied with Q, and the adjacency matrix of the sampled subgraph is added to the calculation process as an attention bias item. After the Softmax operation, it is matrix multiplied with V after atrous convolution to obtain the output result of the atrous attention. The specific calculation process is as follows: Q=XW Q ,K=XW K ,V=XW V Among them, DA represents the hole attention mechanism, l represents the hole rate used by the hole convolution, Θ represents the adjacency matrix of the sampled subgraph, and DConv l represents a dilated convolution with a dilated rate of l; C1D represents a one-dimensional convolution, which is used to enhance the feature interaction in the multi-head attention calculation process. By inserting C1D before and after the Softmax operation and superimposing the low-rank distribution calculated by each attention head, the expression ability of the attention mechanism is enhanced; in order to utilize the sparsity of the self-attention mechanism at different scales, a parallel strategy is adopted to set different dilated rates on different attention heads to perform DA to construct MSDA, learn feature information of different scales, fuse the outputs of different attention heads, and use linear layers to aggregate these feature information to obtain a multi-scale feature vector Y. The feature vector is generated according to the following formula: Y=MSDA(Q,K,V)=Linear(Concat[h1,…,h N ]) h i =DA(Q i ,K i ,V i ,l i ) Among them, MSDA represents multi-scale hole attention mechanism, DA represents hole attention mechanism; Q i ,K i ,V i They represent the vector segments input to the i-th attention head respectively; Y is the feature vector obtained by using multi-scale hole attention in Transformer, which fully learns the local interaction information between each node in the subgraph; S32: Based on the standard self-attention, the adjacency matrix of the subgraph is fused to construct SASA as the second stage in the Transformer; the standard self-attention mechanism can effectively capture the global information in the input sequence and obtain the weight of each element. In the calculation process of the standard multi-head self-attention mechanism, the adjacency matrix of the subgraph is added as the attention bias item to provide the connection information between each node in the subgraph to ensure that the attention weight is consistent with the subgraph structure. The specific calculation process is as follows: Q=YES Q ,K=YW K ,V=YW V Among them, SASA represents the structure-aware self-attention mechanism; C1D represents one-dimensional convolution, Θ∈R n×n Represents the adjacency matrix of the sampled subgraph. If two nodes are connected, the value of the corresponding position in the matrix is 1, otherwise it is 0. In order to obtain information from different representation subspaces at different positions, SASA is performed in parallel in multiple attention heads, and then the outputs of these attention heads are connected in series and aggregated through linear projection to form MHSA to obtain the final output result Z, which is generated according to the following formula: Z=MHSA(Q,K,V)=linear(concat[h1,h2,…,h N ]) h i =SASA(Q i ,K i ,V i ) Among them, MHSA represents the multi-head structure-aware self-attention mechanism; Q i ,K i ,V i They respectively represent the input of the i-th attention head; the feature vector Z contains not only the local information obtained by the multi-scale void attention stage, but also the global feature information obtained by the multi-head structure-aware attention.
4. The knowledge graph embedding method according to claim 3, characterized in that: Step S4 specifically includes: obtaining a feature vector O after Transformer encoding m , which contains all the feature information of the head entity, the relationship, and the context, by using the bidirectional inner product calculation O m and the embedding of the tail entity to obtain the score of the triple: in, Yes m The transpose representation of t Embedding vector representing the tail entity; Then use the nonlinear logic sigmod function to get the probability distribution of whether all candidate tail entities are correct: p(t)=sigmod(φ(h,r,t)) During the model training process, in order to ensure that the correct triplet can obtain a higher score than other triplets, the binary cross entropy loss is used to optimize the model. The specific calculation process of the loss function L is as follows: Among them, t i is a label, if t i If it is the correct tail entity, its corresponding value is 1, otherwise it is 0; |E| is the number of entities in the knowledge graph; the loss function takes (h, r) pairs and scores all candidate tail entities at the same time. The higher the score, the greater the possibility that the corresponding triple is established.
5. A knowledge graph embedding device based on multi-scale void transformer, characterized in that: The device includes: The sample collection module is used to build an index for all entities and relationships in the knowledge graph, and use a random walk-based sampling method to obtain neighbor triplets of the central triplet; The vector fusion module is used to encode entities and relations to obtain the embedding vectors of entities and relations, and use the embedding vectors of adjacent entities and relations as the context of the entity vector to obtain the fused embedding vector; A model training module, used to train the initial knowledge graph embedding model based on the fused embedding vector to obtain a trained knowledge graph embedding model, wherein the initial knowledge graph embedding model is a deep learning model based on a multi-scale hole Transformer as described in any one of claims 1 to 4; The link prediction module is used to combine entities and relations in the knowledge graph to obtain (h, r) pairs, and use them as inputs of the trained knowledge graph embedding model to obtain prediction results.
6. The knowledge graph embedding device according to claim 5, characterized in that: The sample collection module comprises: The index generation unit is used to generate indexes for all entities and relationships in the original knowledge graph; Subgraph sampling unit, used to convert the central triplet T C As input, a random walk based sampling method is used to sample T C The neighbor nodes are sampled and the sampling result T is obtained G =T C ∪E context ∪R context , where E context and R context Respectively represent T C Adjacent entities and relations, taking the sampling results as sample data.
7. The knowledge graph embedding device according to claim 5, characterized in that: The vector fusion module includes: A subgraph conversion unit is used to convert the relations in the sampling results into relation nodes, so that the entire sampling subgraph becomes a directed subgraph without edge attributes, and the directed subgraph is converted into a sequence; The vector fusion unit is used to embed the sequence and add a type vector to each element in the sequence to distinguish whether the element is an entity or a relationship.
8. The knowledge graph embedding device according to claim 5, characterized in that: The model training module includes: Multi-scale dilated attention unit, used to introduce dilated convolution in attention, and utilize the sparsity of self-attention mechanism at different scales. It adopts parallel strategy to set different dilated rates on different attention heads to perform dilated attention to construct multi-scale dilated attention, learn feature information of different scales, fuse the outputs of different attention heads and use linear layers to aggregate these feature information to obtain multi-scale feature vectors. The multi-head structure-aware attention unit is used to add the adjacency matrix of the subgraph as an attention bias item in the calculation process of the standard multi-head self-attention mechanism, provide the connection information between the nodes in the subgraph, ensure that the attention calculation score and the subgraph structure are highly consistent, and obtain the encoding result after the Transformer; The decoding unit is used to perform a bidirectional inner product calculation on the encoding result and the candidate entity embedding vector matrix to obtain the score of each candidate entity and output the prediction result; The model optimization unit is used to calculate the loss function using binary cross entropy and optimize the model parameters through back propagation to obtain a trained knowledge graph embedding model.
Citation Information
Cited By
Time series prediction modeling method, prediction method, equipment, medium and product
CN119416985A
Time series forecasting modeling methods, forecasting methods, equipment, media and products
CN119416985B
Mineral resource prediction method and device based on deep learning, and storage medium
CN119939163A
Risk assessment method, device and equipment for operation state of power grid and medium
CN120087748A
Link prediction method and system based on GNN and Transform
CN120561345A
