Domain knowledge graph-based STM32-bit function question answering method and system
By constructing the STM32 knowledge graph and using the pre-trained model for entity recognition, the problem of difficulty in finding register bit function information in STM32 microcontroller programming is solved, fast and accurate information retrieval is achieved, and programming efficiency is improved.
Patent Information
- Application Number
- CN202410758416.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-13
- Publication Date
- 2024-09-24
AI Technical Summary
During the programming process, when using STM32 microcontrollers, developers need to remember a large amount of peripheral and register information, which increases the difficulty of programming. It is difficult to find register bit function information quickly and accurately with existing technology.
Adopting a method based on domain knowledge graph, by constructing the STM32 knowledge graph, using the pre-trained model mBERT+BILSTM+S-Attention+CRF for entity recognition, and using the Cypher language to query the knowledge graph, we can quickly and accurately find the register bit function information .
It greatly reduces the time to search for register bit function information, improves programming efficiency, reduces dependence on manuals, and enables more comprehensive knowledge information retrieval.
Smart Images

Figure CN118690846A
Abstract
Description
Technical Field
[0001] The present invention relates to a method and system for questioning and answering STM32-bit functions based on a domain knowledge graph, and belongs to the technical field of knowledge graphs. Background Art
[0002] In recent years, scholars have applied knowledge graph technology to the fields of relationship prediction, relationship reasoning, intelligent question answering, and recommendation systems, providing support for the expansion of applications in various fields. Among them, the development of question answering systems can be divided into the following stages. The first is a question answering system based on question-answer pairs; the second is a question answering system based on knowledge graphs; and the third is a question answering system based on large-scale language models.
[0003] In today's rapidly developing technological environment, the technology of processors and microcontrollers continues to advance, bringing unprecedented computing power and greatly promoting the development of artificial intelligence, the Internet of Things and other fields. STM32 high-performance microcontrollers are widely used in various complex applications, demonstrating their powerful functions and flexibility. Although many experienced developers have provided valuable open source projects and learning resources, for developers who use register programming, they need to remember a large number of peripherals and registers as well as their functions, addresses and other information during the coding process, which increases the difficulty of programming. Summary of the invention
[0004] The technical problem to be solved by the present invention is to overcome the shortcomings of the prior art and provide a STM32 bit function question and answer method and system based on a domain knowledge graph, which can comprehensively, quickly and accurately find specific register bit function information and encoding.
[0005] The present invention provides a method for questioning and answering STM32 bit functions based on a domain knowledge graph, comprising the following steps:
[0006] S1. Construct triples of peripherals, registers, and each bit of register related information, and store the triples in Neo4j to form a visual domain knowledge graph;
[0007] S2. Use the pre-trained model mBERT+BILSTM+S-Attention+CRF to perform entity recognition on STM32-related questions raised by users;
[0008] S3. Use Python language to link with the domain knowledge graph, and use Cypher language to query the knowledge graph, obtain the candidate search path, find the answer to the question and return it to the user.
[0009] The present invention provides a method for question-answering STM32 bit functions based on a domain knowledge graph, which organizes the information of STM32 peripherals and registers to form a huge semantic network, and uses a pre-trained model and knowledge graph related technologies to realize question-answering. By converting Chinese expressions into codes of register bit functions in the form of question-answering, knowledge information in the field can be found more comprehensively, quickly and accurately.
[0010] The technical solution further optimized as the present invention is as follows:
[0011] In step S1, the STM32 domain knowledge graph is constructed based on the PDF table data extracted by the tool Tabula and the EXCEL table formed by manual sorting and supplementation. The specific construction method is as follows:
[0012] S1.1, table extraction, use the open source project Tabula to detect and extract table information from standard tables in PDF documents;
[0013] S1.2, manual construction, manual correction and supplement of complex tables containing nested data;
[0014] S1.3. Construct entity-relationship-entity triples according to the register-related manual provided by ST, and store the triples in the Neo4j database to form the STM32 knowledge graph.
[0015] In the above step 1.1, the open source project Tabula is used to extract table information from the PDF version of the register manual provided by ST. The table information includes the English and Chinese names of the specific bits of the register, as well as the specific explanations of the bits. The extracted table data is cleaned, and the extracted data is filled into the EXCEL table according to certain rules to construct a knowledge graph, and the register address information is manually supplemented;
[0016] In the above step S1.3, the entities in the domain knowledge graph include peripherals, registers and information of each bit of the registers, and the entity attributes include the addresses of the peripherals and registers, the reset values, the readability and writability of each bit of the registers and the specific functional description of each bit; the relationships in the domain knowledge graph are the relationship between peripherals and registers, the relationship between registers and each bit of the registers, and the relationship between specific bits of registers and specific functions.
[0017] In step S2, the main data sources of user questions in the named entity recognition model include questions raised by users on the website and questions formed according to the template embedded entity;
[0018] Classify and automatically label various STM32-related user questions, clean the question data obtained from the website, perform BIO annotation on all question data, automatically label the data using Python language, and establish the entity recognition model mBERT+BILSTM+S-Attention+CRF.
[0019] In the above step S2, data expansion is performed after automatic annotation of the data, and the pre-trained model GPT-2 is used to retain the entity-label of the annotated questions, and then the question template is used to form a new question, and the new question is BIO annotated according to the retained entities and labels; the question sentences annotated with BIO labels are used to train the named entity recognition model mBERT+BILSTM+S-Attention+CRF to identify entities in user questions.
[0020] The named entity recognition model mBERT+BILSTM+S-Attention+CRF includes four network layers, including an mBERT layer, a BILSTM layer (Bidirectional Long Short-Term Memory), an S-Attention layer (simplified attention mechanism layer) and a CRF layer; wherein the mBERT layer is used to vectorize the input natural language question encoding to obtain an input vector sequence; the BILSTM layer is used to extract sentence semantic features from the input vector sequence; the S-Attention layer is used to extract semantic features again from the vector sequence after the sentence semantic features are extracted by the BILSTM layer; the CRF layer is used to generate a final label sequence after combining the features extracted by the mBERT layer, the BILSTM layer and the S-Attention layer.
[0021] The operation of the above named entity recognition model is as follows:
[0022] The input natural language question is mapped into n vectors through the mBERT layer, where n is the length of the natural language question. The input sequence is represented as X = (x 1 ,x 2 ,…,x n ), x n Represents the nth vector of the input sentence, and the output is H = (h 1 ,h 2 ,…,h i ), h i is the state vector of the i-th hidden layer. H is used as the input of the BILSTM layer to obtain the forward hidden state output is the forward nth hidden layer state vector of the BILSTM layer, and the backward hidden state output is the state vector of the nth hidden layer in the backward direction of the BILSTM layer, and the forward and backward outputs are combined to obtain h t is the output of the BILSTM layer at time step t, which consists of the forward hidden state output and the backward hidden state output, and is the output h of the BILSTM layer. t Apply Dropout to get H dropout = Dropout(h t ), Dropout(h t ) By randomly discarding the output of some neurons, the model can be prevented from overfitting, thereby improving the generalization ability of the model. dropout is h after applying Dropout t , the output H dropout As the input of S-Attention layer;
[0023] S-Attention is expressed as S-Attention(Q, K, V), where Q, K, V are query, key, and value matrices. The calculation process of Q, K, V is:
[0024] Q=W Q H dropout , K=W K H dropout , V=W V H dropout
[0025] Among them, W Q The input feature matrix H dropout Converted to the weight matrix of the query matrix Q, W K The input feature matrix H dropout Convert the weight matrix W to the key matrix K V The input feature matrix H dropout Convert the weight matrix to the value matrix V;
[0026] The attention score is calculated as:
[0027]
[0028] Where S is the score matrix, a is the rank approximation factor, and each vector has dimension d x , d k It is a scaling factor, which is used to scale the dot product results of the query matrix Q and the key matrix K when calculating the attention score to avoid the dot product value being too large in high-dimensional space, which may lead to the gradient vanishing or gradient exploding problem of the softmax function;
[0029] Construct a mask matrix M for the top-k scores. For each row i of the mask matrix M, if the score S corresponding to column j is ij is one of the first K largest values in the row, then M ij =1, otherwise M ij =0,M ij It is a component element of the mask matrix M, which is used to identify which positions belong to the top-k scores, thereby obtaining the sparse score matrix S′, S′=S⊙M; where ⊙ represents the Hadamard product, which is the multiplication of the corresponding elements of the two matrices. The score matrix S and the mask matrix M are multiplied element by element to obtain the sparse score matrix S′ that only retains the top-k scores;
[0030] Applying sparse Softmax calculation output is:
[0031]
[0032] Among them, S′ ij It is the element of the sparsity score matrix S′. After being processed by the mask matrix M, only the top-k score elements are retained and other elements are kept as zero. S′ il It is an element of the sparsity score matrix S′, indicating that it belongs to TopKIndices i The value of the element at row i of column index l, TopKIndices i Represents the set of top k indexes with the highest scores in the i-th row of the score matrix S, Softmax(S′) ij It means applying the Softmax function to the i-th row of the sparse matrix S′ and calculating the probability distribution of each element j;
[0033] The final output of the S-Attention layer is:
[0034] A = Softmax(S′)
[0035] H=AV
[0036] H′=HW O
[0037] Where A is the attention weight matrix, which is obtained by applying the Softmax function to the sparse score matrix S′, representing the attention relationship between the elements in the input sequence, V is the value matrix, H is the product of the attention matrix A and the value matrix V, which is the value vector after the attention weight is applied, represented as the output of the attention mechanism, and H′ is the matrix H and the linear transformation matrix W O The product of represents the output after linear transformation, is the linear output transformation matrix, Represents the dimensional space of linear transformation matrices;
[0038] Finally, the question features are converted into the final label sequence in the CRF layer, and the input sequence X and label sequence Y are scored by the CRF layer. The CRF layer scoring function is:
[0039]
[0040] in, represents the emission fraction of the i-th position; Represents the label y i Move to label y i+1 transfer score;
[0041] The conditional probability of maximizing the correct label sequence in CRF is:
[0042]
[0043] Among them, exp(score(X,Y)) is the score index of a specific label sequence, ∑ y′ exp(score(X,Y′)) is the sum of the score exponents of all possible label sequences;
[0044] The CRF loss function output by the BILSTM layer is:
[0045]
[0046] in, is the logarithmic denominator normalized over all possible label sequences, is the mark y at position i i ′, indicating that the model predicts label y at position i i ′ confidence level, From the mark y′ i Transfer to label y′ i+1 The transition score represents the transition probability between tags;
[0047] By minimizing the intuitive loss function, the trained model is better able to predict sequence labels. The emission score at each time step is provided by the BILSTM layer, while the transfer score between labels is provided by the CRF layer.
[0048] The scoring function after combining BILSTM and S-Attention is:
[0049]
[0050] in, Represents the emission score after combining BILSTM and S-Attention;
[0051] The loss function combining BILSTM and S-Attention is:
[0052]
[0053] Then the final loss of the model is the sum of the two losses:
[0054] L total =L BILSTM_CRF +L combined_CRF .
[0055] In step S3, entity linking uses Python statements to link with the constructed domain knowledge graph, identifies the entities in the user's question and then links the knowledge graph, uses the Cypher language to query the knowledge graph, finds the target node, target attribute and target relationship, and then finds the relevant nodes and attributes of the relevant nodes, and returns the answer to the question to the user.
[0056] The present invention also provides a STM32 bit function question-answering system based on a domain knowledge graph, comprising:
[0057] Data extraction and sorting module, used to extract and sort the STM32 register related manuals as the source of knowledge graph data;
[0058] The data processing module is used to identify entities in question sentences using the named entity recognition model mBERT+BILSTM+S-Attention+CRF;
[0059] The data search module is used to query the target entity, target attributes and target relationships as well as the attributes and relationships of related node entities, find the answer to the target node entity question, and return the answer to the user's question.
[0060] The present invention constructs entity-relationship-entity triples according to the register-related manual provided by ST, and stores the triples in the database Neo4j to form an STM32 knowledge graph; classifies and automatically annotates various STM32 related question sentences, establishes an entity recognition model mBERT+BILSTM+S-Attention+CRF; and performs entity linking to obtain the problem target entity, target attribute and target relationship using Cypher language query, and finds the answer to the question according to the information retrieval candidate path and returns it to the user. The present invention can provide specific register bit functions by means of intelligent question and answer, reducing the time to find the manual.
[0061] Compared with the prior art, the advantages of the present invention are:
[0062] (1) Using the open source project Tabula to extract table information greatly reduces the time of manually sorting and summarizing register-related information, and can quickly sort out register-related information to build a knowledge graph;
[0063] (2) Automatically label questions and sentences, and perform BIO labeling on questions and template question sentences raised by users on the website; classify questions and sentences, and automatically label them according to categories; and use the GPT-2 model to expand question sentence data, reducing the time for manual BIO labeling and data expansion;
[0064] (3) Improve the named entity recognition model and use the mBERT+BILSTM+S-Attention+CRF model to identify entities in user questions. Compared with the four models of HMM, BILSTM, BILSTM+CRF, and mBERT+BILSTM+CRF, this model has improved precision, recall, and F1 value. It can better identify entities in questions, thereby linking the knowledge graph to query target nodes, node attributes, and node relationships to provide answers to questions. BRIEF DESCRIPTION OF THE DRAWINGS
[0065] Figure 1 This is the architecture diagram of the STM32-bit function question-answering system based on the domain knowledge graph of the present invention;
[0066] Figure 2 It is a visualization display diagram of the local knowledge graph of STM32 peripherals and registers in the present invention;
[0067] Figure 3 The GUI interface diagram of the question data expansion and automatic annotation in the present invention;
[0068] Figure 4 This is a schematic diagram of the structure of the mBERT+BILSTM+S-Attention+CRF entity recognition model in the present invention;
[0069] Figure 5 Schematic diagram of the structure of the S-Attention model in the present invention;
[0070] Figure 6 This is a question query result diagram in the present invention. DETAILED DESCRIPTION
[0071] The technical solution of the present invention is further described in detail below in conjunction with the accompanying drawings: This embodiment is implemented on the premise of the technical solution of the present invention, and a detailed implementation method and specific operation process are given, but the protection authority of the present invention is not limited to the following embodiments.
[0072] The present invention provides a method for questioning and answering STM32 bit functions based on a domain knowledge graph, comprising:
[0073] Obtain the register-related manual information provided by ST to build a domain knowledge graph; the knowledge graph is constructed by triplets of peripherals, registers, and each bit of register-related information and stored in Neo4j to form a visual domain knowledge graph;
[0074] Named entity recognition uses the pre-trained model mBERT+BILSTM+S-Attention+CRF to perform entity recognition on STM32-related questions raised by users;
[0075] The entity linking part uses Python language to link with the domain knowledge graph, and uses Cypher language to query the knowledge graph to obtain the candidate retrieval path to find the answer to the question and return it to the user.
[0076] In practical applications, the STM32 neighborhood knowledge graph is extracted from tables and manually constructed. The specific construction method is as follows:
[0077] The table extraction method uses the open source project Tabula to detect and extract table information from standard tables in PDF documents. However, for complex tables containing nested data, there is data loss when using this table extraction method to extract table information, so it is necessary to use manual construction to correct and supplement it. Finally, according to the register-related manual provided by ST, the entity-relationship-entity triples are constructed and stored in the database Neo4j to form the STM32 knowledge graph.
[0078] The open source project Tabula is used to extract table information from the PDF version of the register-related instruction manual officially provided by ST. The table information includes the English and Chinese names of the specific bits of the register, as well as the specific explanations of the bits. The extracted table data is cleaned up, and the extracted data is filled into the EXCEL table according to certain rules to construct a knowledge graph, and the register address information is manually supplemented.
[0079] The domain knowledge graph is constructed based on PDF table data extracted by the tool Tabula and manually sorted and supplemented by EXCEL tables. The entities in the domain knowledge graph are peripherals, registers, and information about each bit of the register. The entity attributes include the address of the peripherals and registers, the reset value, the readability and writability of each bit of the register, and the specific functional description of each bit. The relationship is the relationship between the head entity and the tail entity. In the present invention, it refers to the relationship between the peripheral and the register, the relationship between the register and each bit of the register, and the relationship between the specific bit of the register and the specific function.
[0080] In actual use, the main data sources of user questions in the named entity recognition pre-training model include questions asked by users on the website and questions formed based on template embedded entities;
[0081] In actual use, user questions are classified and labeled, question data obtained from the website is cleaned, all questions are labeled with BIO, and the data is automatically labeled using Python programs. Question files can be uploaded and automatically labeled after being processed by the program to establish the entity recognition model mBERT+BILSTM+S-Attention+CRF.
[0082] In practical applications, in addition to automatic data annotation, data expansion is also required. The pre-trained model GPT-2 is used to retain the entity-label of the provided annotated questions, and then the question template is used to form new questions. The new questions are annotated with BIO based on the retained entities and labels. The named entity recognition model mBERT+BILSTM+S-Attention+CRF is trained using the BIO labeled question sentences to perform entity recognition on user questions.
[0083] In practical applications, entity linking uses Python language to link with the constructed domain knowledge graph. After identifying the entities in the user's question, the knowledge graph is linked. The knowledge graph is queried using Cypher language. After finding the target node, target attribute and target relationship, the relevant nodes and attributes of the relevant nodes are found, and the answer to the question is returned to the user.
[0084] The present invention uses the above method to construct a knowledge graph for STM32 single-chip microcomputer to establish a question-answering system. The system includes: data extraction and sorting, which is used to extract and sort the STM32 register related instruction manual as the source of knowledge graph data; data processing, which is used to identify the entity of the question sentence using the named entity recognition model mBERT+BILSTM+S-Attention+CRF; data search, which is used to query the target entity, target attribute and target relationship and the attributes and relationship of the related node entity, find the answer to the target node entity question, and return the answer to the user question.
[0085] Example 1
[0086] A STM32 bit function question answering system based on domain knowledge graph, such as Figure 1 As shown in the figure, first, the STM32 knowledge graph is established according to the register-related instruction manual; secondly, the data collection and annotation time of the training model is reduced; finally, the entity recognition model mBERT+BILSTM+S-Attention+CRF is used to perform entity recognition on user questions, and then the knowledge graph is linked, and the Cypher language is used to query the target entity, attribute, relationship and related node information to find the answer to the question and return it to the user.
[0087] Methods for constructing and storing knowledge graphs:
[0088] There are two main methods for constructing knowledge graphs: bottom-up and top-down. The top-down construction method is to extract pattern information from unstructured data sources to construct knowledge graphs. This method is suitable for constructing common sense knowledge graphs. The bottom-up construction method is to extract pattern information from structured and semi-structured data sources and add it to the knowledge base. It requires defining entities first. This method is more suitable for constructing professional knowledge graphs.
[0089] This embodiment is constructed using a bottom-up approach, obtaining entity information of peripherals and registers from the data description manual officially provided by ST, and establishing triples through the open source project Tabula and manual supplementary annotations, which are stored in the Neo4j knowledge base.
[0090] In the dataset for constructing the knowledge graph, triples are usually expressed in the form of (Entity head , Relation, Entity tail ) and (Entity, attribute, value). This embodiment adopts (Entity head , Relation, Entity tail ), where Entity head As the head entity, Entity tail As the tail entity, Relation is the relationship between two entities. The peripherals, registers, and the specific name information of each bit of the registers and the functional information are taken as entities. The peripherals contain many registers, so "include" is a relationship, so the triple form is ("RCC", "include", "RCC_CR"). Table 1 shows some entities and their attributes, Table 2 shows some relationships between entities and their relationship attributes, and Table 3 shows some examples of knowledge graph triple data sets.
[0091] Table 1 Some entities and their attributes
[0092]
[0093]
[0094] Table 2 Some relations and their relation attributes
[0095]
[0096] Table 3 Some examples of triplet datasets
[0097]
[0098] The above entities, relationships, and attributes are stored in the Neo4j database as follows Figure 2The local knowledge graph of STM32 peripherals and registers is shown. As can be seen from the three tables above, an entity contains multiple attributes, that is, a node contains multiple information about peripherals, registers, or specific bits of registers, that is, the knowledge graph can summarize scattered knowledge into the graph to increase the information contained in each node. The relationship attributes between nodes represent the encoding of the specific bit function change.
[0099] Question sentence input data processing part:
[0100] For the input sentence, the pre-trained model is used to first train the entity question contained in it, and then the model is used to perform entity recognition on the question. When processing the training data, first of all, the diversity of questions should be enriched. Questions raised by users are crawled from various websites, and then the data is cleaned to retain useful data. Questions are manually written through templates and entity information. Secondly, the acquired data is classified and BIO labeled, and automatic labeling is achieved through Python program according to the category. Sentences are labeled using BIO labeling method. "B-xxx" represents the beginning part of the entity, "I-xxx" represents the middle part of the entity, and "xxx" represents the category. For example: What is O / what O / is O / backup B-BKP_DRx / copy I-BKP_DRx / data I-BKP_DRx / storage I-BKP_DRx / 1I-BKP_DRx / ? O (for clarity, " / " is a line break). Then use the CPT-2 model to expand the data. The data expansion GUI interface is as follows: Figure 3 shown.
[0101] The entity recognition model is mBERT+BILSTM+S-Attention+CRF, which contains four network layers. The structure diagram is as follows Figure 4 The S-Attention used in this embodiment is a simplified version of the self-attention layer, and its structure is shown in Figure 5 As shown in the figure, by combining dimensionality reduction technology and sparsity principle, the efficiency of processing long sequences is significantly improved. Compared with the traditional standard self-attention mechanism, this method reduces the complexity and memory usage of the model while maintaining the performance of the model. It reduces unnecessary calculations while maintaining key information, and achieves fast and efficient operations in the case of long sequences.
[0102] This embodiment involves the specific information of peripherals, registers, and each bit of the register, including its English name and Chinese name, so the mBERT (bert-base-multilingual-cased) pre-training model is selected. The mBERT model uses a multilingual pre-training data set, covering 104 different languages. This layer of network vectorizes the input natural language question encoding, extracts the sentence semantic features of the input vector sequence through the BILSTM layer, and the bidirectional long short-term memory network saves the forward feature information of the input sequence and the backward feature information of the input sequence. After the BILSTM layer extracts the sentence semantic features, the S-Attention is a simplified version of the self-attention layer to extract the semantic features again. Finally, after combining the features of the mBERT layer, the BILSTM layer and the simplified version of the self-attention mechanism, CRF converts these features into the final label sequence.
[0103] The input natural language question is mapped into n vectors through the mBERT layer, where n is the length of the natural language question. The input sequence is represented as X = (x 1 ,x 2 ,…,x n ), x n Represents the nth vector of the input sentence, and the output is H = (h 1 ,h 2 ,…,h i ), h i is the state vector of the i-th hidden layer. H is used as the input of the BILSTM layer to obtain the forward hidden state output is the forward nth hidden layer state vector of the BILSTM layer, and the backward hidden state output is the state vector of the nth hidden layer in the backward direction of the BILSTM layer, and the forward and backward outputs are combined to obtain h t is the output of the BILSTM layer at time step t, which consists of the forward hidden state output and the backward hidden state output, and is the output h of the BILSTM layer. t Apply Dropout to get H dropout = Dropout(h t ), Dropout(h t ) By randomly discarding the output of some neurons, the model can be prevented from overfitting, thereby improving the generalization ability of the model. dropout is h after applying Dropout t , the output H dropoutAs the input of the simplified self-attention layer. S-Attention is expressed as S-Attention(Q, K, V), where Q, K, V are query, key and value matrices, and the calculation process of Q, K, V is: Q = W Q H dropout , K=W K H dropout , V=W V H dropout , where W Q The input feature matrix H dropout Converted to the weight matrix of the query matrix Q, W K The input feature matrix H dropout Convert the weight matrix W to the key matrix K V The input feature matrix H dropout The weight matrix converted to the value matrix V.
[0104] The attention score is calculated as:
[0105]
[0106] Where S is the score matrix, a is the rank approximation factor, and each vector has dimension d x , d k It is a scaling factor used to scale the dot product of the query matrix Q and the key matrix K when calculating the attention score to avoid excessive dot product values in high-dimensional space, which may lead to the gradient vanishing or gradient exploding problem of the softmax function.
[0107] Construct a mask matrix M for the top-k scores. For each row i of the mask matrix M, if the score S corresponding to column j is ij is one of the first K largest values in the row, then M ij =1, otherwise M ij =0,M ij The constituent elements of the mask matrix M are used to identify which positions belong to the top-k scores, thereby obtaining the sparsity score matrix S′, S′=S⊙M, where ⊙ represents the Hadamard product, which is the multiplication of the corresponding elements of the two matrices. The score matrix S and the mask matrix M are multiplied element by element to obtain the sparse score matrix S′ that only retains the top-k scores.
[0108] Applying sparse Softmax calculation output is:
[0109]
[0110] Among them, S′ ij It is the element of the sparsity score matrix S′. After being processed by the mask matrix M, only the top-k score elements are retained and other elements are kept as zero. S′ ilIt is an element of the sparsity score matrix S′, indicating that it belongs to TopKIndices i The value of the element at row i of column index l, TopKIndices i Represents the set of top k indexes with the highest scores in the i-th row of the score matrix S, Softmax(S′) ij It means applying the Softmax function to the i-th row of the sparse matrix S′ and calculating the probability distribution of each element j.
[0111] The final output of the S-Attention layer is:
[0112] A = Softmax(S′)
[0113] H=AV
[0114] H′=HW O
[0115] Where A is the attention weight matrix, which is obtained by applying the Softmax function to the sparse score matrix S′, representing the attention relationship between the elements in the input sequence, V is the value matrix, H is the product of the attention matrix A and the value matrix V, which is the value vector after the attention weight is applied, represented as the output of the attention mechanism, and H′ is the matrix H and the linear transformation matrix W O The product of represents the output after linear transformation, is the linear output transformation matrix, Represents the dimensional space of linear transformation matrices.
[0116] Finally, the sentence features are converted into the final label sequence in the CRF layer, and the input sequence X and label sequence Y are scored by the CRF layer. The CRF layer scoring function is:
[0117]
[0118] in, Represents the emission fraction of the i-th position, representing the input X generating label y at the i-th position i score. Represents the label y i Move to label y i+1 transfer score.
[0119] The conditional probability of maximizing the correct label sequence in CRF is:
[0120]
[0121] The numerator is the score index of a specific tag sequence, and the denominator is the sum of the score indexes of all possible tag sequences.
[0122] The CRF loss function of BILSTM output is:
[0123]
[0124] Among them, the third is the logarithmic denominator normalized over all possible label sequences, is the mark y at position i i ′, indicating that the model predicts label y at position i i ′ confidence level, From the marker y i 'Transfer to label y' i+1 The transition score represents the transition probability between tags.
[0125] By minimizing an intuitive loss function, the model can be trained to better predict sequence labels, with the emission score at each time step provided by the BILSTM layer and the transition score between labels provided by the CRF layer.
[0126] The scoring function after combining BILSTM and S-Attention is:
[0127]
[0128] in, It represents the emission score after combining BILSTM and S-Attention, and represents the score of the character generating label yi at the i-th position in the input sequence.
[0129] The loss function combining BILSTM and S-Attention is:
[0130]
[0131] The final loss of the model is the sum of two losses:
[0132] L total =L BILSTM_CRF +L combined_CRF
[0133] Calculating the two losses helps optimize the feature representations at different levels during the training process, allowing the model to better learn features at different levels during the training process, using BILSTM to capture bidirectional dependencies, and further combining S-Attention to capture more complex attention relationships, improving the final annotation accuracy, and thus improving the overall performance of the model.
[0134] Entity linking consists of three parts: Neo4j database connection, entity candidate query, and related node query.
[0135] First, connect to the local Neo4j database through the py2neo library to complete the database connection. Secondly, the user asks a question, and the question input data processing part performs entity recognition on the question, classifies the recognized entities, and if the entity is expressed in Chinese and English, dynamically queries the knowledge graph according to the attribute symbol. If the entity is expressed in full English, dynamically queries the knowledge graph according to the attribute name to obtain candidate entities that match the recognized entity. Finally, through undirected relationship queries, find the nodes related to the recognized entity and the attributes and relationships of the related nodes. And return the query results. Figure 6 The question query result graph is shown.
[0136] If the question raised by the user cannot correctly identify the entity content in entity recognition, the natural language question entered by the user cannot be converted into a legal Cpyher query statement. When the user enters content that is not related to STM32, it will return "Sorry, the system cannot understand your content temporarily, please re-enter".
[0137] The above description is only a specific implementation of the present invention, but the protection scope of the present invention is not limited thereto. Any person familiar with the technology can understand and think of any changes or substitutions within the technical scope disclosed by the present invention, which should be included in the scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A method for answering questions about STM32 bit functions based on domain knowledge graph, It is characterized in that The following steps are involved: S1. Construct triples of peripherals, registers, and each bit of register related information, and store the triples in Neo4j to form a visual domain knowledge graph; S2. Use the pre-trained model mBERT+BILSTM+S-Attention+CRF to perform entity recognition on STM32-related questions raised by users; S3. Use Python language to link with the domain knowledge graph, and use Cypher language to query the knowledge graph, obtain the candidate search path, find the answer to the question and return it to the user.
2. According to claim 1, a method for question-answering STM32 functions based on domain knowledge graph, It is characterized in that In step S1, the STM32 domain knowledge graph is constructed based on the PDF table data extracted by the tool Tabula and the EXCEL table formed by manual sorting and supplementation. The specific construction method is as follows: S1.1, table extraction, use the open source project Tabula to detect and extract table information from standard tables in PDF documents; S1.2, manual construction, manual correction and supplement of complex tables containing nested data; S1.
3. Construct entity-relationship-entity triples according to the register-related manual provided by ST, and store the triples in the Neo4j database to form the STM32 knowledge graph.
3. According to claim 2, a method for questioning and answering STM32 functions based on domain knowledge graph, It is characterized in that In step 1.1, the open source project Tabula is used to extract table information from the PDF version of the register-related manual provided by ST. The table information includes the English and Chinese names of the specific bits of the register, as well as the specific explanations of the bits. The extracted table data is cleaned, and the extracted data is filled into an EXCEL table according to certain rules to construct a knowledge graph, and the register address information is manually supplemented; In step S1.3, the entities in the domain knowledge graph include peripherals, registers, and information about each bit of the registers, and the entity attributes include the addresses of the peripherals and registers, reset values, and read / write capabilities of each bit of the registers and the specific functional description of each bit; the relationships in the domain knowledge graph are the relationship between peripherals and registers, the relationship between registers and each bit of the registers, and the relationship between specific bits of registers and specific functions.
4. According to claim 1, a method for questioning and answering STM32 functions based on domain knowledge graph, It is characterized in that In step S2, the main data sources of user questions in the named entity recognition model include questions raised by users on the website and questions formed according to the template embedded entity; Classify and automatically label various STM32-related user questions, clean the question data obtained from the website, perform BIO annotation on all question data, automatically label the data using Python language, and establish the entity recognition model mBERT+BILSTM+S-Attention+CRF.
5. According to claim 4, a method for questioning and answering STM32 bit functions based on domain knowledge graph, It is characterized in that In step S2, in addition to automatically annotating the data, data expansion is performed, and the pre-trained model GPT-2 is used to retain the entity-label of the annotated question, and then a new question is formed using the question template, and the new question is annotated with BIO according to the retained entity and label; Use question sentences annotated with BIO tags to train the named entity recognition model mBERT+BILSTM+S-Attention+CRF to identify entities in user questions.
6. According to claim 5, a method for questioning and answering STM32 bit functions based on domain knowledge graph, It is characterized in that The named entity recognition model mBERT+BILSTM+S-Attention+CRF includes four network layers, including an mBERT layer, a BILSTM layer, an S-Attention layer, and a CRF layer; wherein the mBERT layer is used to vectorize the input natural language question encoding to obtain an input vector sequence; and the BILSTM layer is used to extract sentence semantic features from the input vector sequence. The S-Attention layer is used to extract semantic features again from the vector sequence after the BILSTM layer extracts the semantic features of the sentence; The CRF layer is used to generate the final label sequence by combining the features extracted by the mBERT layer, BILSTM layer, and S-Attention layer.
7. According to claim 6, a method for questioning and answering STM32 bit functions based on domain knowledge graph, It is characterized in that The operation of the named entity recognition model is as follows: The input natural language question is mapped into n vectors through the mBERT layer, where n is the length of the natural language question. The input sequence is represented as X = (x 1 ,x 2 ,L,xn),x n Represents the nth vector of the input sentence, and the output is H = (h 1 ,h 2 ,L,h i ), h i is the i-th hidden layer state vector; H is used as the input of the BILSTM layer to obtain the forward hidden state output is the forward nth hidden layer state vector of the BILSTM layer, and the backward hidden state output is the state vector of the nth hidden layer in the backward direction of the BILSTM layer, and the forward and backward outputs are combined to obtain h t is the output of the BILSTM layer at time step t, and the output h of the BILSTM layer t Apply Dropout to get H dropout = Dropout(h t ), Dropout(h t ) By randomly discarding the output of some neurons, H dropout is h after applying Dropout t , the output H dropout As the input of S-Attention layer; S-Attention is expressed as S-Attention(Q, K, V), where Q, K, V are query, key, and value matrices. The calculation process of Q, K, V is: Q=W Q H dropout ,K=W K H dropout ,V=W V H dropout Among them, W Q The input feature matrix H dropout Converted to the weight matrix of the query matrix Q, W K The input feature matrix H dropout Convert the weight matrix W to the key matrix K V The input feature matrix H dropout Convert the weight matrix to the value matrix V; The attention score is calculated as: Where S is the score matrix, a is the rank approximation factor, and each vector has dimension d x , d k is the scaling factor; Construct a mask matrix M for the top-k scores. For each row i of the mask matrix M, if the score S corresponding to column j is ij is one of the first K largest values in the row, then M ij =1, otherwise M ij =0,M ij It is the constituent element of the mask matrix M, thus obtaining the sparsity score matrix S¢, S¢=SeM; Applying sparse Softmax calculation output is: Among them, S i ¢ j is the element of the sparsity score matrix S¢, S i ¢ l is the element of the sparsity score matrix S¢, TopKIndices i Represents the set of top k indexes with the highest scores in the i-th row of the score matrix S, Softmax(S¢) ij Indicates applying the Softmax function to the i-th row of the sparse matrix S¢; The final output of the S-Attention layer is: A=Softmax(S¢) H=AV H¢=HW O Among them, A represents the attention relationship between the elements in the input sequence, H represents the output of the attention mechanism, and H¢ represents the output after linear transformation. is the linear output transformation matrix, Represents the dimensional space of linear transformation matrices; Finally, the question features are converted into the final label sequence in the CRF layer, and the input sequence X and label sequence Y are scored by the CRF layer. The CRF layer scoring function is: in, represents the emission fraction of the i-th position; Represents the label y i Move to label y i+1 transfer score; The conditional probability of maximizing the correct label sequence in CRF is: Among them, exp(score(X,Y)) is the score index of a specific label sequence, ∑ y¢ exp(score(X,Y¢)) is the sum of the score indices of all possible label sequences; The CRF loss function output by the BILSTM layer is: in, is the logarithmic denominator normalized over all possible label sequences, It means that the model predicts label y at position i i ¢ confidence level, represents the transition probability between markers; By minimizing the intuitive loss function, the trained model is better able to predict sequence labels. The emission score at each time step is provided by the BILSTM layer, while the transfer score between labels is provided by the CRF layer. The scoring function after combining BILSTM and S-Attention is: in, Represents the emission score after combining BILSTM and S-Attention; The loss function combining BILSTM and S-Attention is: Then the final loss of the model is the sum of the two losses: L total =L BILSTM_CRF +L combined_CRF .
8. According to claim 1, a method for questioning and answering STM32 bit functions based on domain knowledge graph, It is characterized in that In step S3, entity linking uses Python statements to link with the constructed domain knowledge graph, identifies the entities in the user's question and then links the knowledge graph, uses the Cypher language to query the knowledge graph, finds the target node, target attribute and target relationship, and then finds the relevant nodes and attributes of the relevant nodes, and returns the answer to the question to the user.
9. A question-answering system based on the method according to any one of claims 1 to 7, It is characterized in that include: Data extraction and sorting module, used to extract and sort the STM32 register related manuals as the source of knowledge graph data; The data processing module is used to identify entities in question sentences using the named entity recognition model mBERT+BILSTM+S-Attention+CRF; The data search module is used to query the target entity, target attributes and target relationships as well as the attributes and relationships of related node entities, find the answer to the target node entity question, and return the answer to the user's question.
Citation Information
Cited By
Scientific research instrument intelligent duplicate checking method based on multi-dimensional parameter decoupling and dynamic weighting
CN121210615A
