Time sequence mapping knowledge domain multi-hop reasoning method based on triplet distributor
By using the triple allocator network and K-means soft reward mechanism in the timing knowledge graph, the problems of entity and relationship decoupling and sparse reward matrix are solved, and more efficient and accurate timing knowledge graph multi-hop reasoning is achieved.
Patent Information
- Application Number
- CN202510076624.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-17
- Publication Date
- 2025-04-11
AI Technical Summary
The existing time sequence knowledge graph multi-hop reasoning method has the problem of decoupling entities and relationships, resulting in limited performance. At the same time, the reward mechanism based on the Dirichlet distribution has the problem of sparse reward matrix.
The time sequence knowledge graph multi-hop reasoning method based on triple allocator is adopted, and the entity embedding is pre-trained and preclustered through the Tucker model and the K-means algorithm. The entity and relationship embedding are updated in combination with the triple allocator network, and relationship-level and entity-level inference are performed through long and short-term memory networks and feedforward neural networks. At the same time, the K-means soft reward mechanism is used to alleviate the sparse reward problem.
Effectively capture the potential dependence of entities and relationships, improve the inference accuracy and efficiency of the knowledge graph, alleviate the sparse problem of reward matrix, and provide more accurate and efficient inference support.
Smart Images

Figure CN119808955A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of knowledge graph reasoning technology, and specifically to a temporal knowledge graph multi-hop reasoning method based on a triplet distributor. Background Art
[0002] In the context of the rapid development of information technology, knowledge graphs play an important role in many fields such as medical care, transportation, and recommendation systems due to their efficient data organization and retrieval capabilities. However, most events in the real world, such as weather forecasts and price forecasts, have complex timeliness and dynamics. They are not just static, but change dynamically over time. Therefore, traditional knowledge graphs can no longer meet the needs of effective management and reasoning of these dynamic data, and time series knowledge graphs have emerged.
[0003] Multi-hop reasoning in a temporal knowledge graph refers to the process of decomposing a complex reasoning problem into a series of simple sub-problems in a knowledge graph, and solving one of the sub-problems in each hop. Formally, multi-hop reasoning in a temporal knowledge graph connects more complex relationships and events by recursively querying and reasoning on multiple interrelated nodes and edges, thereby enabling the system to derive deeper knowledge and causal relationships.
[0004] At present, for multi-hop reasoning tasks in temporal knowledge graphs, mainstream models all combine historical path representation and time encoding with reinforcement learning reward mechanisms for strategy learning. These methods face the problem of action space explosion. Existing models use a hierarchical reasoning framework to decompose reasoning into relational and entity levels to alleviate the problem of action space explosion. However, due to the decoupling of entity embedding and relational embedding, the potential dependencies between entities and relations are not effectively learned, resulting in limited performance of the two-layer reasoning framework. In addition, the reward mechanism based on Dirichlet distribution has the problem of sparse reward matrix. Summary of the invention
[0005] In view of the above-mentioned decoupling problem of entities and relations and the problem of sparse reward matrix in the reward mechanism based on Dirichlet distribution, the present invention provides a multi-hop reasoning method of temporal knowledge graph based on triplet allocator.
[0006] In order to achieve the above object, the present invention adopts the following technical solution: a multi-hop reasoning method of temporal knowledge graph based on triplet distributor, comprising the following steps:
[0007] S1, obtain the historical time series knowledge graph to be inferred, initialize the embedding of entities and relations, and use hybrid time coding to integrate timestamp information into entities;
[0008] S2, use the Tucker model to pre-train entity embeddings, use the K-means algorithm to pre-cluster the pre-trained entity embeddings and timestamps, and group similar entities and timestamps into the same cluster;
[0009] S3, using the triplet distributor network TDN to update the entity and relation embeddings before each iteration to obtain the updated entity embeddings and relation embeddings;
[0010] S4, relation-level reasoning: construct a candidate relation space, use a long short-term memory network to encode the relation-level historical path, predict the next stage relation through a feedforward neural network, select the best matching relation in the candidate relation set, and use the selected best matching relation as the path information for entity-level reasoning;
[0011] S5, entity-level reasoning: construct a candidate entity space, use a long short-term memory network to encode the entity-level historical path, use a feedforward neural network to predict the entity at the next time step, and select the most matching object entity through similarity calculation;
[0012] S6, repeat S3-S5 until the termination condition is reached to obtain the final predicted entity, wherein the termination condition is reaching a preset reasoning path length T, T∈[2,4];
[0013] S7, extracts the cluster labels of the predicted entity and the target entity and the cluster labels of the corresponding timestamps, compares the cluster labels of the predicted entity and the target entity to return the reward signal, calculates the cumulative discounted reward and loss, and updates the parameters of the embedding layer, LSTM, triplet distributor, and inference network.
[0014] Furthermore, the above S1 includes:
[0015] S11, using the current query time series knowledge graph (e qs , r q ,? ,t q ) or (?, r q , e qo , t q )’s timestamp t q , get the relative time difference dt,
[0016] dt=t q -t
[0017] Among them, e qs is the subject entity of the query, e qo is the object entity of the query, t q is the current query timestamp, r q is the relationship to be queried, dt is the relative time difference, and t is the historical timestamp;
[0018] S12, using the relative time difference Δt to obtain the relative time code t d , the formula is as follows:
[0019]
[0020] Among them, w t is the learned weight vector, b t is a bias term, d1 is a hyperparameter, indicating the timestamp embedding dimension, linspace(0,9,d1) generates d1 equally spaced values from 0 to 9 to adjust the impact of different dimensions, t d Indicates relative time coding;
[0021] S13, generating an absolute time code according to the sine wave function and the cosine wave function, the formula is as follows:
[0022]
[0023] Where d1 is the time embedding dimension, i is the dimension index, and t abs Indicates absolute time coding;
[0024] S14, using relative time coding and absolute time coding to perform mixed time coding, as shown below:
[0025]
[0026] Among them, α t is a learnable parameter used to adjust the weights of the two temporal encodings. Indicates mixed time coding;
[0027] S15, mixed time encoding Concatenate with the initial entity embedding e′ to get the final entity embedding:
[0028]
[0029] Where e is the final entity embedding, e′ is the initialized entity embedding, and the final entity embedding dimension is the hyperparameter d3.
[0030] Furthermore, the above S2 includes:
[0031] S21, use the Tucker model to pre-train entity embedding and obtain the initial representation E of the entity:
[0032] E = Tucker (E rae )
[0033] Among them, E raw is the original entity data, and E is the pre-trained entity embedding;
[0034] S22, pre-trained entity embedding E and absolute time encoding t abs Apply the K-means clustering algorithm to obtain the cluster label C:
[0035] C=K-means(E,t abs ; k)
[0036] Where k is a hyperparameter, indicating the number of clusters;
[0037] S23, embed each entity e i and timestamp t j Assign cluster label c i and c j :
[0038] c i =C(e i )
[0039] c j =C(t j ).
[0040] Furthermore, the triplet distributor network TDN of the above S3 includes:
[0041] S31, extracts the information features of entities and relationships through gating functions and spatial convolution, and obtains the aggregated intermediate representation of entities and relationships. The formula is as follows:
[0042] e q1 =(eq)⊙[σ(f(eq))]
[0043] r q1 =(rq)⊙[σ(f(rq))]
[0044] Where σ represents the activation function, ⊙ represents element-by-element multiplication, f(·) represents the spatial convolution operation, which is used to mine the relationship between different spaces, q is the aggregate information of the previous iteration, e represents entity embedding, r represents relationship embedding, and e q1 and r q1 It is the intermediate representation of entities and relationships in the aggregation stage;
[0045] Spatial convolution operation:
[0046] f(F)=f 3 (Concat(AvgP(F),MaxP(F)))
[0047] Among them, AvgP(·) and MaxP(·) are maximum pooling and average pooling operations respectively, Concat(·) represents the concatenation operation, and f 3 (·) represents a 3x3 convolutional layer, and F represents the feature matrix of the input convolutional layer;
[0048] S32, message aggregation is performed through the residual network to obtain the aggregation information of the current time step. The formula is as follows:
[0049]
[0050] in, represents the sum operation, is the updated aggregate information;
[0051] S33, extracts the intermediate representation of entity and relationship distribution from the aggregated information through gating function and spatial convolution, the formula is as follows:
[0052]
[0053] in, Indicates the aggregate information after the current iteration update;
[0054] S34, distribute the obtained information through the residual network to obtain the updated entity embedding and relationship embedding of the current time step, the formula is as follows:
[0055]
[0056] in, and Represents the updated entity and relation embeddings.
[0057] Furthermore, the relational level reasoning in S4 above specifically includes:
[0058] S41, obtain the main entity e of the current given query qs All relevant relations constitute the candidate relation space It is expressed as follows:
[0059]
[0060] Among them, r represents the relationship, e qs is the subject entity of the query, e qo is the object entity of the query, t p is the starting timestamp, t q is the query timestamp, t p ≤t q represents the validity within a time range, ε represents the knowledge graph, and the set The representation in real number space is d2 is a hyperparameter, which represents the embedding dimension of the relationship;
[0061] S42, using a long short-term memory network to encode a relationship-level historical path, where the historical path refers to a series of entities and relationships arranged in chronological order starting from the subject entity of the query; the initial state of the long short-term memory network Obtained by passing the embedding of the initial input and relation r0 to the LSTM network:
[0062]
[0063] Where r0 is the first relation in the reasoning path, i.e. the queried relation r q , is the initial state of the LSTM network, i.e., the initial representation of the historical path of the relationship;
[0064] For i>0, i≤T, the state of the LSTM at the i-th time step is by converting the previous state and the current relationship i-1 Passed to the LSTM network to update:
[0065]
[0066] in, is the state of the previous time step, i.e., the historical path representation of the relationship of the i-1th hop, r i-1 is the representation of the current query relation, Represent the updated relationship history path;
[0067] According to the embedding r of the updated query relation q , and the current state of LSTM Generate relation-level reasoning state
[0068]
[0069] in, is the current relationship history path representation, r q is the embedding of the query relation, is the relation-level reasoning state;
[0070] S43, using a feedforward neural network to predict the next stage of relations:
[0071]
[0072] in, and is the weight matrix of the fully connected layer, d4 is a hyperparameter representing the dimension of the relation-level reasoning state, d5 is a hyperparameter representing the number of hidden units in the fully connected layer, and r m Relations inferred for feed-forward neural networks;
[0073] S44, using inner product calculation and r m The similarity is converted into a probability distribution through the softmax function σ, and the best matching relationship o is selected according to the similarity and passed to the entity level process. The formula is expressed as:
[0074]
[0075] Among them, μ represents the relation-level reasoning strategy, θ r represents the parameters of the relational level reasoning layer, r m is the relationship inferred by the feedforward neural network, represents the relational reasoning state of the i-th hop, represents the candidate relationship space, and o represents the best matching relationship selected based on similarity.
[0076] Furthermore, the entity-level reasoning in S5 above includes:
[0077] S51, candidate entity space Including from the main entity e qs The object entity that can be reached by the most matching relation o selected by similarity is expressed as follows:
[0078]
[0079] Among them, e o is an object entity, a collection The representation in real number space is d3 is a hyperparameter, which is the entity embedding dimension;
[0080] S52, using long short-term memory network to encode entity-level historical path, initial state of LSTM By embedding the initial input and query header entities qs Pass it to the LSTM network to get:
[0081]
[0082] in, is the initial state of LSTM, i.e., the initial representation of the entity’s historical path;
[0083] For i>0, i≤T, the state of LSTM is by converting the previous state and the current entity e i-1 Passed to the LSTM network to update:
[0084]
[0085] in, is the state of the previous time step, i.e., the entity history path representation of the i-1th hop, e i-1 is the representation of the current query head entity, Representing the updated entity history path;
[0086] According to the updated embedding of the query entity e qs , and the current state of LSTM Generate relation-level reasoning state
[0087]
[0088] in, is the historical path representation of the current relationship, is the entity-level reasoning state;
[0089] S53, use a feedforward neural network to predict the entity at the next time step:
[0090]
[0091] in, and is the weight matrix of the fully connected layer, d6 is a hyperparameter representing the dimension of the entity-level state, d5 is a hyperparameter representing the number of hidden units in the fully connected layer, and e m Relations inferred for feed-forward neural networks;
[0092] S54, using inner product calculation and e m The similarity is converted into a probability distribution through the softmax function σ, and the best matching object entity a is selected. The formula is expressed as:
[0093]
[0094] Among them, π represents the entity-level reasoning strategy, θ e represents the entity-level reasoning layer parameters, e m is the entity inferred by the feedforward neural network, represents the entity reasoning state of the i-th hop, and a represents the best matching entity selected based on similarity.
[0095] Furthermore, the above S6 includes:
[0096] According to the current time step i, if i<T, repeat S3-S5,
[0097] If i == T, the reasoning is completed and the final predicted entity and timestamp are obtained. The formula is as follows:
[0098] e P=a[:d3-d1]
[0099]
[0100] Among them, e P is the prediction entity, is the prediction timestamp, d3 is a hyperparameter, is the entity embedding dimension, and d1 is a hyperparameter, is the time encoding dimension.
[0101] Furthermore, the above S7 specifically includes:
[0102] S71, calculate the soft reward of the prediction result, the formula is as follows:
[0103]
[0104] Among them, e P is the prediction entity, e qo is the target entity, is the prediction timestamp, is the target timestamp, I{·} is the indicator function, which is 1 when the condition is true and 0 otherwise, C represents the cluster label, represents the soft reward value;
[0105] S72, determine the final reward value r, the formula is as follows:
[0106]
[0107] Among them, α0 is the discount factor, which is used to adjust the reward value belonging to the same cluster to decay with the iteration process, and τ is the iteration round;
[0108] S73, calculate the cumulative discount reward for each hop, the formula is as follows:
[0109]
[0110] Among them, i represents the current time step, that is, the current number of hops, T represents the total length of the path, that is, the total number of time steps, k is an index variable, representing each future event step from the current time step i to the end of the path T, γ is a hyperparameter, representing the discount factor, r i+k represents the reward obtained at time step i+k;
[0111] S74, calculate the entropy regularization loss, the formula is as follows:
[0112]
[0113] Where N is the batch size, T is the path length, A is the action space size, and p i,j,k is the probability of selecting the kth action at the i-th sample and j-th time step;
[0114] S75, calculate the loss of reinforcement learning, the formula is as follows:
[0115]
[0116] Among them, p i,j is the probability of selecting an action at the i-th sample and the j-th time step, R i,j is the cumulative discounted reward at the i-th sample and j-th time step, and b is the baseline value;
[0117] The baseline value b is updated by the following formula:
[0118]
[0119] Where λ is the baseline update coefficient, R i is the cumulative discounted reward for the ith sample, and N is the batch size;
[0120] S76, the total loss is calculated by entropy regularization loss and reinforcement learning loss, the formula is as follows:
[0121] L=L RL -α·L entropy
[0122] Among them, α(α∈[10 -3 ,10 -1 ]) is the entropy regularization coefficient.
[0123] S77, the prediction ability of this method is measured by the evaluation indicators MRR and Hits@n.
[0124] The present invention has the following beneficial effects:
[0125] The present invention solves the decoupling problem of entities and relations and the sparsity problem of reward matrix in the prior art. By fully capturing the potential dependencies between entities and relations, it provides more accurate and efficient reasoning support for knowledge graph-driven applications. BRIEF DESCRIPTION OF THE DRAWINGS
[0126] Figure 1 It is a flow chart of the overall model of multi-hop reasoning of the temporal knowledge graph provided by the present invention;
[0127] Figure 2 is a schematic diagram of a triplet distributor provided by the present invention;
[0128] Figure 3 It is a schematic diagram of the K-means soft reward mechanism module provided by the present invention. DETAILED DESCRIPTION
[0129] The present invention is further described below in conjunction with the accompanying drawings and specific embodiments. It should be pointed out that the technical solution and design principle of the present invention are described in detail below only with an optimized technical solution.
[0130] The embodiments are preferred implementations of the present invention, but the present invention is not limited to the above-mentioned implementations. Any obvious improvements, substitutions or modifications that can be made by those skilled in the art without departing from the essential content of the present invention belong to the protection scope of the present invention.
[0131] like Figure 1 As shown, the present invention provides a multi-hop reasoning method for a temporal knowledge graph, comprising the following steps:
[0132] S1, obtain the historical time series knowledge graph to be inferred, initialize the embedding of entities and relations, and use hybrid time coding to integrate timestamp information into entities;
[0133] S2, use the Tucker model to pre-train entity embeddings, use the K-means algorithm to pre-cluster the pre-trained entity embeddings and timestamps, and group similar entities and timestamps into the same cluster;
[0134] S3, using the triplet distributor network TDN to update the entity and relation embeddings before each iteration to obtain the updated entity embeddings and relation embeddings;
[0135] S4, relation-level reasoning: construct a candidate relation space, use a long short-term memory network to encode the relation-level historical path, predict the next stage relation through a feedforward neural network, select the best matching relation in the candidate relation set, and use the selected best matching relation as the path information for entity-level reasoning;
[0136] S5, entity-level reasoning: construct a candidate entity space, use a long short-term memory network to encode the entity-level historical path, use a feedforward neural network to predict the entity at the next time step, and select the most matching object entity through similarity calculation;
[0137] S6, repeat S3-S5 until the termination condition is reached to obtain the final predicted entity, wherein the termination condition is reaching a preset reasoning path length T, T∈[2,4];
[0138] S7, extracts the cluster labels of the predicted entity and the target entity and the cluster labels of the corresponding timestamps, compares the cluster labels of the predicted entity and the target entity to return the reward signal, calculates the cumulative discounted reward and loss, and updates the parameters of the embedding layer, LSTM, triplet distributor, and inference network.
[0139] As a preferred embodiment of the present invention, step S1 includes:
[0140] S11, using the current query time series knowledge graph (e qs ,r q ,? ,t q ) or (?,r q ,e qo ,t q )’s timestamp t q , get the relative time difference dt,
[0141] dt=t q -t
[0142] Among them, e qs is the subject entity of the query, e qo is the object entity of the query, t q is the current query timestamp, r q is the relationship to be queried, dt is the relative time difference, and t is the historical timestamp.
[0143] S12, using the relative time difference Δt to obtain the relative time code t d , the formula is as follows:
[0144]
[0145] Among them, w t is the learned weight vector, b t is a bias term, d1 (d1 = 20) is a hyperparameter, indicating the timestamp embedding dimension, linspace(0,9,d1) generates d1 equally spaced values from 0 to 9 to adjust the impact of different dimensions, t d Indicates relative time coding;
[0146] S13, generating an absolute time code according to the sine wave function and the cosine wave function, the formula is as follows:
[0147]
[0148] Where d1 is the time embedding dimension, i is the dimension index, and t abs Indicates absolute time coding;
[0149] S14, using relative time coding and absolute time coding to perform mixed time coding, as shown below:
[0150]
[0151] Among them, α t is a learnable parameter used to adjust the weights of the two temporal encodings. Indicates mixed time coding;
[0152] S15, mixed time encoding Concatenate with the initial entity embedding e′ to get the final entity embedding:
[0153]
[0154] Where e is the final entity embedding, e′ is the initialized entity embedding, and the final entity embedding dimension is the hyperparameter d3 (d3=100).
[0155] As a preferred embodiment of the present invention, step S2 includes:
[0156] S21, use the Tucker model to pre-train entity embedding and obtain the initial representation E of the entity:
[0157] E = Tucker (E raw )
[0158] Among them, E raw is the original entity data, and E is the pre-trained entity embedding;
[0159] S22, pre-trained entity embedding E and absolute time encoding t abs Apply the K-means clustering algorithm to obtain the cluster label C:
[0160] C = K-means(E, t abs ; k)
[0161] Where k (k∈[4,7]) is a hyperparameter, indicating the number of clusters;
[0162] S23, embed each entity e i and timestamp t j Assign cluster label c i and c j :
[0163] c i =C(e i )
[0164] c j =C(t j ).
[0165] like Figure 2 As shown, as a preferred embodiment of the present invention, step S3 includes:
[0166] S31, extracts the information features of entities and relationships through gating functions and spatial convolution, and obtains the aggregated intermediate representation of entities and relationships. The formula is as follows:
[0167] e q1 =(eq)⊙[σ(f(eq))]
[0168] r q1=(rq)⊙[σ(f(rq))]
[0169] Where σ represents the activation function, ⊙ represents element-by-element multiplication, f(·) represents the spatial convolution operation, which is used to mine the relationship between different spaces, q is the aggregate information of the previous iteration, e represents entity embedding, r represents relationship embedding, and e q1 and r q1 It is the intermediate representation of entities and relationships in the aggregation stage;
[0170] Spatial convolution operation:
[0171] f(F)=f 3 (Concat(AvgP(F),MaxP(F)))
[0172] Among them, AvgP(·) and MaxP(·) are maximum pooling and average pooling operations respectively, Concat(·) represents the concatenation operation, and f 3 (·) represents a 3x3 convolutional layer, and F represents the feature matrix of the input convolutional layer;
[0173] S32, message aggregation is performed through the residual network to obtain the aggregation information of the current time step. The formula is as follows:
[0174]
[0175] in, represents the sum operation, is the updated aggregate information;
[0176] S33, extracts the intermediate representation of entity and relationship distribution from the aggregated information through gating function and spatial convolution, the formula is as follows:
[0177]
[0178]
[0179] in, Indicates the aggregate information after the current iteration update;
[0180] S34, distribute the obtained information through the residual network to obtain the updated entity embedding and relationship embedding of the current time step, the formula is as follows:
[0181]
[0182] in, and Represents the updated entity and relation embeddings.
[0183] As a preferred embodiment of the present invention, step S4 includes:
[0184] S41, get the main entity e related to the current given query qs All relevant relations constitute the candidate relation space It is expressed as follows:
[0185]
[0186] Among them, r represents the relationship, e qs is the subject entity of the query, e qo is the object entity of the query, t p is the starting timestamp, t q is the query timestamp, t p ≤t q represents the validity within a time range, ε represents the knowledge graph, and the set The representation in real number space is d2 is a hyperparameter, d2=100, which represents the embedding dimension of the relationship;
[0187] S42, using a long short-term memory network to encode a relationship-level historical path, where the historical path refers to a series of entities and relationships arranged in chronological order starting from the subject entity of the query; the initial state of the long short-term memory network Obtained by passing the embedding of the initial input and relation r0 to the LSTM network:
[0188]
[0189] Where r0 is the first relation in the reasoning path, i.e. the queried relation r q , is the initial state of the LSTM network, i.e., the initial representation of the historical path of the relationship;
[0190] For i>0, i≤T, the state of the LSTM at the i-th time step is by converting the previous state and the current relationship i-1 Passed to the LSTM network to update:
[0191]
[0192] in, is the state of the previous time step, i.e., the historical path representation of the relationship of the i-1th hop, r i-1 is the representation of the current query relation, Represent the updated relationship history path;
[0193] According to the embedding r of the updated query relation q , and the current state of LSTM Generate relation-level reasoning state
[0194]
[0195] in, is the current relationship history path representation, r q is the embedding of the query relation, is the relation-level reasoning state;
[0196] S43, using a feedforward neural network to predict the next stage of relations:
[0197]
[0198] in, and is the weight matrix of the fully connected layer, d4 is a hyperparameter, d4=100, indicating the dimension of the relation-level reasoning state, d5 is a hyperparameter, d5=100, indicating the number of hidden units in the fully connected layer, r m Relations inferred for feed-forward neural networks;
[0199] S44, using inner product calculation and r m The similarity is converted into a probability distribution through the softmax function σ, and the best matching relationship o is selected according to the similarity and passed to the entity level process. The formula is expressed as:
[0200]
[0201] Among them, μ represents the relation-level reasoning strategy, θ r represents the parameters of the relational level reasoning layer, r m is the relationship inferred by the feedforward neural network, represents the relational reasoning state of the i-th hop, represents the candidate relationship space, and o represents the best matching relationship selected based on similarity.
[0202] As a preferred embodiment of the present invention, step S5 includes:
[0203] S51, candidate entity space Including from the main entity e qs The object entity that can be reached through the relation o selected by the relation-level reasoning layer is expressed as follows:
[0204]
[0205] Among them, e o is an object entity, a collection The representation in real number space is d3 is a hyperparameter, d3=100, which is the entity embedding dimension;
[0206] S52, using long short-term memory network to encode entity-level historical path, initial state of LSTM By embedding the initial input and query header entities qs Pass it to the LSTM network to get:
[0207]
[0208] in, is the initial state of LSTM, i.e., the initial representation of the entity’s historical path;
[0209] For i>0, i≤T, the state of LSTM is by converting the previous state and the current entity e i-1 Passed to the LSTM network to update:
[0210]
[0211] in, is the state of the previous time step, i.e., the entity history path representation of the i-1th hop, e i-1 is the representation of the current query head entity, Represent the updated entity history path;
[0212] According to the updated embedding of the query entity e qs , and the current state of LSTM Generate relation-level reasoning state
[0213]
[0214] in, is the historical path representation of the current relationship, is the entity-level reasoning state;
[0215] S53, use a feedforward neural network to predict the entity at the next time step:
[0216]
[0217] in, and is the weight matrix of the fully connected layer, d6 is a hyperparameter, d6=100, indicating the dimension of the entity-level state, d5 is a hyperparameter, d5=100, is the number of hidden units in the fully connected layer, and e m Relations inferred for feed-forward neural networks;
[0218] S54, using inner product calculation and e m The similarity is then converted into a probability distribution through the softmax function σ, and the best matching object entity a is selected. The formula is expressed as:
[0219]
[0220] Among them, π represents the entity-level reasoning strategy, θ e represents the entity-level reasoning layer parameters, e m is the entity inferred by the feedforward neural network, represents the entity reasoning state of the i-th hop, and a represents the best matching entity selected based on similarity.
[0221] As a preferred embodiment of the present invention, step S6 includes:
[0222] According to the current time step i, if i<T, repeat S3-S5,
[0223] If i == T, the reasoning is completed and the final predicted entity and timestamp are obtained. The formula is as follows:
[0224] e P =a[:d3-d1]
[0225]
[0226] Among them, e P is the prediction entity, is the prediction timestamp, d3 (d3=100) is a hyperparameter, is the entity embedding dimension, and d1 (d1=20) is a hyperparameter, is the time encoding dimension.
[0227] like Figure 3 As shown, as a preferred embodiment of the present invention, step S7 includes:
[0228] S71, calculate the soft reward of the prediction result, the formula is as follows:
[0229]
[0230] Among them, e P is the prediction entity, e qo is the target entity, is the prediction timestamp, is the target timestamp, I{·} is the indicator function, which is 1 when the condition is true and 0 otherwise, C represents the cluster label, represents the soft reward value;
[0231] S72, determine the final reward value r, the formula is as follows:
[0232]
[0233] Among them, α0 (α0 = 0.95) is a discount factor, which is used to adjust the reward value belonging to the same cluster to decay with the iteration process, and τ is the iteration round;
[0234] S73, calculate the cumulative discount reward for each hop, the formula is as follows:
[0235]
[0236] Where i represents the current time step, i.e., the current number of hops, T represents the total path length, i.e., the total number of time steps, k is an index variable representing each future event step from the current time step i to the end of the path T, γ (γ = 0.95) is a hyperparameter representing the discount factor, and r i+k represents the reward obtained at time step i+k;
[0237] S74, calculate the entropy regularization loss, the formula is as follows:
[0238]
[0239] Where N is the batch size, T is the path length, A is the action space size, and p i,j,k is the probability of selecting the kth action at the i-th sample and j-th time step;
[0240] S75, calculate the loss of reinforcement learning, the formula is as follows:
[0241]
[0242] Among them, p i,j is the probability of selecting an action at the i-th sample and the j-th time step, R i,j is the cumulative discounted reward at the i-th sample and j-th time step, and b is the baseline value;
[0243] The baseline value b is updated by the following formula:
[0244]
[0245] Where λ is the baseline update coefficient, R i is the cumulative discounted reward for the ith sample, and N is the batch size;
[0246] S76, the total loss is calculated by entropy regularization loss and reinforcement learning loss, the formula is as follows:
[0247] L=L RL -α·L entropy
[0248] Among them, α(α∈[10 -3 ,10 -1]) is the entropy regularization coefficient.
[0249] S77, the prediction ability of this method is measured by the evaluation indicators MRR and Hits@n. The formula for calculating the evaluation indicators is as follows:
[0250]
[0251] Among them, MRR represents the mean reciprocal rank, S is the number of quadruple sets, and rank(e o |q o ) and rank(e s |q s ) represent the predicted rankings of the correct tail entity and head entity, respectively. Hits@n represents the average proportion of triplets ranked less than n, where n is usually 1, 3, and 10. I(·) is an indicator function whose true value is 1 if the condition is true, otherwise it is 0. The larger the two indicators are, the better the model performance is.
[0252] The present invention also provides a temporal knowledge graph multi-hop reasoning system, which is used to implement a temporal knowledge graph multi-hop reasoning method integrating a hierarchical reasoning framework and a triplet distributor network, including:
[0253] The hierarchical reasoning framework decomposes complex reasoning tasks into entity-level reasoning and relation-level reasoning. Each level focuses on solving a specific part of the problem, thereby simplifying the overall reasoning process and improving efficiency and explainability.
[0254] The triplet distributor independently and effectively models the embedding of entities, relations, and timestamps in the temporal knowledge graph to promote information aggregation and distribution among the three and enhance the reasoning process.
[0255] The K-means discounted reward mechanism provides soft reward feedback between 0 and 1 by comparing the similarity between the predicted entity and the target entity on the pre-clustered labels, thereby alleviating the problem of sparse rewards in reinforcement learning.
Claims
1. A multi-hop reasoning method for temporal knowledge graph based on triplet distributor, characterized in that: It includes the following steps: S1. Obtain the historical temporal knowledge graph to be inferred, initialize the embeddings of entities and relationships, and incorporate timestamp information into entities using hybrid time encoding; S2. Pre-train entity embeddings using the Tucker model, and pre-cluster the pre-trained entity embeddings and timestamps using the K-means algorithm, grouping similar entities and timestamps into the same cluster; S3. Use the Triplet Distributor Network (TDN) to update entity and relationship embeddings before each iteration to obtain updated entity and relationship embeddings; S4. Relationship-level reasoning: Construct a candidate relationship space, use a long short-term memory network to encode relationship-level historical paths, predict the next-stage relationship through a feed-forward neural network, select the most matching relationship from the candidate relationship set, and use the selected most matching relationship as the path information for entity-level reasoning; S5. Entity-level reasoning: Construct a candidate entity space, use a long short-term memory network to encode entity-level historical paths, use a feed-forward neural network to predict the entity at the next time step, and select the most matching object entity through similarity calculation; S6. Repeat S3 - S5 until the termination condition is reached to obtain the final predicted entity. The termination condition is reaching the preset inference path length T, where T ∈ [2, 4]; S7. Extract the clustering labels of the predicted entity and the target entity and the clustering labels of the corresponding timestamps, compare the clustering labels of the predicted entity and the target entity to return a reward signal, calculate the cumulative discounted reward and loss, and update the parameters of the embedding layer, LSTM, triplet distributor, and inference network.
2. According to claim 1, a method for multi-hop reasoning of a temporal knowledge graph based on a triplet distributor is characterized in that: The S1 includes: S11, using the current query time series knowledge graph (e qs , r q ,? ,t q ) or (?, r q , e qo , t q )’s timestamp t q , get the relative time difference dt, dt=t q -t Among them, e qs is the subject entity of the query, e qo is the object entity of the query, t q is the current query timestamp, r q is the relationship to be queried, dt is the relative time difference, and t is the historical timestamp; S12, using the relative time difference Δt to obtain the relative time code t d , the formula is as follows: Among them, w t is the learned weight vector, b t is a bias term, d1 is a hyperparameter, indicating the timestamp embedding dimension, linspace(0, 9, d1) generates d1 equally spaced values from 0 to 9 to adjust the impact of different dimensions, t d Indicates relative time coding; S13. Generate absolute time encoding according to the sine wave function and cosine wave function, with the formula as follows: Where d1 is the time embedding dimension, i is the dimension index, and t abs Indicates absolute time coding; S14. Perform hybrid time encoding using relative time encoding and absolute time encoding, as shown below: Among them, α t is a learnable parameter used to adjust the weights of the two temporal encodings. Indicates mixed time coding; S15, mixed time encoding Concatenate with the initial entity embedding e′ to get the final entity embedding: where e is the final entity embedding, e′ is the initialized entity embedding, and the dimension of the final entity embedding is a hyperparameter d3.
3. According to claim 1, a method for multi-hop reasoning of a temporal knowledge graph based on a triplet distributor is characterized in that: The S2 includes: S21. Pre-train entity embeddings using the Tucker model to obtain the initial representation E of the entity: E=Tucker(E raw ) Among them, E raw is the original entity data, and E is the pre-trained entity embedding; S22, pre-trained entity embedding E and absolute time encoding t abs Apply the K-means clustering algorithm to obtain the cluster label C: C=K-means(E,t abs ;k) where k is a hyperparameter representing the number of clusters; S23, embed each entity e i and timestamp t j Assign cluster label c i and c j : c i =C(e i ) c j =C(t j )。 4. According to claim 1, a method for multi-hop reasoning of a temporal knowledge graph based on a triplet distributor is characterized in that: The Triplet Distributor Network (TDN) in S3 includes: S31. Extract the information features of entities and relationships through a gating function and spatial convolution to obtain the aggregated intermediate representation of entities and relationships, with the formula as follows: And q1 =(eq)⊙[σ(f(eq))] r q1 =(r-q)⊙[σ(f(r-q))] Where σ represents the activation function, ⊙ represents element-by-element multiplication, f(·) represents the spatial convolution operation, which is used to mine the relationship between different spaces, q is the aggregate information of the previous iteration, e represents entity embedding, r represents relationship embedding, and e q1 and r q1 It is the intermediate representation of entities and relationships in the aggregation stage; Spatial convolution operation: f(F)=f 3 (Concat(AvgP(F),MaxP(F))) Among them, AvgP(·) and MaxP(·) are maximum pooling and average pooling operations respectively, Concat(·) represents the concatenation operation, and f 3 (·) represents a 3x3 convolutional layer, and F represents the feature matrix of the input convolutional layer; S32. Perform message aggregation through a residual network to obtain the aggregated information at the current time step, with the formula as follows: in, represents the sum operation, is the updated aggregate information; S33. Extract the assignment intermediate representation of entities and relationships from the aggregated information through a gating function and spatial convolution for the aggregated information, with the formula as follows: in, Indicates the aggregate information after the current iteration update; S34. Distribute the obtained information through a residual network to obtain the updated entity and relationship embeddings at the current time step, with the formula as follows: in, and Represents the updated entity and relation embeddings.
5. According to claim 1, a method for multi-hop reasoning of a temporal knowledge graph based on a triplet distributor is characterized in that: The relationship-level reasoning in S4 specifically includes: S41, get the main entity e related to the current given query qs All relevant relations constitute the candidate relation space It is expressed as follows: Among them, r represents the relationship, e qs is the subject entity of the query, e qo is the object entity of the query, t p is the starting timestamp, t q is the query timestamp, t p ≤t q represents the validity within a time range, ε represents the knowledge graph, and the set The representation in real number space is d2 is a hyperparameter, which represents the embedding dimension of the relationship; S42, using a long short-term memory network to encode a relationship-level historical path, where the historical path refers to a series of entities and relationships arranged in chronological order starting from the subject entity of the query; the initial state of the long short-term memory network Obtained by passing the embedding of the initial input and relation r0 to the LSTM network: Where r0 is the first relation in the reasoning path, i.e. the queried relation r q , is the initial state of the LSTM network, i.e., the initial representation of the historical path of the relationship; For i>0, i≤T, the state of the LSTM at the i-th time step is by converting the previous state and the current relationship i-1 Passed to the LSTM network to update: in, is the state of the previous time step, i.e., the historical path representation of the relationship of the i-1th hop, r i-1 is the representation of the current query relation, Represent the updated relationship history path; According to the embedding r of the updated query relation q , and the current state of LSTM Generate relation-level reasoning state in, is the current relationship history path representation, r q is the embedding of the query relation, is the relation-level reasoning state; S43. Use a feed-forward neural network to predict the next-stage relationship: in, and is the weight matrix of the fully connected layer, d4 is a hyperparameter representing the dimension of the relation-level reasoning state, d5 is a hyperparameter representing the number of hidden units in the fully connected layer, and r m Relations inferred for feed-forward neural networks; S44, using inner product calculation and r m The similarity is converted into a probability distribution through the softmax function σ, and the best matching relationship o is selected according to the similarity and passed to the entity level process. The formula is expressed as: Among them, μ represents the relation-level reasoning strategy, θ r represents the parameters of the relational level reasoning layer, r m is the relationship inferred by the feedforward neural network, represents the relational reasoning state of the i-th hop, represents the candidate relationship space, and o represents the best matching relationship selected based on similarity.
6. According to claim 1, a method for multi-hop reasoning of a temporal knowledge graph based on a triplet distributor is characterized in that: The entity-level reasoning in S5 includes: S51, candidate entity space Including from the main entity e qs The object entity that can be reached by the most matching relation o selected by similarity is expressed as follows: Among them, e o is an object entity, a collection The representation in real number space is d3 is a hyperparameter, which is the entity embedding dimension; S52, using long short-term memory network to encode entity-level historical path, initial state of LSTM By embedding the initial input and query header entities qs Pass it to the LSTM network to get: in, is the initial state of LSTM, i.e., the initial representation of the entity’s historical path; For i>0, i≤T, the state of LSTM is by converting the previous state and the current entity e i-1 Passed to the LSTM network to update: in, is the state of the previous time step, i.e., the entity history path representation of the i-1th hop, e i-1 is the representation of the current query head entity, Representing the updated entity history path; According to the updated embedding of the query entity e qs , and the current state of LSTM Generate relation-level reasoning state in, is the historical path representation of the current relationship, is the entity-level reasoning state; S53. Use a feed-forward neural network to predict the entity at the next time step: in, and is the weight matrix of the fully connected layer, d6 is a hyperparameter representing the dimension of the entity-level state, d5 is a hyperparameter representing the number of hidden units in the fully connected layer, and e m Relations inferred for feed-forward neural networks; S54, using inner product calculation and e m The similarity is then converted into a probability distribution through the softmax function σ, and the best matching object entity a is selected. The formula is expressed as: Among them, π represents the entity-level reasoning strategy, θ e represents the entity-level reasoning layer parameters, e m is the entity inferred by the feedforward neural network, represents the entity reasoning state of the i-th hop, and a represents the best matching entity selected based on similarity.
7. According to claim 1, a method for multi-hop reasoning of a temporal knowledge graph based on a triplet distributor is characterized in that: The S6 includes: According to the current time step i, if i < T, then repeat S3 - S5, If i == T, the reasoning is completed and the final predicted entity and timestamp are obtained. The formula is as follows: the P <a[:d3-d1 Among them, e P is the prediction entity, is the prediction timestamp, d3 is a hyperparameter, is the entity embedding dimension, and d1 is a hyperparameter, is the time encoding dimension.
8. According to claim 1, a method for multi-hop reasoning of a temporal knowledge graph based on a triplet distributor is characterized in that: The S7 specifically includes: S71, calculate the soft reward of the prediction result, the formula is as follows: Among them, e P is the prediction entity, e qo is the target entity, is the prediction timestamp, is the target timestamp, I{·} is the indicator function, which is 1 when the condition is true and 0 otherwise, C represents the cluster label, represents the soft reward value; S72, determine the final reward value r, the formula is as follows: Among them, α0 is the discount factor, which is used to adjust the reward value belonging to the same cluster to decay with the iteration process, and τ is the iteration round; S73, calculate the cumulative discount reward for each hop, the formula is as follows: Among them, i represents the current time step, that is, the current number of hops, T represents the total length of the path, that is, the total number of time steps, k is an index variable, representing each future event step from the current time step i to the end of the path T, γ is a hyperparameter, representing the discount factor, r i+k represents the reward obtained at time step i+k; S74, calculate the entropy regularization loss, the formula is as follows: Where N is the batch size, T is the path length, A is the action space size, and p i,j,k is the probability of selecting the kth action at the i-th sample and j-th time step; S75, calculate the loss of reinforcement learning, the formula is as follows: Among them, p i,j is the probability of selecting an action at the i-th sample and the j-th time step, R i,j is the cumulative discounted reward at the i-th sample and j-th time step, and b is the baseline value; The baseline value b is updated by the following formula: Where λ is the baseline update coefficient, R i is the cumulative discounted reward for the ith sample, and N is the batch size; S76, the total loss is calculated by entropy regularization loss and reinforcement learning loss, the formula is as follows: L=L RL -a·L entropy Among them, α(α∈[10 -3 , 10 -1 ]) is the entropy regularization coefficient; S77, the prediction ability of this method is measured by the evaluation indicators MRR and Hits@n.