Hash code generation method and device, computer device, and storage medium

By generating and sampling initial hash codes, a more accurate semantic hash model is established, which solves the problem of inaccurate posterior probability distribution calculation and improves the accuracy of hash model and text matching.

CN113821527BActive Publication Date: 2026-02-06TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202110733009.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-06-30
Publication Date
2026-02-06
Estimated Expiration
2041-11-13

AI Technical Summary

Technical Problem

In existing semantic hashing models, the posterior probability distribution is difficult to calculate accurately, which leads to a decrease in the accuracy of the hashing model and affects the precision of text matching.

Method used

By generating an initial hash code and sampling it, a posterior probability distribution closer to the real situation is obtained. The initial hash code is generated using an encoder to accelerate the sampling process and establish a more accurate semantic hash model.

Benefits of technology

It improves the accuracy of semantic hashing models, enhances the efficiency and accuracy of text matching, and generates higher quality hash codes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113821527B_ABST
    Figure CN113821527B_ABST
Patent Text Reader

Abstract

The application discloses a hash code generation method and device, computer equipment and a storage medium, and relates to the technical field of artificial intelligence. The method comprises the following steps: generating an initial hash code of a target text; establishing a semantic hash model of the target text according to a joint probability distribution of the target text and the initial hash code; sampling the initial hash code according to a sampling probability distribution of the initial hash code to obtain a posterior probability distribution of the semantic hash model, wherein the sampling probability distribution is a conditional probability distribution determined according to the initial hash code and the target text; and determining a corrected hash code corresponding to the target text according to the posterior probability distribution. The application provides a method for obtaining a corrected hash code that can better reflect the discrete representation of a target text by sampling an initial hash code, improves the quality of generated hash codes, and improves the accuracy of a semantic hash model.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of artificial intelligence, and particularly relates to a hash code generation method and device, a computer device and a storage medium. BACKGROUND

[0002] In many application scenarios of data retrieval, a target text most matching a retrieval condition needs to be determined by similarity calculation on the text. A semantic hashing algorithm in artificial intelligence technology maps the text into a hash code, and similarity calculation on the text is realized by calculating a Hamming distance between the hash codes. The semantic hashing algorithm establishes a semantic hashing model by a joint probability distribution between the text and the hash code, and obtains the hash code corresponding to the text by calculating a posterior probability distribution of the semantic hashing model.

[0003] The posterior probability distribution in the semantic hashing model is usually difficult to calculate, and in the related technology, the posterior probability distribution of the semantic hashing model is approximately calculated by variational inference, but there is a large gap from the real posterior probability distribution, resulting in a decrease in the accuracy of the semantic hashing model.

[0004] How to better fit the posterior probability distribution in the semantic hashing model and improve the precision of the semantic hashing model is a problem to be solved. SUMMARY

[0005] Embodiments of the present application provide a hash code generation method and device, a computer device and a storage medium, which can improve the accuracy of the semantic hashing model. The technical solution is as follows:

[0006] On the one hand, a hash code generation method is provided, and the method comprises:

[0007] generating an initial hash code of a target text;

[0008] establishing a semantic hashing model of the target text according to a joint probability distribution of the target text and the initial hash code;

[0009] sampling the initial hash code according to a sampling probability distribution of the initial hash code to obtain a posterior probability distribution of the semantic hashing model, the sampling probability distribution being a probability distribution determined according to the initial hash code and the target text;

[0010] determining a corrected hash code corresponding to the target text according to the posterior probability distribution.

[0011] On the other hand, a text matching method is provided, and the method comprises:

[0012] obtaining a first text input;

[0013] obtaining a first hash code of the first text and a second hash code of a candidate text, the first hash code and the second hash code being both corrected hash codes obtained by sampling an initial hash code according to a sampling probability distribution of the initial hash code, the initial hash code being a hash code generated based on the first text and the candidate text, and the sampling probability distribution being a conditional probability distribution determined according to the initial hash code and the candidate text; and

[0014] determining the candidate text corresponding to the second hash code with the smallest Hamming distance from the first hash code as a second text, the second text being a text most similar to the first text.

[0015] In another aspect, a hash code generation apparatus is provided, and the apparatus includes:

[0016] a generation module configured to generate an initial hash code of a target text;

[0017] a model establishment module configured to establish a semantic hash model of the target text according to a joint probability distribution of the target text and the initial hash code;

[0018] a sampling module configured to sample the initial hash code according to a sampling probability distribution of the initial hash code to obtain a posterior probability distribution of the semantic hash model, the sampling probability distribution being a probability distribution determined according to the initial hash code and the target text;

[0019] a determination module configured to determine a corrected hash code corresponding to the target text according to the posterior probability distribution.

[0020] In another aspect, a text matching apparatus is provided, and the apparatus includes:

[0021] an acquisition module configured to acquire a first text input;

[0022] The acquisition module is further configured to obtain a first hash code of the first text and a second hash code of a candidate text, the first hash code and the second hash code being both corrected hash codes obtained by sampling an initial hash code according to a sampling probability distribution of the initial hash code, the initial hash code being a hash code generated based on the first text and the candidate text, and the sampling probability distribution being a conditional probability distribution determined according to the initial hash code and the candidate text.

[0023] a calculation module configured to calculate a Hamming distance between the first hash code and the second hash code;

[0024] The determining module determines a candidate text corresponding to the second hash code with the minimum Hamming distance to the first hash code as a second text, the second text being a text most similar to the first text.

[0025] In another aspect, a computer device is provided, which includes a processor and a memory, and the memory stores at least one program, which is loaded and executed by the processor to implement the hash code generation method or the text matching method according to any of the embodiments of the present application.

[0026] In another aspect, a computer readable storage medium is provided, which stores computer instructions, and the computer instructions are loaded and executed by a processor to implement the hash code generation method or the text matching method according to various aspects of the present application.

[0027] In another aspect, a computer program product is provided, which includes computer instructions stored in a computer readable storage medium. A processor of a computer device reads the computer instructions from the computer readable storage medium, and the processor executes the computer instructions to enable the computer device to perform the hash code generation method or the text matching method.

[0028] The technical scheme provided by the embodiments of the present application has at least the following beneficial effects:

[0029] By generating the initial hash code of the target text, and then sampling the initial hash code, the posterior probability distribution of the semantic hash model that is more in line with the real situation can be obtained, so that the corrected hash code that is more accurate can be obtained, the hash code with optimized generation quality can be generated, and the precision of the semantic hash model is improved. BRIEF DESCRIPTION OF DRAWINGS

[0030] In order to more clearly illustrate the technical scheme in the embodiments of the present application, the drawings needed in the embodiment description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor.

[0031] Figure 1 is an application scenario diagram of text matching using hash code provided by an exemplary embodiment of the present application;

[0032] Figure 2 is a structural block diagram of a computer system provided by an exemplary embodiment of the present application;

[0033] Figure 3 is a flowchart of a hash code generation method provided by an exemplary embodiment of the present application;

[0034] Figure 4 is a flow chart of a method for generating hash codes according to an example embodiment of the present application;

[0035] Figure 5 is a flow chart of a text matching method according to an example embodiment of the present application;

[0036] Figure 6 is a flow chart of a text matching method applied in a retrieval scenario according to an example embodiment of the present application;

[0037] Figure 7 is a schematic diagram of a data model according to an example embodiment of the present application;

[0038] Figure 8 is a schematic diagram of a two-stage training according to an example embodiment of the present application;

