Weight calculation method and device of attention network, electronic equipment and medium
By dividing the image attention weight matrix into blocks and calculating the associated attention matrix, the target query attention vector is determined, which solves the problem of insufficient global information extraction and feature retention caused by the single blocking method in Swim-Transformer, and improves the attention network. Accuracy and efficiency of weight calculation.
Patent Information
- Application Number
- CN202410491747.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-23
- Publication Date
- 2024-08-23
AI Technical Summary
Due to the single block division method of Swim-Transformer in the attention neural network weight calculation, the global information of the image cannot be effectively extracted, and the features of overlapping pixels are not well preserved, which affects the effectiveness and accuracy of the weight results.
By dividing the image attention weight matrix input into the attention network, multiple sub-matrices are obtained, and the associated attention matrix is calculated based on the query sub-matrix and key sub-matrix, thereby determining the target query attention vector, reducing the amount of calculation and improving calculation efficiency. and accuracy of results.
It improves the accuracy and comprehensiveness of attention network weight calculation, reduces the amount of calculation, improves calculation efficiency, and can better retain image feature information.
Smart Images

Figure CN118537577A
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of image processing technology, and in particular to a method, device, electronic device and medium for calculating weights of an attention network. Background Art
[0002] Swim-Transformer is a hierarchical Transformer structure designed specifically for the visual field. The input of Swim-Transformer is an image. Through different block division methods, multiple non-overlapping small blocks (also called patches) are obtained. Each patch captures the local information of the image, thereby reducing the amount of attention calculation. For example, the image is divided into 4 patches by the quartering method, and the image is divided into 9 patches by the ninth method. The pixels between these 4 patches do not overlap with each other, and the pixels between the 9 patches do not overlap with each other. However, the pixels between the 4 patches and the 9 patches overlap, thereby compensating for the pixel information at the division point. However, when calculating the weights of the attention neural network in this way, the pixel content of each patch is fixed because the block division method is still too simple, so that the global information of the image cannot be effectively extracted, and the features of the overlapping parts of the pixels cannot be well retained, which affects the effectiveness and accuracy of the weight results of the attention neural network. Summary of the invention
[0003] In order to solve the above technical problems or at least partially solve the above technical problems, the present disclosure provides a weight calculation method, device, electronic device and medium for an attention network, which can improve the effectiveness and accuracy of calculating the weights of the attention neural network.
[0004] In order to achieve the above objectives, the technical solutions provided by the embodiments of the present disclosure are as follows:
[0005] In a first aspect, the present disclosure provides a weight calculation method for an attention network, comprising:
[0006] The attention weight matrix of the image input to the attention network is divided into blocks to obtain multiple sub-matrices; wherein the attention weight matrix includes: a query matrix, a key matrix and a value matrix, and correspondingly, the sub-matrices include: a query sub-matrix, a key sub-matrix and a value sub-matrix;
[0007] Based on the query sub-matrix and the key sub-matrix, calculate the associated attention matrix corresponding to the query sub-matrix;
[0008] Based on the associated attention matrix, determine the target query attention vector from the query sub-matrix;
[0009] According to the target query attention vector and the corresponding value vector, the weight result of the attention network is calculated.
[0010] As an optional implementation of the embodiment of the present disclosure, the attention weight matrix of the image input to the attention network is divided into blocks to obtain multiple sub-matrices, including:
[0011] The attention weight matrix is divided into blocks based on a window of a preset size to obtain a plurality of first sub-matrices;
[0012] After offsetting the center position of the window of a preset size, the attention weight matrix is subdivided into blocks to obtain multiple second sub-matrices.
[0013] As an optional implementation of the embodiment of the present disclosure, based on the query sub-matrix and the key sub-matrix, calculating the associated attention matrix corresponding to the query sub-matrix includes:
[0014] Perform attention calculation on the query sub-matrix and the key sub-matrix to obtain the query sub-matrix and the key attention matrix;
[0015] Each query attention vector in the query sub-matrix is calculated with all the key attention vectors in the key attention matrix to obtain the associated attention matrix corresponding to the query sub-matrix.
[0016] As an optional implementation of the embodiment of the present disclosure, attention calculation is performed on the query sub-matrix and the key sub-matrix to obtain the query sub-matrix and the key attention matrix, including:
[0017] Through the convolution operation and the binary classification function, the query sub-matrix is paid attention to obtain the query sub-matrix, and the key sub-matrix is paid attention to obtain the key attention matrix.
[0018] As an optional implementation of the embodiment of the present disclosure, each query attention vector in the query submatrix is calculated with all the key attention vectors in the key attention matrix to obtain a correlation attention matrix corresponding to the query submatrix, including:
[0019] For each query sub-matrix corresponding to the query sub-matrix, perform the following operations until all query attention vectors in the query sub-matrix are traversed:
[0020] The query attention vector is dot-multiplied and normalized with the key attention vector corresponding to the corresponding key submatrix to obtain the association attention matrix between the query attention vector and other query attention vectors.
[0021] As an optional implementation of the embodiment of the present disclosure, the correlation attention matrix includes correlation attention values; the correlation attention values correspond to the query attention vector;
[0022] Based on the associated attention matrix, the target query attention vector is determined from the query sub-matrix, including:
[0023] Select N query attention vectors with larger associated attention values as target query attention vectors, where N is a positive integer.
[0024] In a second aspect, the present disclosure provides a weight calculation device for an attention network, the device comprising:
[0025] A block division module is used to divide the attention weight matrix of the image input into the attention network into blocks to obtain multiple sub-matrices; wherein the attention weight matrix includes: a query matrix, a key matrix and a value matrix, and correspondingly, the sub-matrices include: a query sub-matrix, a key sub-matrix and a value sub-matrix;
[0026] A first calculation module, used for calculating a correlation attention matrix corresponding to the query submatrix based on the query submatrix and the key submatrix;
[0027] A determination module, configured to determine a target query attention vector from a query sub-matrix based on a correlation attention matrix;
[0028] The second calculation module is used to calculate the weight result of the attention network according to the target query attention vector and the corresponding value vector.
[0029] As an optional implementation of the embodiment of the present disclosure, the blocking module is specifically used to: block the attention weight matrix based on a window of a preset size to obtain multiple first sub-matrices; after offsetting the center position of the window of the preset size, re-block the attention weight matrix to obtain multiple second sub-matrices.
[0030] As an optional implementation of the embodiment of the present disclosure, the first calculation module is specifically used to: perform attention calculation on the query submatrix and the key submatrix to obtain the query submatrix and the key attention matrix; calculate each query attention vector in the query submatrix with all the key attention vectors in the key attention matrix to obtain the associated attention matrix corresponding to the query submatrix.
[0031] As an optional implementation of the embodiment of the present disclosure, the first computing module is specifically used to: perform attention calculation on the query submatrix through convolution operation and binary classification function to obtain the query submatrix, and perform attention calculation on the key submatrix to obtain the key attention matrix.
[0032] As an optional implementation of the embodiment of the present disclosure, the first calculation module is specifically used to: perform the following operations on the query sub-matrix corresponding to each query sub-matrix until all query attention vectors in the query sub-matrix are traversed:
[0033] The query attention vector is dot-multiplied and normalized with the key attention vector corresponding to the corresponding key submatrix to obtain the association attention matrix between the query attention vector and other query attention vectors.
[0034] As an optional implementation of the embodiment of the present disclosure, the association attention matrix includes association attention values; the association attention values correspond to query attention vectors; and a determination module is specifically used to: select N query attention vectors with larger association attention values as target query attention vectors, where N is a positive integer.
[0035] In a third aspect, the present disclosure provides an electronic device comprising: a processor, a memory, and a computer program stored in the memory and executable on the processor, wherein when the computer program is executed by the processor, the computer program implements the weight calculation method of the attention network as described in the first aspect or any one of its optional embodiments.
[0036] In a fourth aspect, the present disclosure provides a computer-readable storage medium, comprising: a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the weight calculation method of the attention network as described in the first aspect or any one of its optional embodiments is implemented.
[0037] In a fifth aspect, the present disclosure provides a computer program product, including: the computer program product includes a computer program, and when the computer program runs on a computer, the computer implements the weight calculation method of the attention network as described in the first aspect or any one of its optional embodiments.
[0038] Compared with the prior art, the technical solution provided by the embodiments of the present disclosure has the following advantages:
[0039] The disclosed embodiment provides a weight calculation method, device, electronic device and medium of an attention network, wherein the method first divides the attention weight matrix of the image input to the attention network into blocks to obtain multiple sub-matrices, wherein the attention weight matrix includes a query matrix, a key matrix and a value matrix, and the corresponding sub-matrices include: a query sub-matrix, a key sub-matrix and a value sub-matrix; based on the query sub-matrix and the key sub-matrix, the associated attention matrix corresponding to the query sub-matrix is calculated, and then according to the associated attention matrix, the target attention vector is determined from the query sub-matrix, so as to calculate the weight result of the attention network according to the target query attention vector and the corresponding value vector. In this way, in the disclosed embodiment, the attention weight matrix of the image is divided into blocks, and the obtained multiple sub-matrices can contain more comprehensive image feature information to improve the accuracy and comprehensiveness of the weight calculation results; according to the associated attention matrix calculated by the query sub-matrix and the key sub-matrix, the target query attention vector is selected from the query sub-matrix, thereby avoiding attention calculation for all query attention vectors, reducing the amount of calculation and improving the calculation efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.
[0041] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0042] Figure 1 This is a schematic diagram of the block division method of Swim-Transform;
[0043] Figure 2 A flowchart of a weight calculation method for an attention network provided in an embodiment of the present disclosure;
[0044] Figure 3A Schematic diagram of the block division method provided in the embodiment of the present disclosure Figure 1 ;
[0045] Figure 3B Schematic diagram of the block division method provided in the embodiment of the present disclosure Figure 2 ;
[0046] Figure 4 A schematic diagram of attention calculation provided for an embodiment of the present disclosure;
[0047] Figure 5 A schematic diagram of the structure of a weight calculation device for an attention network provided in an embodiment of the present disclosure;
[0048] Figure 6 The present invention is a schematic diagram of the structure of an electronic device according to an embodiment of the present disclosure. DETAILED DESCRIPTION
[0049] In order to more clearly understand the above-mentioned objectives, features and advantages of the present disclosure, the scheme of the present disclosure will be further described below. It should be noted that the embodiments of the present disclosure and the features in the embodiments can be combined with each other without conflict.
[0050] In the following description, many specific details are set forth to facilitate a full understanding of the present disclosure, but the present disclosure may also be implemented in other ways different from those described herein; it is obvious that the embodiments in the specification are only part of the embodiments of the present disclosure, rather than all of the embodiments.
[0051] Currently, Swim-Transform uses two block division methods, such as Figure 1 As shown, Figure 1The figure is a schematic diagram of the Swim-Transform block division method. The original image consists of 64 elements. One method is to divide the image into 4 patches by the quartering method. Each patch contains 16 elements, and the pixels of the 4 patches do not overlap. The other method is to divide the image into 9 patches by the ninth method. The 9 patches contain 4, 8, 4, 8, 16, 8, 4, 8 and 4 elements respectively, and the pixels of the 9 patches do not overlap. However, the pixels between the 4 patches and the 9 patches overlap, thus compensating for the pixel information at the division point.
[0052] However, this attention calculation method is too simple in its block division method and fixes the pixel content of each patch, so that the global information of the image cannot be effectively extracted, and the features of the overlapping parts of the pixels cannot be well preserved, which affects the effectiveness and accuracy of the weight calculation results of the attention neural network. The attention calculation involves all pixel contents of all patches, which is computationally intensive and has poor results.
[0053] In order to solve some or all of the technical problems existing in the related art, the embodiment of the present disclosure provides an interactive method, device, electronic device, medium and vehicle of a vehicle terminal, wherein the method first divides the attention weight matrix of the image input into the attention network into blocks to obtain multiple sub-matrices, wherein the attention weight matrix includes a query matrix, a key matrix and a value matrix, and the corresponding sub-matrices include: a query sub-matrix, a key sub-matrix and a value sub-matrix; based on the query sub-matrix and the key sub-matrix, the associated attention matrix corresponding to the query sub-matrix is calculated, and then according to the associated attention matrix, the target attention vector is determined from the query sub-matrix, so as to calculate the weight result of the attention network according to the target query attention vector and the corresponding value vector. In this way, in the embodiment of the present disclosure, the attention weight matrix of the image is divided into blocks, and the obtained multiple sub-matrices can contain more comprehensive image feature information to improve the accuracy and comprehensiveness of the weight calculation result; according to the associated attention matrix calculated by the query sub-matrix and the key sub-matrix, the target query attention vector is selected from the query sub-matrix, thereby avoiding attention calculation for all query attention vectors, reducing the amount of calculation and improving the calculation efficiency.
[0054] A weight calculation method for an attention network provided in an embodiment of the present disclosure can be implemented by a weight calculation device or an electronic device of the attention network, and the electronic device includes but is not limited to a vehicle terminal, a server, a personal computer, a laptop computer, a tablet computer, a smart phone, etc. The operating system of the electronic device may include Android, a mobile operating system (iOS) developed by Apple, an operating system (Windows) developed by Microsoft Corporation of the United States, etc., and the embodiments of the present disclosure are not limited to this. The electronic device can be operated alone to implement the present disclosure, or it can be connected to the network and implement the present disclosure through interactive operations with other computer devices in the network. Among them, the network in which the electronic device is located includes but is not limited to the Internet, a wide area network, a metropolitan area network, a local area network, a virtual private network (VPN) network, etc.
[0055] It should be noted that the protection scope of the weight calculation method of the attention network described in the embodiment of the present disclosure is not limited to the execution order of the steps listed in this embodiment, and all solutions implemented by adding, reducing or replacing steps in the prior art based on the principles of the present disclosure are included in the protection scope of the present disclosure.
[0056] like Figure 2 As shown, Figure 2 The present invention provides a flowchart of a method for calculating the weight of an attention network provided by an embodiment of the present invention. The method can be performed by a device for calculating the weight of an attention network, wherein the device can be implemented by software and / or hardware and can generally be integrated in an electronic device. Figure 2 As shown, the method mainly includes the following steps S201 to S204:
[0057] S201, dividing the attention weight matrix of the image input into the attention network into blocks to obtain multiple sub-matrices.
[0058] The attention network includes a self-attention network (Self-Attention) and a cross-attention network (Cross-Attention). The image is an input image of the attention network. The embodiment of the present disclosure inputs an image to the attention network to train the weight coefficient of the attention network, thereby improving the performance of the attention network.
[0059] Among them, the attention weight matrix includes: query matrix (Quest, Q), key matrix (Key, K), value matrix (Value, V); the attention weight matrix can be a self-attention weight matrix or a mutual attention weight matrix, which is not specifically limited in the present disclosure.
[0060] The multiple sub-matrices include: multiple query sub-matrices q corresponding to the query matrix, multiple key sub-matrices k corresponding to the key matrix, and multiple value sub-matrices v corresponding to the value matrix. The query sub-matrices do not overlap with each other, the key sub-matrices do not overlap with each other, and the value sub-matrices do not overlap with each other.
[0061] In some embodiments, the attention weight matrix of the image is divided into blocks to obtain multiple sub-matrices, including: dividing the attention weight matrices Q, K and V into sub-matrices of the same form and non-overlapping. The same form means that the number of rows and columns of the sub-matrices are the same. It can be understood that, corresponding to the image, the image is divided into blocks of the same size, and the image blocks do not overlap with each other.
[0062] Exemplarily, the size of the query matrix Q, key matrix K and value matrix V is 16*100*20, where 16 is the number of matrix channels, 100 is the matrix width, and 200 is the matrix height; after block division, 400 query sub-matrices q, 400 key sub-matrices k and 400 value sub-matrices v are obtained. The size of each query sub-matrix q, key sub-matrix k and value sub-matrix v is 16*5*10. It should be emphasized that if the sub-matrix edge is insufficient during block division, blank zero values are used to fill it.
[0063] In some embodiments, a multi-head blocking method is applied to block the attention weight matrix of the image, wherein the multi-head blocking method is to block according to different area divisions.
[0064] Optionally, first obtain the size of the attention weight matrix, and divide the attention weight matrix into blocks based on a window of a preset size to obtain multiple first sub-matrices. Exemplarily, the window of the preset size is 5*10 in size. Then, after offsetting the center position of the window of the preset size, the attention weight matrix is re-divided into blocks to obtain multiple second sub-matrices.
[0065] For example, Figure 3A As shown, Figure 3A Schematic diagram of the block division method provided in the embodiment of the present disclosure Figure 1 The size of the attention weight matrix is 100*200. The attention weight matrix is divided based on a 5*10 window to obtain 400 first sub-matrices ( Figure 3A The above is the first block division method, and the first sub-matrices do not overlap with each other. Figure 3B As shown, Figure 3B Schematic diagram of the block division method provided in the embodiment of the present disclosure Figure 2, and then offset the center position of the window, for example, offset 1 / 2 window to the right and 1 / 2 window downward, and divide the attention weight matrix based on the 5*10 window; it should be noted that if the window contains only part of the attention weight matrix, the insufficient positions are filled with blanks, that is, the vacancies are filled with 0 values, and 441 second sub-matrices are obtained by division. The above is the second blocking method, and the second sub-matrices do not overlap with each other.
[0066] It should be noted that the first sub-matrix and the second sub-matrix obtained by the first blocking method and the second method will overlap, so that in the subsequent attention calculation, the attention relationship between different matrix vectors can be paid attention to, so as to obtain more comprehensive feature information.
[0067] The above-mentioned embodiment divides the attention weight matrix of the image into blocks through a multi-head blocking method, which will divide different feature points into the same block, can include more comprehensive image features, and obtain multi-angle attention information; dividing the attention weight matrix into non-overlapping small blocks increases the diversity of weight calculation and improves the accuracy and comprehensiveness of the weight settlement results.
[0068] S202. Based on the query sub-matrix and the key sub-matrix, calculate the associated attention matrix corresponding to the query sub-matrix.
[0069] In some embodiments, in the process of executing step S202, attention calculation is first performed on the query submatrix and the key submatrix to obtain a query submatrix and a key attention matrix, and then each query attention vector in the query attention is calculated with all key attention vectors in the key attention matrix to obtain the associated attention matrix corresponding to the query submatrix.
[0070] Optionally, in the process of performing attention calculation on the query submatrix and the key submatrix to obtain the query submatrix and the key attention matrix, the query submatrix q and the key submatrix k can be specifically calculated by convolution operation and binary classification function to obtain the query submatrix q_a and the key attention matrix k_a. Among them, the binary classification can be a SigMoid function. The query submatrix q_a and the key attention matrix k_a can be single-channel, and exemplarily, the size of the query submatrix q_a and the key attention matrix k_a is 1*100*200.
[0071] Exemplarily, by performing 3*3 convolution, 1*1 convolution and independent operations on the sub-matrices of the query matrix and the key matrix, namely the query sub-matrix q and the key sub-matrix k, the attention value of each vector in the block is obtained, and the attention value is between 0 and 1. After that, each block is spliced according to the original position to obtain the query sub-matrix and the key attention matrix.
[0072] like Figure 4As shown, Figure 4 The schematic diagram of attention calculation provided for the embodiment of the present disclosure performs the same operation as follows on the query submatrix q and the key submatrix k: Here, the query submatrix q is calculated for attention to obtain the query submatrix q_a as an example: First, the query submatrix q is passed through 3*3 convolution and (3*3Conv) linear rectified function (Rectified Linear Unit, ReLU) to obtain the first convolution result, and the first convolution result is the same size as the query submatrix q; then the first convolution result and the query submatrix q are added element by element, and then through 1*1 convolution (1*1Conv) and S-type function (SigMoid function), channel fusion and feature dimensionality reduction are realized to obtain the query submatrix q_a. The specific operation of performing attention calculation on the key submatrix k to obtain the key attention matrix is similar to the above, and the present disclosure will not repeat it here.
[0073] The above embodiment performs attention calculation after block division, which can obtain the pixel attention of the entire image, and at the same time pays attention to the attention between pixels in the block. By using the relationship between nearby blocks as weights, it is possible to avoid mutual interference between blocks that are far apart, thereby reducing the amount of calculation.
[0074] In some embodiments, in the process of calculating each query attention vector in the query submatrix with all the key attention vectors in the key attention matrix to obtain the associated attention matrix corresponding to the query submatrix, the following operation is performed on the query submatrix corresponding to each query submatrix until all the query attention vectors in the query submatrix are traversed: the query attention vector is dot-multiplied and normalized (SoftMax) with the key attention vector corresponding to the corresponding key submatrix to obtain the associated attention matrix q_t of the query attention vector and other query attention vectors.
[0075] Exemplarily, following the previous example, each query attention vector in the query submatrix q_a is multiplied with each key attention vector in the corresponding key attention matrix k_a, and SoftMax processing is performed to obtain the associated attention values between 20,000 query attention vectors and the corresponding key attention vectors (50). The size of the associated attention matrix q_t is 50*100*200.
[0076] In the above embodiment, the query attention vector and the key attention vector are operated to obtain the associated attention matrix, so as to filter the query attention vector using the associated attention matrix later. It should be noted that the key matrix, key submatrix key attention matrix and attention vector do not participate in the final weight calculation.
[0077] S203. Determine a target query attention vector from the query sub-matrix based on the associated attention matrix.
[0078] The correlation attention matrix includes correlation attention values, which correspond to the query attention vectors and are used to indicate the degree of correlation between the query attention vectors and other query attention vectors. The higher the correlation attention value, the higher the correlation between the two query attention vectors.
[0079] The number of target query attention vectors can be multiple, for example, N, N = 5; illustratively, the target query attention vector q j The size of is 16*1*1. The target query attention vector is the query attention vector corresponding to the N associated attention vectors with the largest associated attention in the associated attention matrix. Exemplarily, the size of the target query attention vector is 50*1*1.
[0080] In some embodiments, in the process of determining a target query attention vector from a query sub-matrix based on a correlation attention matrix, N query attention vectors with larger key attention values are selected as target query attention vectors, where N is a positive integer.
[0081] Optionally, all query attention vectors in the query submatrix are sorted according to the associated attention values. The associated attention values can be sorted in descending order to determine the top N associated attention values, and then the N query attention vectors corresponding to these N associated attention values are determined, and these N query attention vectors are used as target query attention vectors.
[0082] The above embodiment selects N target query attention vectors with higher correlation levels for limited attention calculation according to the degree of correlation, so that the focus is on the query attention vectors with close relationship. The calculated weight result is more accurate and avoids attention calculation for all query attention vectors, reducing the amount of calculation.
[0083] S204. Calculate the weight result of the attention network based on the target query attention vector and the corresponding value vector.
[0084] Among them, the weight result corresponds to each target query attention vector, and each weight matrix can be expressed as a Z value matrix.
[0085] In some embodiments, the target query attention vector and the corresponding value vector are dot-multiplied, and the result of SoftMax processing of the associated attention values corresponding to the N target query attention vectors is used as the weight, and the weighted sum of the dot-multiplication result is obtained to obtain the weight result of the attention network.
[0086] Exemplarily, the weight value z is calculated according to the following formula (1):
[0087]
[0088] Among them, q j is the target query attention vector, v i Yes j The corresponding value vector, p i It is the result of SoftMax processing of N target query attention vectors.
[0089] The aforementioned method selects N target query attention vectors based on the degree of association, and then performs attention calculation with the corresponding value vectors, thereby avoiding the need to perform attention calculation with all query attention vectors and all value vectors, reducing the amount of calculation. In addition, the target query attention vector is a query attention vector with a larger associated attention value. Using these target query attention vectors for attention calculation can improve the accuracy of the calculated weight results.
[0090] In some embodiments, after executing step S204, a weight result of the attention network corresponding to a blocking method is obtained. Based on the multi-head blocking method, after executing the aforementioned steps S201 to S204 respectively, weight results corresponding to different blocking methods are obtained, and then these weight results are superimposed according to the channel, and then the channel dimension reduction is performed after passing through a 1*1 convolution layer to obtain the weight result of this attention network. The weight result is more accurate and can improve the performance of the attention network.
[0091] In summary, the embodiment of the present disclosure provides a weight calculation method for an attention network, which first divides the attention weight matrix of the image input to the attention network into blocks to obtain multiple sub-matrices, wherein the attention weight matrix includes a query matrix, a key matrix and a value matrix, and the corresponding sub-matrices include: a query sub-matrix, a key sub-matrix and a value sub-matrix; based on the query sub-matrix and the key sub-matrix, the associated attention matrix corresponding to the query sub-matrix is calculated, and then according to the associated attention matrix, the target attention vector is determined from the query sub-matrix, so as to calculate the weight result of the attention network according to the target query attention vector and the corresponding value vector. In this way, in the embodiment of the present disclosure, the attention weight matrix of the image is divided into blocks, and the obtained multiple sub-matrices can contain more comprehensive image feature information to improve the accuracy and comprehensiveness of the weight calculation results; according to the associated attention matrix calculated from the query sub-matrix and the key sub-matrix, the target query attention vector is selected from the query sub-matrix, thereby avoiding attention calculation for all query attention vectors, reducing the amount of calculation and improving the calculation efficiency.
[0092] like Figure 5 As shown, Figure 5 A schematic diagram of a weight calculation device for an attention network provided in an embodiment of the present disclosure, the device comprising:
[0093] The block division module 501 is used to divide the attention weight matrix of the image input into the attention network into blocks to obtain multiple sub-matrices; wherein the attention weight matrix includes: a query matrix, a key matrix and a value matrix, and correspondingly, the sub-matrices include: a query sub-matrix, a key sub-matrix and a value sub-matrix;
[0094] A first calculation module 502, configured to calculate a correlation attention matrix corresponding to the query submatrix based on the query submatrix and the key submatrix;
[0095] A determination module 503, configured to determine a target query attention vector from the query sub-matrix based on the associated attention matrix;
[0096] The second calculation module 504 is used to calculate the weight result of the attention network according to the target query attention vector and the corresponding value vector.
[0097] As an optional implementation of the embodiment of the present disclosure, the blocking module 501 is specifically used to: block the attention weight matrix based on a window of a preset size to obtain multiple first sub-matrices; after offsetting the center position of the window of the preset size, re-block the attention weight matrix to obtain multiple second sub-matrices.
[0098] As an optional implementation of the embodiment of the present disclosure, the first calculation module 502 is specifically used to: perform attention calculation on the query submatrix and the key submatrix to obtain the query submatrix and the key attention matrix; calculate each query attention vector in the query submatrix with all the key attention vectors in the key attention matrix to obtain the associated attention matrix corresponding to the query submatrix.
[0099] As an optional implementation of the embodiment of the present disclosure, the first computing module 502 is specifically used to: perform attention calculation on the query submatrix through convolution operation and binary classification function to obtain the query submatrix, and perform attention calculation on the key submatrix to obtain the key attention matrix.
[0100] As an optional implementation of the embodiment of the present disclosure, the first calculation module 502 is specifically used to: perform the following operations on the query sub-matrix corresponding to each query sub-matrix until all query attention vectors in the query sub-matrix are traversed:
[0101] The query attention vector is dot-multiplied and normalized with the key attention vector corresponding to the corresponding key submatrix to obtain the association attention matrix between the query attention vector and other query attention vectors.
[0102] As an optional implementation of the embodiment of the present disclosure, the associated attention matrix includes associated attention values; the associated attention values correspond to query attention vectors; the determination module 503 is specifically used to: select N query attention vectors with larger associated attention values as target query attention vectors, where N is a positive integer.
[0103] like Figure 6 As shown, Figure 6 This is a schematic diagram of the structure of an electronic device according to an embodiment of the present disclosure, and the electronic device includes: a processor 601, a memory 602, and a computer program stored in the memory 602 and executable on the processor 601, and when the computer program is executed by the processor 601, each process of the weight calculation method of the attention network in the above method embodiment is implemented. And the same technical effect can be achieved, and it will not be repeated here to avoid repetition.
[0104] The embodiments of the present disclosure provide a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, each process of the weight calculation method of the attention network in the above-mentioned method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be described here.
[0105] The computer readable storage medium may be a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.
[0106] The embodiments of the present disclosure provide a computer program product, which stores a computer program. When the computer program is executed by a processor, each process of the weight calculation method of the attention network in the above method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be described here.
[0107] Those skilled in the art will appreciate that the embodiments of the present disclosure may be provided as methods, systems, or computer program products. Therefore, the present disclosure may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Furthermore, the present disclosure may take the form of a computer program product implemented on one or more computer-usable storage media containing computer-usable program code.
[0108] In several embodiments provided by the present disclosure, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely schematic. For example, the flowcharts and block diagrams in the accompanying drawings show the possible architecture, functions and operations of the devices, methods and computer program products according to multiple embodiments of the present disclosure. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of a code, and the module, a program segment or a part of a code contains one or more executable instructions for implementing the specified logical functions. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in an order different from that marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of boxes in the block diagram and / or flowchart, can be implemented with a dedicated hardware-based system that performs a specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.
[0109] In the present disclosure, the processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.
[0110] In the present disclosure, memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. Memory is an example of a computer-readable medium.
[0111] In the present disclosure, computer-readable media include permanent and non-permanent, removable and non-removable storage media. Storage media can be implemented by any method or technology to store information, and the information can be computer-readable instructions, data structures, modules of programs or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, read-only compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. According to the definition in this article, computer-readable media does not include temporary computer-readable media (transitory media), such as modulated data signals and carrier waves.
[0112] It should be noted that, in this article, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0113] The above are only specific embodiments of the present disclosure, so that those skilled in the art can understand or implement the present disclosure. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein can be implemented in other embodiments without departing from the spirit or scope of the present disclosure. Therefore, the present disclosure will not be limited to these embodiments herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A weight calculation method for attention network, It is characterized in that include: The attention weight matrix of the image input to the attention network is divided into blocks to obtain a plurality of sub-matrices; wherein the attention weight matrix includes: a query matrix, a key matrix and a value matrix, and correspondingly, the sub-matrices include: a query sub-matrix, a key sub-matrix and a value sub-matrix; Based on the query submatrix and the key submatrix, calculating a correlation attention matrix corresponding to the query submatrix; Determining a target query attention vector from the query sub-matrix based on the associated attention matrix; According to the target query attention vector and the corresponding value vector, the weight result of the attention network is calculated.
2. The method according to claim 1, It is characterized in that The attention weight matrix of the image input to the attention network is divided into blocks to obtain multiple sub-matrices, including: Dividing the attention weight matrix into blocks based on a window of a preset size to obtain a plurality of first sub-matrices; After shifting the center position of the window of the preset size, the attention weight matrix is sub-divided into blocks to obtain a plurality of second sub-matrices.
3. The method according to claim 1, It is characterized in that The calculating, based on the query submatrix and the key submatrix, a correlation attention matrix corresponding to the query submatrix includes: Performing attention calculation on the query submatrix and the key submatrix to obtain a query submatrix and a key attention matrix; Each query attention vector in the query sub-matrix is calculated with all the key attention vectors in the key attention matrix to obtain the associated attention matrix corresponding to the query sub-matrix.
4. The method according to claim 3, It is characterized in that The performing attention calculation on the query sub-matrix and the key sub-matrix to obtain a query sub-matrix and a key attention matrix includes: Through convolution operation and binary classification function, attention calculation is performed on the query submatrix to obtain the query submatrix, and attention calculation is performed on the key submatrix to obtain the key attention matrix.
5. The method according to claim 3, It is characterized in that The step of calculating each query attention vector in the query submatrix with all key attention vectors in the key attention matrix to obtain a correlation attention matrix corresponding to the query submatrix includes: For each query sub-matrix corresponding to the query sub-matrix, perform the following operations until all query attention vectors in the query sub-matrix are traversed: The query attention vector is dot-multiplied and normalized with the key attention vector corresponding to the corresponding key submatrix to obtain the associated attention matrix between the query attention vector and other query attention vectors.
6. The method according to claim 1, It is characterized in that The association attention matrix includes an association attention value; the association attention value corresponds to the query attention vector; The determining a target query attention vector from the query sub-matrix based on the associated attention matrix includes: Select N query attention vectors with larger associated attention values as the target query attention vectors, where N is a positive integer.
7. A weight calculation device for an attention network, It is characterized in that include: A block division module is used to divide the attention weight matrix of the image input into the attention network into blocks to obtain multiple sub-matrices; wherein the attention weight matrix includes: a query matrix, a key matrix and a value matrix, and correspondingly, the sub-matrices include: a query sub-matrix, a key sub-matrix and a value sub-matrix; A first calculation module, configured to calculate a correlation attention matrix corresponding to the query submatrix based on the query submatrix and the key submatrix; A determination module, configured to determine a target query attention vector from the query sub-matrix based on the associated attention matrix; The second calculation module is used to calculate the weight result of the attention network according to the target query attention vector and the corresponding value vector.
8. An electronic device, It is characterized in that include: A processor, a memory, and a computer program stored in the memory and executable on the processor, wherein the computer program, when executed by the processor, implements the weight calculation method of the attention network as described in any one of claims 1 to 6.
9. A computer-readable storage medium, It is characterized in that include: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the weight calculation method of the attention network according to any one of claims 1 to 6 is implemented.
10. A computer program product, It is characterized in that include: The computer program product includes a computer program, and when the computer program is run on a computer, the computer implements the weight calculation method of the attention network according to any one of claims 1 to 6.
Citation Information
Patent Citations
Classroom action recognition method based on dual-scale space-time block mutual attention
CN113408343A
Construction method of multivariable long-sequence time sequence prediction model based on Transform framework
CN114519469A
Storage and calculation integrated chip and operation method thereof
CN116882462A
Visual Transform reasoning optimization method and device
CN117893881A
Cited By
Low-complexity Transform attention module prediction method and device
CN119312839A
Large model self-attention optimization method, device, equipment and medium
CN119443183A
A Method, Device, Equipment and Medium for Optimizing Self-Attention of Large Models
CN119443183B
Data splicing method, computing device, storage medium and program product
CN121211382A
Attention calculation method and device, equipment, storage medium and program product
CN121327313A