[0039] Figure 9 is a flow chart of a method for generating hash codes according to an example embodiment of the present application;

[0040] Figure 10 is a structural block diagram of a hash code generation device according to an example embodiment of the present application;

[0041] Figure 11 is a structural block diagram of a text matching device according to an example embodiment of the present application;

[0042] Figure 12 is a structural block diagram of a computer device according to an example embodiment of the present application. DETAILED DESCRIPTION

[0043] In order to make the purpose, technical scheme and advantages of the present application clearer, the embodiments of the present application will be further described in detail below with reference to the drawings.

[0044] Firstly, the terms involved in the embodiments of the present application are introduced:

[0045] Semantic Hashing: refers to a hash algorithm that maps a spatial vector from a high-dimensional space to a low-dimensional Hamming space while preserving the similarity of the spatial vector in the original high-dimensional space, so that the Hamming distance of the spatial vector in the low-dimensional Hamming space can reflect the similarity of the original spatial vector.

[0046] Hamming Distance: the number of different bit values in two strings is called Hamming distance. XOR operation can be performed on two strings, and the number of results of 1 is counted, which is the Hamming distance.

[0047] Variational Inference: a technique for approximating the difficult integrals that arise in Bayesian inference and machine learning.

[0048] Similarity search: also known as nearest neighbor search, the purpose is to find the most similar item in a large-scale database according to the user query statement. It has important application in large-scale data retrieval.

[0049] The hash code generation method and text matching method provided by the embodiment of the application relate to the field of artificial intelligence technology. The following describes the artificial intelligence related technology.

[0050] Artificial intelligence (AI) is to use digital computers or digital computer controlled machine simulation, extension and expansion of human intelligence, perception of the environment, knowledge acquisition and use of knowledge to obtain the best results of theory, method, technology and application system. In other words, artificial intelligence is a comprehensive technology of computer science, which tries to understand the essence of intelligence and produce a new intelligent machine that can react in a similar way to human intelligence. Artificial intelligence is to study the design principles and implementation methods of various intelligent machines, so that the machine has the functions of perception, reasoning and decision-making.

[0051] Artificial intelligence technology is a comprehensive discipline, involving a wide range of fields, both hardware and software level technology. Artificial intelligence basic technology generally includes such as sensors, special artificial intelligence chips, cloud computing, distributed storage, big data processing technology, operation / interaction system, mechatronics and other technologies. Artificial intelligence software technology mainly includes computer vision technology, speech processing technology, natural language processing technology and machine learning / deep learning and other several major directions.

[0052] Natural language processing (NLP) is an important direction in the field of computer science and artificial intelligence. It studies the various theories and methods that can realize effective communication between man and computer with natural language. Natural language processing is a science that integrates linguistics, computer science and mathematics. Therefore, the research in this field will involve natural language, that is, the language used in daily life, so it has a close relationship with the study of linguistics. Natural language processing technology usually includes text processing, semantic understanding, machine translation, robot question and answer, knowledge graph and other technologies.

[0053] Machine Learning (ML) is a multi-disciplinary subject involving probability theory, statistics, approximation theory, convex analysis, algorithmic complexity theory, etc. It is a specialized study of how computers simulate or implement human learning behavior to acquire new knowledge or skills, reorganize existing knowledge structure, and continuously improve their performance. Machine learning is the core of artificial intelligence and the fundamental approach to making computers intelligent. It is applied in various fields of artificial intelligence. Machine learning and deep learning generally include artificial neural networks, belief networks, reinforcement learning, transfer learning, inductive learning, and teaching learning.

[0054] Autonomous driving technology generally includes high-precision map, environment perception, behavior decision, path planning, motion control, etc. Autonomous driving technology has a wide application prospect.

[0055] With the research and progress of artificial intelligence technology, artificial intelligence technology is researched and applied in many fields, such as common smart home, smart wearable device, virtual assistant, smart speaker, smart marketing, unmanned driving, autonomous driving, unmanned aerial vehicle, robot, intelligent medical treatment, intelligent customer service, etc. It is believed that with the development of technology, artificial intelligence technology will be applied in more fields and play an increasingly important role.

[0056] The scheme provided by the embodiments of the present application relates to natural language processing Chinese text processing, semantic understanding and machine learning technologies of artificial intelligence, which is specifically explained by the following embodiments.

[0057] Figure 1 As shown is an interface schematic diagram of a medical health system. A user accesses the medical health system through a terminal, and a search function box 32 and other function controls, such as a medical insurance electronic voucher control, an electronic health card control, a doctor Q&A control, a registration control, etc. are displayed on a user graphical interface 30 of the medical health system. The user inputs a retrieval keyword or a retrieval sentence in the search function box 32 to retrieve relevant information.

[0058] For example, after the user inputs a retrieval sentence “Is it better to eat fruits or drink freshly squeezed fruit juice?” in the search function box 32 and clicks “Search”, the terminal jumps to a retrieval result interface 40. The retrieval result interface 40 displays articles with high similarity to the retrieval sentence, and the order of the articles is arranged from high to low according to the similarity. For example, Figure 1 As shown in the above, the article with the highest similarity to the retrieval sentence is the article 42 arranged at the top, and the articles 44 and 46 with decreasing similarity are arranged downward, etc.

[0059] The article retrieval process described above can be completed by a semantic hashing method. In brief, the input retrieval keyword or retrieval sentence is mapped to a hash code, the similarity between the hash code and the hash code corresponding to the articles in the database is represented by Hamming distance, the article with the smallest Hamming distance is determined as the article with the highest similarity to the retrieval keyword or retrieval sentence, and the retrieval result page is returned.

[0060] Similarly, the method of retrieving relevant articles by inputting keywords or sentences using the semantic hashing method can also be used for answer retrieval in a question and answer engine, drug recommendation in guided diagnosis, etc.

[0061] The following embodiments will specifically describe the hash code generation method and article matching method involved in this process.

[0062] Figure 2 A structural block diagram of a computer system provided by an example embodiment of the present application is shown. The computer system 100 includes a terminal 120 and a server 140.

[0063] The terminal 120 can be at least one of a smartphone, a game console, a desktop computer, a tablet computer, an e-book reader, an MP3 (Moving Picture Experts Group Audio Layer III) player, an MP4 (Moving Picture Experts Group Audio Layer IV) player, a laptop computer, a smart robot, and a self-service payment device. The terminal 120 is installed and runs an application supporting text retrieval, for example, the application can be a system application, a search engine, a news push application, a shopping application, a social application, etc.

[0064] The terminal 120 is connected to the server 140 through a wireless network or a wired network.

[0065] The server 140 includes at least one of a server, multiple servers, a cloud computing platform, and a virtualization center. The server 140 is used to provide background services for clients. Optionally, the server 140 undertakes the main computing work, and the terminal undertakes the secondary computing work; or the server 140 undertakes the secondary computing work, and the terminal undertakes the main computing work; or the server 140 and the terminal adopt a distributed computing architecture for collaborative computing.

[0066] Those skilled in the art can know that the number of the terminal 120 and the server 140 can be more or less. For example, the terminal 120 and the server 140 can be only one, or the terminal 120 and the server 140 can be dozens or hundreds, or more, and the number of the terminal or the server and the type of the equipment are not limited in the embodiments of the present application.

[0067] In the related art, a semantic hash model of a document is established, and a posterior probability distribution of a hidden variable (i.e., a hash code) in the semantic hash model corresponding to the document is fitted by a method of variational inference. However, the posterior probability distribution of the hidden variable obtained by the variational inference is greatly different from the real posterior probability distribution, which leads to a decrease in the accuracy of the semantic hash model and a poor quality of the obtained hash code, and thus the precision of the document matching is insufficient. In the present application, the posterior probability distribution of the hidden variable closer to the real situation is obtained by sampling the initial hash code, and thus the hash code reflecting the discrete representation of the text is obtained, and the efficiency and the accuracy of the text matching are improved.

[0068] Figure 3 A flowchart of a method for generating a hash code provided by an example embodiment of the present application is shown, and the method is applied to a terminal 120 or a server 140 as shown. Figure 2 As shown in the figure, the method comprises the following steps. Figure 3 As shown in the figure, the method comprises the following steps.

[0069] Step 320: generating an initial hash code of the target text.

[0070] The target text can be any form of text with text content, for example, the target text can be a sentence, a paragraph, a document, etc. Illustratively, the target text can be a search sentence input by a user into a search engine; or the target text can be a document stored in a database, etc.

[0071] Illustratively, the initial hash code of the target text is randomly generated; or the initial hash code of the target text is generated by an encoder.

[0072] Illustratively, the initial hash code z is generated according to a Bernoulli distribution {Bernoulli(0.5)} d The initial hash code z is generated, where d is the dimension of the initial hash code z, that is, 0 or 1 is randomly generated in each dimension of the initial hash code z according to the Bernoulli distribution, and the probability of generating 0 or 1 is equal.

[0073] For example, the initial hash code of the target text is generated by an encoder. For example, in the process of sampling the posterior probability distribution by using the sampling probability distribution determined based on the Markov chain, the convergence speed is slow, and therefore the initial hash code can be generated by the encoder. The quality of the initial hash code generated by the encoder is better than that of the initial hash code randomly generated in terms of representing the discrete features of the target text, and the convergence process of sampling the sampling probability distribution determined based on the Markov chain can be accelerated.

[0074] The application does not limit the way of generating the initial hash code.

[0075] Step 340: establishing a semantic hash model of the target text according to the joint probability distribution of the target text and the initial hash code corresponding to the target text;

[0076] For example, for the target text, the semantic hash model of the target text is established according to the joint probability distribution of the target text and the initial hash code corresponding to the target text, and the specific process is as follows:

[0077] Taking the target text as a document x with |x| words as an example. The document x can be represented as a sequence of words x={w1, w2, …, w |x|}, where w i represents the i-th word in the document x, and w i is a |V|-dimensional one-hot vector, that is, the vector is used to represent the features in one dimension, and only one item in the vector is 1 and the other items are 0; |V| is the number of all words in the corpus. For the document x, the semantic hash model is established by the joint distribution probability of the document x and the initial hash code z corresponding to the document x:

[0078] p θ (x,z)=p θ (x|z)p(z)

[0079] where p(z) is a prior distribution, which is set to a Bernoulli distribution {Bernoulli(0.5)} in the embodiment of the application d , d is the dimension of the hash code; p θ (x|z) is a likelihood function, since the document x can be represented as a sequence of |x| words, the likelihood function can be decomposed as

[0080] p θ (w i |z) is defined as follows:

[0081]

[0082] where W∈R d×|V|is a parameter matrix; d is the dimension of the latent variable (i.e., the hash code); w j is a one-hot vector and only the jth item in the vector is 1 and the other items are 0; b i is a bias term; θ = {W, b1, b2, …, b |V|}.

[0083] Step 360: sampling the initial hash code according to the sampling probability distribution of the initial hash code to obtain a posterior probability distribution of the semantic hash model.

[0084] The sampling probability distribution is a conditional probability distribution determined according to the initial hash code and the target text. The sampling probability distribution is used to indicate the sampling process from the initial hash code to the hash code obtained after sampling. Taking the sampling probability distribution for sampling the initial hash code based on a Markov chain as an example, the sampling probability distribution is used to indicate the transition probability distribution of the hash code before sampling and the hash code after sampling.

[0085] Illustratively, the sampling probability distribution for sampling the initial hash code is determined based on a Markov chain according to the initial hash code and the target text; the initial hash code is sampled for n rounds according to the sampling probability distribution to obtain a posterior probability distribution of the semantic hash model, n being a positive integer.

[0086] Illustratively, taking the initial hash code having d dimensions and the initial hash code being sampled for n rounds as an example, the sampling probability distribution for sampling the initial hash code is determined under the condition that the initial hash code z = [z1, z2, …, zd] and the document x are given, where d is a positive integer. d T k (t) -k (t-1) ), where k = 1, 2, …, d; z -k = [z1, …, z k-1 , z k+1 , …, z d ] T , i.e., the initial hash code z removes the kth item z k ; the superscript t represents the sampling in the tth round, the superscript t-1 represents the sampling in the (t-1)th round, and t is an integer greater than 1 and not greater than n.

[0087] That is, based on the sampling result z -k (t-1) and the document x, the probability of sampling 0 or 1 for the kth item in the tth round is calculated, t being an integer greater than 1 and not greater than n.

[0088] ​​​That is, in the first round of sampling, the sampling probability distribution when the kth dimension of the initial hash code is sampled for the first time is determined according to the target text and the remaining dimensions except the kth dimension in the initial hash code, k is a positive integer not greater than d; in the sampling after the first round, the sampling probability distribution when the kth dimension of the t-1 hash code obtained after the initial hash code is sampled for t-1 times is sampled for the tth time is determined according to the target text and the other dimensions except the kth dimension in the t-1 hash code, k is a positive integer not greater than d, and t is an integer greater than 1 and not greater than n.

[0089] The present application does not limit the determination method of the sampling probability distribution of the initial hash code.

[0090] Optionally, n can be 1 or an integer greater than 1, that is, the initial hash code can be sampled for one round or multiple rounds. The more rounds of sampling, the more accurate the posterior probability distribution of the semantic hash model obtained, but at the same time, more computing resources are consumed and longer computing time is required. The present application does not limit the number of rounds of sampling of the initial hash code.

[0091] Step 380: determining the corrected hash code corresponding to the target text according to the posterior probability distribution.

[0092] By sampling the initial hash code, a more accurate posterior probability distribution p(z|x) of the semantic hash model is obtained, and then the corrected hash code corresponding to the target text is determined according to the sampled posterior probability distribution p(z|x), that is, the corrected hash code is generated according to the posterior probability distribution p(z|x).

[0093] In summary, in the embodiments of the present application, the initial hash code is generated, and the semantic hash model of the target document is established based on the initial hash code and the target document. A posterior probability distribution with better fitting effect is obtained by sampling the initial hash code, and then a corrected hash code that can better represent the discrete representation of the target text is obtained, so that the accuracy of the semantic hash model is improved.

[0094] The above embodiments generally introduce the process of sampling the initial hash code based on the semantic hash model to obtain a posterior probability distribution with better quality. Based on this, the process is specifically described below based on the method of determining the sampling probability distribution based on Markov chain.

[0095] Figure 4 A flowchart of a hash code generation method provided by an example embodiment of the present application is shown, which is applied to a terminal 120 or a server 140 as shown. Figure 2 As shown, the method includes: Figure 4

[0096] ​Step 422: randomly generating an initial hash code of the target text;

[0097] For example, a prior probability distribution of the initial hash code of the target text is preset, and the initial hash code is generated according to the prior probability distribution.

[0098] For example, the prior probability distribution p(z) of the initial hash code of the target text is set as a Bernoulli distribution {Bernoulli(0.5)} d where d is the dimension of the initial hash code z, that is, the initial hash code z is randomly generated as 0 or 1 in each dimension according to the Bernoulli distribution, and the probabilities of generating 0 or 1 are equal.

[0099] The present application does not limit the way of randomly generating the initial hash code of the target text.

[0100] Step 424: generating the initial hash code of the target text by an encoder;

[0101] For example, the initial hash code of the target text is generated by an encoder. Since the convergence speed of the process of approximating the posterior probability distribution by sampling according to the sampling probability distribution based on the Markov chain is slow, the initial hash code can be generated by the encoder. The quality of the initial hash code generated by the encoder is better than that of the randomly generated initial hash code in terms of representing the discrete features of the target text, and the convergence process of sampling according to the sampling probability distribution based on the Markov chain is accelerated.

[0102] For example, the following encoder is introduced:

[0103] q Φ (z|x)=Bernoulli(σ(f(x))

[0104] where Bernoulli() represents a Bernoulli distribution; σ represents a sigmoid function; and f(x) represents an encoder of a one-layer feedforward neural network.

[0105] For the target text x, the initial hash code z of the target text x is generated by the encoder, and z conforms to the probability distribution of the encoder, that is, z ~ q Φ (z|x).

[0106] Optionally, the encoder is obtained by training a one-layer feedforward neural network. The encoder is obtained by training by the following steps: determining a loss function of the encoder; and training parameters of the encoder based on the loss function by using an expectation maximization algorithm.

[0107] The present application does not limit the implementation of the encoder.

[0108] It should be noted that step 422 and step 424 are only required to be performed alternatively. If step 422 is selected to be performed, the initial hash code can be obtained quickly and simply, and the computing resources can be saved; if step 424 is selected to be performed, the quality of the initial hash code generated is better than that of the initial hash code randomly generated, and the process of sampling based on the Markov chain can be accelerated to converge.

[0109] Step 440: establishing a semantic hash model of the target text according to the joint probability distribution of the target text and the initial hash code corresponding to the target text;

[0110] For example, for a target text, a semantic hash model of the target text is established according to the joint probability distribution of the target text and the initial hash code corresponding to the target text, and the specific process is as follows:

[0111] Taking the target text as a document x with |x| words as an example. The document x can be represented as a sequence of words x={w1, w2, …, w |x|}, where w i represents the i-th word in the document x, and w i is a |V|-dimensional one-hot vector, that is, only one item in the vector can be 1 and the other items are 0; V is the number of all words in the corpus. For the document x, a semantic hash model is established through the joint distribution probability of the document x and the initial hash code z corresponding to the document x:

[0112] p θ (x,z)=p θ (x|z)p(z)

[0113] Wherein, p(z) is a prior distribution, which is set to Bernoulli(0.5) in the embodiment of the application d , d is the dimension of the hash code; p θ (x|z) is a likelihood function, since the document x can be represented as a sequence of |x| words, the likelihood function can be decomposed as

[0114] p θ (w i |z) is defined as follows:

[0115]

[0116] Wherein, W∈R d×|V| is a parameter matrix; d is the dimension of the hidden variable (i.e., the hash code); w j is a one-hot vector and only the j-th item in the vector is 1 and the other items are 0; b i is a bias term; θ={W, b1, b2, …, b |V|}.

[0117] Step 462: determining a sampling probability distribution of sampling the initial hash code based on a Markov chain according to the initial hash code and the target text;

[0118] Exemplarily, the process of sampling the initial hash code can only perform one round of sampling, or multiple rounds of sampling according to actual needs.

[0119] This embodiment takes the tthround of sampling of the hash code obtained by the t-1thround of sampling as an example to demonstrate the specific process of determining the sampling of the initial hash code based on the Markov chain.

[0120] Exemplarily, the t-1thhash code z obtained by the t-1thround of sampling is z = [z1, z2, …, z d ] T ; define z -k = [z1, …, z k-1 , z k+1 , …, z d ] T , that is, the remaining hash code matrix obtained after removing the kthitem z k in the t-1thhash code is z -k . Sampling is performed by calculating the conditional probability distribution p(z k (t) | x, z -k (t-1) ), that is, in the tthround of sampling, according to the target document x and the remaining hash code in the t-1thhash code except z k , the kthbit in the hash code is sampled in the tthround.

[0121] The specific calculation process is as follows:

[0122] Let , that is, the kthbit of the hash code sampled in the tthround is 1; let , that is, the kthbit of the hash code sampled in the tthround is 0.

[0123] According to the Bayes formula, the following formula can be derived and defined as γ k :

[0124]

[0125] According to the model established in step 440 and the definition of p θ (w i | z), the following formula can be obtained:

[0126]

[0127] Further, the kth bit z k The value in the tth round of sampling can only be 0 or 1, that is, p(z k (t) =1|x,z -k (t-1) )+p(z k (t) =0|x,z -k (t-1) )=1, the following can be derived:

[0128]

[0129] The above process of calculating p(z k (t) |x,z -k (t-1) ), k = 1, 2, …, d, shows the determination process of the sampling probability distribution of the hash code in the tth round of sampling. Thus, the sampling probability distribution of the hash code obtained in the previous round in any round of sampling can be derived.

[0130] The embodiments of the present application calculate the sampling probability distribution of the initial hash code by the above method, but the number of rounds of sampling is not limited.

[0131] Step 464: sampling the initial hash code according to the sampling probability distribution to obtain the posterior probability distribution of the semantic hash model;

[0132] Each bit of the initial hash code is sampled according to the sampling probability distribution determined in step 462, that is, d times of sampling are required in each round, and d is the dimension of the initial hash code.

[0133] Exemplarily, one round of sampling is performed on the initial hash code; or multiple rounds of sampling are performed on the initial hash code. After the sampling is completed, the posterior probability distribution p(z|x) of the semantic hash model is obtained.

[0134] Step 480: determining the correction hash code corresponding to the target text according to the posterior probability distribution.

[0135] In step 464, the posterior probability distribution p(z|x) of the semantic hash model is obtained by sampling the initial hash code, and then the correction hash code corresponding to the target text is determined according to the posterior probability distribution p(z|x) after sampling, that is, the correction hash code is generated according to the posterior probability distribution p(z|x).

[0136] In summary, in the embodiments of the present application, an initial hash code is generated, and a posterior probability distribution more consistent with the actual situation is obtained after sampling, and then a corrected hash code corresponding to the target text is obtained. A method for generating a corrected hash code with optimized quality by sampling based on a Markov chain is provided, so that the corrected hash code can better represent the discrete representation of the target text, and the accuracy of the semantic hash model is improved, and the result of subsequent text matching based on the corrected hash code is more accurate.

[0137] In addition, in the embodiments of the present application, an encoder is introduced to generate an initial hash code, so that a hash code with better quality can be obtained before sampling, and the number of subsequent sampling rounds is reduced, and the accuracy of the hash code is improved.

[0138] The above embodiments show a semantic hash model based on a target text, a method for determining a sampling probability distribution based on a Markov chain and sampling to obtain a hash code with better quality. After obtaining the hash code of the target text, the similarity can be calculated by comparing the Hamming distance of the hash codes of the target text and the candidate text, so as to realize the matching of the texts.

[0139] Figure 5 A flowchart of a text matching method provided by an example embodiment of the present application is shown, and the method is applied to a terminal 120 or a server 140 as shown. Figure 2 As shown in the terminal 120 or the server 140, the method includes: Figure 5

[0140] Step 720: obtaining an input first text;

[0141] The first text can be any form of text with text content, for example, the first text can be a sentence, a paragraph, a document, etc. Illustratively, the first text can be a search sentence input by a user into a search engine.

[0142] Step 740: obtaining a first hash code of the first text and a second hash code of a candidate text;

[0143] The candidate text is a text used to match the first text to obtain a matching result. The candidate text can be any form of text with text content. Illustratively, the candidate text is a document stored in a database.

[0144] The first hash code and the second hash code are both corrected hash codes obtained by sampling an initial hash code according to a sampling probability distribution of the initial hash code, the initial hash code is a hash code generated based on the first text and the candidate text respectively, and the sampling probability distribution is a conditional probability distribution determined according to the initial hash code and the candidate text.

[0145] The process of obtaining the first hash code of the first text and the second hash code of the candidate text can be referred to in the description of the above embodiments. Figure 3 ​The illustrated embodiment or Figure 4 The illustrated embodiment, which will not be described again here.

[0146] Step 760: Calculate the Hamming distance of the first hash code and the second hash code.

[0147] For example, the first hash code is 1011101, and the second hash code is 1001001. The Hamming distance of the first hash code and the second hash code is 2.

[0148] Step 780: Determine the candidate text corresponding to the second hash code with the smallest Hamming distance from the first hash code as the second text.

[0149] If the Hamming distance of the second hash code and the first hash code is the smallest, it means that the number of corresponding bits that are the same between the second hash code and the first hash code is the most, that is, the discrete representation of the same between the text corresponding to the second hash code and the first text corresponding to the first hash code is the most, that is, the text corresponding to the second hash code and the first text are the most similar. Therefore, the candidate text corresponding to the second hash code with the smallest Hamming distance from the first hash code is determined as the second text.

[0150] For example, the second hash code with the smallest Hamming distance from the first hash code is input into the generator to obtain the posterior probability distribution p(z|x) corresponding to the second hash code; and the second text corresponding to the second hash code is determined based on the posterior probability distribution p(z|x) corresponding to the second hash code.

[0151] Optionally, after determining the second text, the second text is displayed. For example, if the first text is a search statement input by a user in a search engine, the second text is displayed on the user interface of the search engine after being determined. The second text can be one or more texts that are the most similar to the first text determined by the Hamming distance of the hash codes; if it is multiple texts, they are displayed in order from high to low according to the similarity.

[0152] The number and display method of the second text are not limited in the present application.

[0153] In summary, in the embodiments of the present application, the hash codes of the first text and the candidate text are obtained, and the second text similar to the first text is determined based on the Hamming distance of the hash codes. By using the correction hash code obtained by the foregoing sampling method, the accuracy of text matching is improved.

[0154] Figure 5The embodiment shown illustrates the process of the text matching method, and in particular, the text matching method can be applied in a retrieval scenario. The following takes the retrieval scenario in the medical field as an example to illustrate the process of applying the text matching method in the scenario.

[0155] Figure 6 A flowchart of applying the text matching method in a retrieval scenario is shown, and the method is applied in the terminal 120 or the server 140 shown. Figure 2 As shown, the method comprises the following steps. Figure 6

[0156] The generation stage:

[0157] Step 810: obtaining the input retrieval text;

[0158] The retrieval text is the text used to retrieve specific information, for example, the retrieval text is a keyword or a sentence used for retrieval; for another example, the retrieval text is a question input by a user, and the like.

[0159] Taking the search in the medical field as an example, the retrieval text can be a keyword such as a disease name or a drug name input by a user in the search box of a search engine; or a description of a disease condition input by a user; or health care information that a user wants to know, and the like.

[0160] Step 820: obtaining the first hash code of the retrieval text;

[0161] For example, the initial hash code of the retrieval text is generated according to the retrieval text; the semantic hash model of the retrieval text is established according to the joint probability distribution of the retrieval text and the initial hash code; the initial hash code of the retrieval text is sampled according to the sampling probability distribution of the initial hash code based on the semantic hash model, to obtain the posterior probability distribution of the semantic hash model, and then the correction hash code of the retrieval text is determined according to the posterior probability distribution.

[0162] Optionally, the sampling probability distribution of sampling the initial hash code is determined based on a Markov chain according to the initial hash code and the target text; the initial hash code is sampled for n rounds according to the sampling probability distribution, to obtain the posterior probability distribution of the semantic hash model, and n is a positive integer.

[0163] ​Optionally, taking the initial hash code with d dimensions as an example, n rounds of sampling are performed on the initial hash code, and a sampling probability distribution of the kth dimension of the initial hash code in the first round of sampling is determined according to the target text and the remaining dimensions of the initial hash code except the kth dimension, where k is a positive integer not greater than d; a sampling probability distribution of the kth dimension of the (t-1) th hash code in the tth round of sampling is determined according to the target text and the other dimensions of the (t-1) th hash code except the kth dimension, where k is a positive integer not greater than d, and t is an integer greater than 1 and not greater than n.

[0164] Optionally, the initial hash code can be generated in a random manner or by a trained encoder.

[0165] Optionally, the encoder can be trained by the following steps: determining a loss function of the encoder; and training parameters of the encoder based on the loss function by using an expectation maximization algorithm.

[0166] Optionally, the sampling on the initial hash code can be performed for one round or multiple rounds.

[0167] Step 830: obtaining a second hash code of the candidate text;

[0168] The candidate text is a text that is a candidate for the answer text obtained by retrieval; and the candidate text that is most matched with the retrieval condition is selected from the candidate text by a text matching method as the answer text. Illustratively, the candidate text can be a document in a medical database; or the candidate text is a drug name in a drug recommendation database, etc.

[0169] The way of obtaining the second hash code of the candidate text is similar to the way of obtaining the first hash code of the retrieval text, which will not be described here.

[0170] It should be noted that steps 810 to 820 and step 830 have no execution sequence limitation and no execution time limitation. For example, steps 810 to 820 can be executed first, and then step 830 is executed; or step 830 can be executed first, and then steps 810 to 820 are executed; or steps 810 to 820 and step 830 can be executed simultaneously. Steps 810 to 820 and step 830 can be executed at different frequencies. For example, steps 810 to 820 are executed when the retrieval text is input; and step 830 is executed periodically due to the change of data in the database. The present application does not limit the execution sequence and the number of times between steps 810 to 820 and step 830.

[0171] Retrieval phase:

[0172] Step 840: Calculate the Hamming distance between the first hash code and the second hash code;

[0173] For example, performing an XOR operation on the first hash code and the second hash code, and counting the number of times the result is 1, yields the Hamming distance between the first hash code and the second hash code. The Hamming distance represents the number of times corresponding bits in the first hash code and the second hash code differ. For instance, if the first hash code is 1011101 and the second hash code is 1001001, the Hamming distance between the first hash code and the second hash code is 2.

[0174] Step 850: The candidate text corresponding to the second hash code that has the smallest Hamming distance to the first hash code is determined as the answer text.

[0175] If the Hamming distance between the second hash code and the first hash code is the smallest, it means that the second hash code and the first hash code have the most identical corresponding bits. In other words, the text corresponding to the second hash code and the first text corresponding to the first hash code have the most identical discrete representations, meaning that the text corresponding to the second hash code and the first text are most similar. Therefore, the candidate text corresponding to the second hash code with the smallest Hamming distance to the first hash code is determined as the answer text.

[0176] For example, the second hash code with the smallest Hamming distance to the first hash code is input into the generator to obtain the posterior probability distribution p(z|x) corresponding to the second hash code; the answer text corresponding to the second hash code is determined based on the posterior probability distribution p(z|x) corresponding to the second hash code.

[0177] Optionally, the generator is trained by the following steps: determining the loss function of the generator; and training the parameters of the encoder using the expectation-maximization algorithm based on the loss function.

[0178] Optionally, the answer text is displayed after it has been determined. For example, if the search text is a query entered by the user in the search engine, the answer text is displayed on the user's graphical interface of the search engine after it has been determined. The answer text can be one or more texts that are most similar to the search text, as determined by the Hamming distance of the hash code; if there are multiple texts, they are displayed in descending order of similarity.

[0179] Examples such as Figure 1The user enters the search text "Is drinking freshly squeezed juice better than eating fruit?" into the search box of a medical and health search engine. Using the hash code generation method described in this application, the user obtains a first hash code for the search text and a second hash code for candidate texts in the search engine's database. The user then selects the candidate text corresponding to the second hash code closest to the first hash code as the answer text and pushes it to the user. The number of answer texts can be preset; for example, the ten documents with the highest similarity to the search text can be selected as the answer texts pushed to the user.

[0180] As can be seen from the above embodiments, the semantic hash model can be represented as follows: Figure 7 The data model shown.

[0181] Figure 7 The left half shows the sampling process at stage t. Document x establishes a semantic hash model by processing p(z|x,θ). t-1 Sampling is performed to obtain the hash code z corresponding to document x, where θ t-1 This represents the parameter θ obtained in the previous training round, i.e., phase t-1.

[0182] Figure 7 The right half shows the hash code z generated through stage t sampling, using generator g. θ (z) decodes the hash code z into the corresponding document.

[0183] Based on the foregoing embodiments and Figure 7 The data model shown illustrates that in the sampling phase of stage t, an encoder can be introduced to generate a high-quality initial hash code. In the decoding phase of stage t, a generator is needed to decode the hash code z into p(z|x), thereby determining the document x corresponding to the hash code z. Therefore, the encoder and generator need to be pre-trained to improve their performance in practical applications and enhance the accuracy of the semantic hashing model. The training process of the encoder and generator is described below.

[0184] This application uses the expectation-maximization algorithm as a framework to perform two-stage training on the encoder and generator. Figure 8 The diagram shows a two-stage training process. The specific process is as follows:

[0185] Step 520: Fix the generator parameters θ, and train the encoder parameters.

[0186] Training encoder parameters During the process, the generator parameters θ are kept constant. The encoder parameters are trained. The process involves first determining the encoder's loss function, and then using the expectation-maximization algorithm to optimize the encoder parameters based on the loss function. Conduct training.

[0187] For example, the loss function of the encoder is determined as follows: in, Let p be the probability distribution of the encoder. θ (x|z) is the likelihood function of the semantic hash model.

[0188] The encoder-based loss function employs the expectation-maximization algorithm on the encoder parameters. To train, that is, through training Training is performed to obtain the loss function Parameters for obtaining the maximum value

[0189] because Since it is a discrete probability distribution function, its derivative cannot be obtained. Therefore, a straight-through estimator (STE) is used for gradient estimation.

[0190] For example, training termination conditions can be set according to actual conditions. Training termination conditions include: the number of training iterations reaches a preset number, or the gradient estimate obtained by the pass-through estimator is less than a gradient threshold, etc. This application does not limit the preset number of training iterations or the gradient threshold.

[0191] Step 530: Determine if t is less than or equal to T;

[0192] For example, the initial hash code generated by the encoder is sampled T times, where T is a positive integer. For the current t-th sample, it is determined whether t is less than or equal to T. If t is less than or equal to T, step 532 is executed to perform the t-th sample; if t is greater than T, the sampling process is completed, a correction hash code is generated, and step 540 is executed to train the generator using the correction hash code, where t is a positive integer not greater than T.

[0193] Step 532: Perform the t-th round of sampling;

[0194] If t is less than or equal to T, proceed to step 532, based on the sampling of the first t-1 rounds, by calculating p(z) k (t) |x,z -k (t-1) The initial hash code is sampled in the t-th round. The sampling process is shown in steps 462 and 464, and will not be repeated here.

[0195] Step 540: Fix encoder parameters The generator parameter θ is trained.

[0196] During the training of the generator parameter θ, the encoder parameter The generator parameter θ is trained. After the correction hash code z is obtained by T rounds of sampling, z is input into the generator, and the training of the generator parameter θ is performed. The loss function of the generator is determined; the parameter of the encoder is trained based on the loss function using the expectation maximization algorithm.

[0197] For example, the generator parameter θ is trained by maximizing the log-likelihood function logp(x). Since there is no analytical solution for p(x), it is necessary to use the expectation maximization algorithm to maximize the lower bound of the log-likelihood function logp(x) to achieve the purpose of maximizing logp(x), that is:

[0198]

[0199] where const is a constant that does not need to be maximized. The sum z p θ t-1 (z|x)logp θ t (z|x) is taken, and the loss function is defined as follows:

[0200]

[0201] where z is the hash code obtained by sampling p(z|x).

[0202] The generator parameter θ is trained based on the loss function of the generator using the expectation maximization algorithm, that is, by training maxlogp θ t (x|z) to obtain the parameter θ that makes the loss function logp θ t (x|z) reach the maximum value.

[0203] For example, the end condition of the training is set according to the actual situation. The end condition of the training includes that the number of training reaches a preset number, or the gradient value of the loss function is less than a gradient threshold, etc. The preset number of training and the gradient threshold are not limited in the present application.

[0204] Through the above two-stage training, the optimized encoder and generator can be obtained. Figure 9The method of obtaining the hash code corresponding to the document using the trained encoder is shown. Since the trained encoder performs well, the convergence process is quickly completed when sampling is performed using the sampling probability distribution method based on Markov chain determination, so here only one round of sampling is performed as an example to show the process of obtaining the hash code corresponding to the document. The steps are as follows:

[0205] Step 620: generating an initial hash code by the encoder;

[0206] For the input document x, the encoder trained in step 520 is used The initial hash code is generated according to the probability distribution of the encoder, that is,

[0207] Step 630: determining whether k is less than or equal to d;

[0208] d represents the dimension of the hash code, and k represents the number of bits in the initial hash code currently sampled. If k is less than or equal to d, it means that the sampling process has not been completed, and step 632 is performed; if k is greater than d, it means that the sampling of each bit in the initial hash code has been completed, and the corrected hash code after sampling is output.

[0209] Step 632: sampling the kth bit of the initial hash code.

[0210] The p(z k (t) |x, z -k (t-1) ) is calculated, and the kth bit of the initial hash code is sampled. The calculation process is described in step 464, which is not repeated here.

[0211] Through the above method, the document x generates an initial hash code z through the encoder, and then obtains a corrected hash code after one round of sampling. This method performs well in practical applications.

[0212] Exemplarily, testing is performed on data sets Reuters21578 and TMC. Among them, the Reuters21578 data set has 7164 words, 7752 training files, 967 validation files and 964 test files; the TMC data set has 20000 words, 21286 training files, 3498 validation files and 6498 test files. Each file in the data set has a class label.

[0213] The search precision is used as the test standard. For each test file, the first 100 most similar files to the test file are selected based on the Hamming distance of the hash code, and the proportion of the class labels in the 100 most similar files that match the class label of the test file is used as the search precision of this search. Finally, the average of the search precisions of each test file is the test result of the entire test set.

[0214] The test results of the hash code generation method provided in the present application and the method of generating hash code using related technologies are shown in Tables 1 and 2. Table 1 is the result obtained by testing in the data set Reuters21578, and Table 2 is the result obtained by testing in the data set TMC. In the table, Ours (ours) is used to identify the method provided in the present application, and the methods in the related technologies are SpH (Spectrum Hash), STH (Self Taught Hash), S-RBM (S-Restricted Boltzmann Machine), VDSH (Variational Deep Semantic Hashing), NASH (Neural Architecture Semantic Hashing), and GMSH (Gaussian Mixture Semantic Hashing).

[0215] As can be seen from Tables 1 and 2, in the case of setting the hash code to different dimensions (8 bits, 16 bits, 32 bits, 64 bits, and 128 bits are selected as examples in the test), the accuracy obtained by the method provided in the present application is generally higher than that of other models in the related technologies.

[0216] Table 1

[0217]

[0218] Table 2

[0219]

[0220] Figure 10 is a structural block diagram of a hash code generation device provided by an exemplary embodiment of the present application, as shown in Figure 10 The device includes:

[0221] The generation module 920 is configured to generate an initial hash code of the target text.

[0222] The model establishing module 940 is configured to establish a semantic hash model of the target text according to a joint probability distribution of the target text and the initial hash code.

[0223] The sampling module 960 is configured to sample the initial hash code according to a sampling probability distribution of the initial hash code to obtain a posterior probability distribution of the semantic hash model, where the sampling probability distribution is a conditional probability distribution determined according to the initial hash code and the target text.

[0224] The determining module 980 is configured to determine a corrected hash code corresponding to the target text according to the posterior probability distribution.

[0225] In one possible implementation, the sampling module 960 includes a determining sub-module 962 and a sampling sub-module 964. The determining sub-module 962 is configured to determine, according to the initial hash code and the target text, a sampling probability distribution of sampling the initial hash code based on a Markov chain; and the sampling sub-module 964 is configured to sample the initial hash code n times according to the sampling probability distribution to obtain the posterior probability distribution of the semantic hash model, where n is a positive integer.

[0226] In one possible implementation, the initial hash code has d dimensions, and the initial hash code is sampled n times. The determining sub-module 962 is configured to determine, according to the target text and the remaining dimensions of the initial hash code except for the kth dimension, a sampling probability distribution of sampling the kth dimension of the initial hash code for the first time, where k is a positive integer not greater than d. The determining sub-module 962 is further configured to determine, according to the target text and the other dimensions of an (t-1)th hash code obtained after the initial hash code is sampled for (t-1) times except for the kth dimension, a sampling probability distribution of sampling the kth dimension of the (t-1)th hash code for the tth time, where k is a positive integer not greater than d, and t is an integer greater than 1 and not greater than n.

[0227] In one possible implementation, the generating module 920 is configured to randomly generate the initial hash code of the target text; or the generating module 920 is configured to generate the initial hash code of the target text by using an encoder.

[0228] In one possible implementation, the encoder is obtained by training a feedforward neural network of one layer; and the encoder is obtained by training using the following steps: determining a loss function of the encoder; and training parameters of the encoder based on the loss function by using an expectation maximization algorithm.

[0229] It should be noted that the above embodiment provides the hash code generation apparatus, and only the division of each functional module is exemplified, and in actual application, the above functions can be completed by different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the hash code generation apparatus provided by the above embodiment belongs to the same concept as the text matching embodiment, and the specific implementation process is detailed in the method embodiment, which will not be repeated here.

[0230] Figure 11 is a structural block diagram of a text matching apparatus provided by an example embodiment of the present application, as shown in Figure 11 The apparatus comprises:

[0231] The acquisition module 1020 is configured to acquire the input first text.

[0232] The acquisition module 1020 is further configured to acquire a first hash code of the first text and a second hash code of a candidate text, the first hash code and the second hash code are both corrected hash codes obtained by sampling an initial hash code according to a sampling probability distribution of the initial hash code, the initial hash code is a hash code generated based on the first text and the candidate text, and the sampling probability distribution is a conditional probability distribution determined according to the initial hash code and the candidate text.

[0233] The calculation module 1040 is configured to calculate a Hamming distance of the first hash code and the second hash code.

[0234] The determination module 1060 is configured to determine the candidate text corresponding to the second hash code with the smallest Hamming distance from the first hash code as a second text, the second text being the text most similar to the first text.

[0235] In one possible embodiment, the determination module 1060 is configured to input the second hash code with the smallest Hamming distance from the first hash code into a generator to obtain a posterior probability distribution corresponding to the second hash code, and determine the second text corresponding to the second hash code based on the posterior probability distribution corresponding to the second hash code.

[0236] In one possible embodiment, the generator is trained by the following steps: determining a loss function of the generator; and training parameters of the encoder based on the loss function by using an expectation maximization algorithm.

[0237] It should be noted that the text matching apparatus provided in the above embodiment is only exemplified by the division of the above functional modules, and in actual application, the above functions can be completed by different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the text matching apparatus provided in the above embodiment belongs to the same concept as the text matching embodiment, and its specific implementation process is described in detail in the method embodiment, which will not be repeated here.

[0238] Figure 12 is a structural schematic diagram of a computer device according to an exemplary embodiment. The computer device 1300 includes a central processing unit (CPU) 1301, a system memory 1304 including a random access memory (RAM) 1302 and a read-only memory (ROM) 1303, and a system bus 1305 connecting the system memory 1304 and the central processing unit 1301. The computer device 1300 also includes a basic input / output system (I / O) 1306 to help transfer information between various devices within the computer device, and a mass storage device 1307 for storing an operating system 1313, application programs 1314 and other program modules 1315.

[0239] The basic input / output system 1306 includes a display 1308 for displaying information and an input device 1309 such as a mouse, keyboard, etc. for user input. The display 1308 and the input device 1309 are both connected to the central processing unit 1301 through an input / output controller 1310 connected to the system bus 1305. The basic input / output system 1306 can also include an input / output controller 1310 for receiving and processing input from a keyboard, mouse, or electronic stylus, and other devices. Similarly, the input / output controller 1310 also provides output to a display screen, printer, or other types of output devices.

[0240] The mass storage device 1307 is connected to the central processing unit 1301 through a mass storage controller (not shown) connected to the system bus 1305. The mass storage device 1307 and its associated computer device readable medium provide non-volatile storage for the computer device 1300. That is, the mass storage device 1307 can include a computer device readable medium (not shown) such as a hard disk or a compact disc read-only memory (CD-ROM) drive.

[0241] Without loss of generality, the computer device readable medium can include computer device storage media and communication media. Computer device storage media includes volatile and non-volatile, removable and non-removable media implemented in any method or technology for storage of information such as computer device readable instructions, data structures, program modules or other data. Computer device storage media includes RAM, ROM, Erasable Programmable Read-Only Memory (EPROM), Electrically Erasable Programmable Read-Only Memory (EEPROM), CD-ROM, Digital Video Disc (DVD) or other optical disk storage, magnetic cassettes, magnetic tape, magnetic disk storage or other magnetic storage devices. It should be understood by those skilled in the art that computer device storage media is not limited to the above-mentioned several. The system memory 1304 and the mass storage device 1307 mentioned above can be collectively referred to as memory.

[0242] According to various embodiments of the present disclosure, the computer device 1300 can also run on a remote computer device connected to a network, such as the Internet. That is, the computer device 1300 can be connected to a network 1311 through a network interface unit 1312 connected to the system bus 1305, or can be connected to other types of network or remote computer device systems (not shown) using the network interface unit 1312.

[0243] The memory further includes one or more programs stored in the memory, and the central processing unit 1301 implements all or part of the steps of the shooting display method of the virtual gun by executing the one or more programs. The embodiments of the present application also provide a computer readable storage medium, which stores at least one instruction, at least one program, a code set or an instruction set. The at least one instruction, at least one program, code set or instruction set is loaded and executed by the processor to implement the hash code generation method or the text matching method provided by the above-mentioned method embodiments.

[0244] The embodiments of the present application also provide a computer program product or computer program, which includes computer instructions stored in a computer readable storage medium. The processor of the computer device reads the computer instructions from the computer readable storage medium, and the processor executes the computer instructions, so that the computer device executes the hash code generation method or the text matching method described in any of the above embodiments.

[0245] Optionally, the computer readable storage medium can include a read only memory (ROM), a random access memory (RAM), a solid state disk (SSD), an optical disk, etc. Among them, the random access memory can include a resistance random access memory (ReRAM) and a dynamic random access memory (DRAM). The above-mentioned application embodiment serial number is only for description, not representing the pros and cons of the embodiments.

[0246] Those skilled in the art can understand that all or part of the steps of the above-mentioned embodiments can be completed by hardware, or can be completed by a program instructing related hardware, and the program can be stored in a computer readable storage medium. The storage medium mentioned above can be a read only memory, a magnetic disk or an optical disk. The above is only an optional embodiment of the present application, and is not used to limit the present application. Any modification, equivalent replacement, improvement, etc. within the spirit and principle of the present application shall be included in the protection scope of the present application.

Claims

1. A method for generating a hash code, characterized by, The method comprises: generating an initial hash code of a target text; establishing a semantic hash model of the target text according to a joint probability distribution of the target text and the initial hash code; sampling the initial hash code according to a sampling probability distribution of the initial hash code to obtain a posterior probability distribution of the semantic hash model, the sampling probability distribution being a conditional probability distribution determined according to the initial hash code and the target text; determining a corrected hash code corresponding to the target text according to the posterior probability distribution; the sampling of the initial hash code according to the sampling probability distribution of the initial hash code to obtain the posterior probability distribution of the semantic hash model comprises: determining a sampling probability distribution for sampling the initial hash code based on a Markov chain according to the initial hash code and the target text; sampling the initial hash code n rounds according to the sampling probability distribution to obtain the posterior probability distribution of the semantic hash model, n being a positive integer; wherein the initial hash code has d dimensions, and the initial hash code is sampled n rounds; the determination of the sampling probability distribution for sampling the initial hash code based on a Markov chain according to the initial hash code and the target text comprises: determining a sampling probability distribution for sampling the kth dimension of the initial hash code in the first round according to the remaining dimensions of the initial hash code except the kth dimension and the target text, k being a positive integer not greater than d; determining a sampling probability distribution for sampling the kth dimension of the t-1th hash code in the tth round according to the other dimensions of the t-1th hash code except the kth dimension and the target text, k being a positive integer not greater than d, and t being an integer greater than 1 and not greater than n.

2. The method of claim 1, wherein, the generation of the initial hash code of the target text comprises: randomly generating the initial hash code of the target text; or, generating the initial hash code of the target text through an encoder.

3. The method of claim 2, wherein, The encoder is trained by the following steps: determining a loss function of the encoder; training parameters of the encoder based on the loss function using an expectation maximization algorithm.

4. A method of text matching, characterized by, The method comprises: obtaining an input first text; obtaining a first hash code of the first text and a second hash code of a candidate text, the first hash code and the second hash code being generated based on the method of any one of claims 1 to 3; calculating a Hamming distance between the first hash code and the second hash code; determining the candidate text corresponding to the second hash code with the smallest Hamming distance from the first hash code as a second text, the second text being a text most similar to the first text.

5. The method of claim 4, wherein, the determination of the candidate text corresponding to the second hash code with the smallest Hamming distance from the first hash code as a second text comprises: inputting the second hash code with the smallest Hamming distance from the first hash code into a generator to obtain a posterior probability distribution corresponding to the second hash code; The second text corresponding to the second hash code is determined based on the posterior probability distribution corresponding to the second hash code.

6. The method of claim 5, wherein, The generator is trained using the following steps: Determine the loss function of the generator; The generator's parameters are trained using the expectation-maximization algorithm based on the loss function.

7. An apparatus for generating a hash code, characterized by comprising: The device includes: The generation module is used to generate the initial hash code of the target text; The model building module is used to build a semantic hash model of the target text based on the joint probability distribution of the target text and the initial hash code. A sampling module is used to sample the initial hash code according to the sampling probability distribution of the initial hash code to obtain the posterior probability distribution of the semantic hash model, wherein the sampling probability distribution is a conditional probability distribution determined based on the initial hash code and the target text; The determination module is used to determine the correction hash code corresponding to the target text based on the posterior probability distribution; The sampling module includes a determination submodule and a sampling submodule; The determining submodule is used to determine the sampling probability distribution for sampling the initial hash code based on the initial hash code and the target text, using a Markov chain. The sampling submodule is used to sample the initial hash code for n rounds according to the sampling probability distribution to obtain the posterior probability distribution of the semantic hash model, where n is a positive integer; The initial hash code has d dimensions, and the initial hash code is sampled n times. The determining submodule is used to determine the sampling probability distribution when performing the first round of sampling on the k-th dimension of the initial hash code based on the remaining dimensions of the initial hash code except for the k-th dimension and the target text, where k is a positive integer not greater than d; The determining submodule is further configured to determine the sampling probability distribution when sampling the k-th dimension of the (t-1)-th hash code in the t-th round based on the other dimensions of the (t-1)-th hash code obtained after sampling the initial hash code in the (t-1)-th round, excluding the k-th dimension, and the target text, where k is a positive integer not greater than d, and t is an integer greater than 1 and not greater than n.

8. The apparatus according to claim 7, characterized in that, The generation module is used to randomly generate the initial hash code of the target text; or, The generation module is used to generate the initial hash code of the target text through an encoder.

9. A text matching apparatus, characterized by comprising: The device includes: The acquisition module is used to acquire the first text input. The acquisition module is further configured to acquire a first hash code of the first text and a second hash code of the candidate text, wherein the first hash code and the second hash code are both generated based on the method described in any one of claims 1 to 3; The calculation module is used to calculate the Hamming distance between the first hash code and the second hash code; The determining module is used to determine the candidate text corresponding to the second hash code that has the smallest Hamming distance to the first hash code as the second text, wherein the second text is the text most similar to the first text.

10. A computer device, comprising: The computer device comprises a processor, a memory connected to the processor, and program instructions stored on the memory, and the processor executes the program instructions to implement the hash code generation method according to any one of claims 1 to 3 or the text matching method according to any one of claims 4 to 6.

11. A computer-readable storage medium having stored therein program instructions, the program instructions comprising: The program instructions are executed by the processor to implement the hash code generation method according to any one of claims 1 to 3 or the text matching method according to any one of claims 4 to 6.

Citation Information

Patent Citations

  • Image data processing method and device, model establishment method and device, computer equipment and storage medium

    CN109711422A

  • Model training method and device, electronic equipment and storage medium

    CN110209851A