Detection method and device of confrontation sample and electronic equipment
By performing sharding and likelihood analysis on the input text, using standard deviations to determine whether the input text is an adversarial sample, the problem of difficulty in detecting and defending against adversarial samples generated by GCG attacks in the prior art is solved, and efficient adversarial sample detection is achieved.
Patent Information
- Application Number
- CN202311333574.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-16
- Publication Date
- 2025-04-18
AI Technical Summary
The prior art is difficult to effectively detect and defend against adversarial samples generated by greedy coordinate gradient (GCG) attacks that are able to bypass the rejection ability of the language model, resulting in the generation of undesirable harmful content.
By dividing the input text into fragment sequences, the log-likelihood sequence and its standard deviation of each fragment are calculated, and based on the comparison of the standard deviation and the predetermined threshold, it is determined whether the input text is an adversarial sample.
It realizes efficient detection of input text of any length, can correctly distinguish adversarial samples from normal samples, and improves the efficiency of adversarial samples detection.
Smart Images

Figure CN119848839A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of smart terminal technology, and in particular to a method, device and electronic device for detecting adversarial samples. Background Art
[0002] A large language model (LLM) is a language model consisting of a neural network with many parameters (usually billions or more) that can perform a variety of tasks using natural language as instructions.
[0003] Large language models at this stage can generate a lot of harmful content. Recent research has focused on aligning these models to prevent unwanted generation results. Among the existing related technologies, the greedy coordinate gradient (GCG) attack proposes a simple and effective attack method that can cause aligned language models to produce harmful behavior. Specifically, the attack finds a suffix and connects it to the back of the LLM input sample, which can increase the probability that the LLM will generate a positive answer instead of a negative answer. This method does not rely on manual debugging, but automatically generates these adversarial suffixes through greedy and gradient-based search techniques, and improves on the previous automatic prompt generation method. In addition, the adversarial prompts generated by this type of method are highly transferable and can attack all kinds of open source and closed source LLMs, which makes a series of content security technologies previously studied for LLMs invalid. It is necessary to provide a new solution to detect and defend against this type of attack that generates adversarial prompts. Summary of the invention
[0004] The embodiments of the present application provide a method, device and electronic device for detecting adversarial samples. The embodiments of the present application also provide a computer-readable storage medium to enable the electronic device to correctly distinguish between adversarial samples and normal samples, thereby improving the detection efficiency of adversarial samples.
[0005] In a first aspect, an embodiment of the present application provides a method for detecting an adversarial sample, comprising: obtaining an input text; dividing the input text into a sequence of fragments; calculating a log-likelihood sequence corresponding to the sequence of fragments, and calculating a standard deviation of the log-likelihood sequence; and obtaining a detection result of whether the input text is an adversarial sample based on the standard deviation.
[0006] In one possible implementation, obtaining a detection result of whether the input text is an adversarial sample based on the standard deviation includes: comparing the standard deviation with a predetermined threshold; if the standard deviation is greater than or equal to the predetermined threshold, determining that the input text is an adversarial sample; if the standard deviation is less than the predetermined threshold, determining that the input text is not an adversarial sample.
[0007] In one possible implementation manner, segmenting the input text into a sequence of segments includes: converting the input text into a sequence of tokens; and segmenting the sequence of tokens into a sequence of segments using a sliding window of a predetermined width.
[0008] In one possible implementation, calculating the log-likelihood sequence corresponding to the fragment sequence includes: calculating the probability sequence corresponding to the fragment sequence through a pre-trained model; and calculating the log-likelihood sequence corresponding to the fragment sequence based on the probability sequence corresponding to the fragment sequence.
[0009] In one possible implementation, calculating the probability sequence corresponding to the fragment sequence through a pre-trained model includes: calculating the probability of the predetermined element appearing at a predetermined position based on the elements other than the predetermined element in each fragment of the fragment sequence through a pre-trained model; wherein the predetermined element is an element at the predetermined position, and the probability of the predetermined element appearing at the predetermined position is the probability corresponding to the fragment.
[0010] In the second aspect, an embodiment of the present application provides a detection device for adversarial samples, which is included in an electronic device, and has the function of implementing the behavior of the electronic device in the first aspect and the possible implementation of the first aspect. The function can be implemented by hardware, or by hardware executing corresponding software. The hardware or software includes one or more modules or units corresponding to the above functions. For example, an acquisition module, a segmentation module, a calculation module, and a detection module.
[0011] In a third aspect, an embodiment of the present application provides an electronic device, comprising: one or more processors; a memory; multiple applications; and one or more computer programs, wherein the one or more computer programs are stored in the memory, and the one or more computer programs include instructions, which, when executed by the electronic device, enable the electronic device to execute the method provided in the first aspect.
[0012] It should be understood that the second and third aspects of the embodiments of the present application are consistent with the technical solutions of the first aspect of the embodiments of the present application, and the beneficial effects achieved by each aspect and the corresponding feasible implementation methods are similar and will not be repeated here.
[0013] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, in which a computer program is stored. When the computer-readable storage medium is run on a computer, the computer executes the method provided in the first aspect.
[0014] In a fifth aspect, an embodiment of the present application provides a computer program, which, when executed by a computer, is used to execute the method provided in the first aspect.
[0015] In one possible design, the program in the fifth aspect may be stored in whole or in part on a storage medium packaged together with the processor, or may be stored in whole or in part on a memory not packaged together with the processor. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 A schematic diagram of the structure of an electronic device provided by an embodiment of the present application;
[0017] Figure 2 A schematic diagram of an application scenario provided for an embodiment of the present application;
[0018] Figure 3 A flowchart of a method for detecting adversarial samples provided in one embodiment of the present application;
[0019] Figure 4 A schematic diagram of the structure of an electronic device provided in another embodiment of the present application;
[0020] Figure 5 A schematic diagram of the structure of an electronic device provided in yet another embodiment of the present application. DETAILED DESCRIPTION
[0021] The terms used in the implementation section of this application are only used to explain the specific embodiments of this application and are not intended to limit this application.
[0022] For the detection of adversarial text attacks, the existing related technologies provide two solutions, one is adversarial training and the other is adversarial detection, which are introduced below.
[0023] 1. Adversarial Training
[0024] In traditional machine learning research, adversarial training is a common method to deal with text adversarial. Adversarial training can be further divided into different subgroups based on data augmentation strategies. Some methods create adversarial samples by modifying individual words or characters in the input text, while others use separate models such as generative adversarial nets (GANs) to generate adversarial samples. After training, these models can create samples that the target model may misclassify. In addition, there is a method that generates a mixed training data set by splicing adversarial samples with the original data. Some methods choose to generate a set of adversarial examples and then insert the generated adversarial examples directly into the training data set, while other methods regard adversarial training as a regularizer to introduce perturbations inside the neural network during training.
[0025] For targeted adversarial attacks, targeted training can also be used. For example, fast gradient sign method (FGSM) adversarial training is a simple but effective method that adds perturbations along the gradient direction of the loss function to the input data to increase the model's resistance to adversarial attacks. In general, the core idea of adversarial training is to add robust noise to the data and training so that the model can better adapt to challenging inputs that simulate situations that the model may encounter in actual applications, thereby improving the model's robustness and ability to resist adversarial attacks.
[0026] However, adversarial training usually requires a lot of computing resources and time. Each training step requires generating adversarial perturbations on the input, which increases the complexity and cost of training. Choosing appropriate adversarial training strategies and parameter settings is even more complicated, and different adversarial attacks may require different training methods. This requires a lot of debugging and experimentation to find the right settings, which is particularly expensive for large language models. In addition, adversarial training may cause the model to overfit on the training data, causing it to perform poorly on natural data. Because when adversarial perturbations are introduced, the model may focus too much on noise rather than real features, so that the normal capabilities of the large model are also degraded, and the large model is difficult to retrain.
[0027] 2. Adversarial Detection
[0028] At present, there are also related technologies to identify whether the text input has been maliciously tampered with, reject the detected adversarial samples or eliminate potential adversarial tampering in the prediction stage. Defense against character-level attacks, such as spell checking, is used to detect spelling and grammatical errors and correct them before sending them to the model. For word-level attacks, there are also frequency-guided word substitutions (FGWS) to identify text adversarial examples based on the fact that adversarial attack algorithms prefer to use words that rarely appear in the training set of the victim model. In addition, there are also studies showing that the pre-trained masked language model can capture information about the manifold of normal samples after completing self-supervised pre-training on large-scale normal data. Therefore, it is possible to compare the difference between the previous and next texts by first performing a masking operation (part of the input in the sample will be masked) and then performing an unmasking operation, which is called masked language model detection (MLMD). MLMD utilizes a pre-trained masked language model to distinguish normal samples from adversarial samples by exploring changes in the manifold. For adversarial samples, MLMD uses a masking operation to push them away from their original manifold, but the subsequent unmasking operation maps the masked examples onto the normal example manifold.
[0029] However, using large language models for masking and unmasking operations may result in long delays and require large computing resources, which may be difficult to implement in actual industrial scenarios, especially when massive amounts of text need to be detected quickly. In addition, the manifold of a large language model may be very complex, containing richer language structures and text variations. The effectiveness of this solution is limited by the simplified model or assumptions of the manifold. If the manifold is very complex, the simplification will fail.
[0030] In order to solve the problem that the GCG adversarial suffix can bypass the model's rejection capability and control the large dialogue model to answer any questions asked by the user, causing the large model to output harmful content, the embodiment of the present application proposes a universal and efficient adversarial sample detection method. The adversarial sample detection method provided by the embodiment of the present application can correctly distinguish between adversarial samples and normal samples, is applicable to inputs of any length, and has high detection efficiency.
[0031] The adversarial sample detection method provided in the embodiments of the present application can be applied to electronic devices, wherein the above-mentioned electronic device can be a server, for example: a server deployed with a large speech model or a large dialogue model, and the server can be set in the cloud; or, the above-mentioned electronic device can be a smart phone, a tablet computer, a wearable device, a vehicle-mounted device, an augmented reality (AR) / virtual reality (VR) device, a laptop computer, an ultra-mobile personal computer (UMPC), a netbook or a personal digital assistant (PDA) and other devices; the embodiments of the present application do not impose any restrictions on the specific type of electronic device.
[0032] For example, Figure 1 A schematic diagram of the structure of an electronic device provided by an embodiment of the present application is shown as follows: Figure 1 As shown, the electronic device 100 may include a processor 110 and a communication interface 120. Optionally, the electronic device 100 may also include a memory 130. The processor 110, the communication interface 120 and the memory 130 may communicate with each other through an internal connection path to transmit control and / or data signals, and the memory 130 is used to store a computer program, and the processor 110 is used to call and run the computer program from the memory 130.
[0033] The processor 110 and the memory 130 may be combined into a processing device, or more commonly, they are independent components, and the processor 110 is used to execute program codes stored in the memory 130. In specific implementation, the memory 130 may also be integrated into the processor 110, or may be independent of the processor 110.
[0034] In addition, in order to make the functions of the electronic device 100 more complete, the electronic device 100 may further include one or more of an input unit 160 and a display unit 170 .
[0035] Optionally, the electronic device 100 may further include a power supply 150 for providing power to various devices or circuits in the electronic device 100 .
[0036] It should be understood that Figure 1 The processor 110 in the electronic device 100 shown may be a system on chip SOC, and the processor 110 may include a central processing unit (CPU), and may further include other types of processors, such as a graphics processing unit (GPU) and the like.
[0037] For ease of understanding, the following embodiments of the present application will be described in detail with Figure 1 Taking the electronic device with the structure shown as an example, the adversarial sample detection method provided in the embodiment of the present application is specifically described in combination with the accompanying drawings and application scenarios.
[0038] GCG adversarial texts are often iterated by converting elements in a random sequence into tokens one by one. The semantics of the adversarial suffixes are quite different from normal texts, making them difficult for people to read or understand. Therefore, the degree of abnormal semantics can be evaluated to determine whether they have adversarial purposes. Abnormal semantics can be detected by the evaluation indicators of the language model, such as semantic perplexity. After a language model is pre-trained, the semantic perplexity calculated for a semantically natural test set should be stable and low, so a good language model can be used in this way to evaluate the semantic fluency of a sentence in reverse, but perplexity lacks robustness for sentences of different lengths. The method provided in the embodiment of the present application segments the input text, and then calculates the log-likelihood for each segment. Under the action of adversarial noise, different segments of the same text will change due to the presence of adversarial noise, making the distribution offset of fluency much larger than that of ordinary sentences. Therefore, the embodiment of the present application characterizes the distribution offset of the local fluency of the input text by counting the standard deviation of different segments, so as to determine whether the input text is a GCG adversarial sample.
[0039] The security capabilities of the dialogue model rely on a large amount of security data fine-tuning and rejection capability enhancement training to build, and the security mechanism of the dialogue model can be cracked by adding adversarial suffixes to the original sentence. This adversarial attack can be effectively migrated to different dialogue models, even if different dialogue models use different tokens, training processes and / or data sets. The method provided in the embodiment of the present application detects adversarial samples by utilizing the characteristic that the semantic fluency distribution deviation of adversarial samples is larger than that of normal samples, thereby preventing adversarial samples from being directly input into the dialogue model and preventing the dialogue model from outputting unsafe content. Figure 2 This is a schematic diagram of an application scenario provided by an embodiment of the present application. When it is implemented specifically, Figure 2 As shown, the application form of the method provided in the embodiment of the present application can be a module for adversarial sample detection in a large dialogue model, which is set before the large dialogue model.
[0040] Figure 3 This is a flow chart of a method for detecting adversarial samples provided by an embodiment of the present application. The method for detecting adversarial samples provided by this embodiment can be executed by the electronic device 100, such as Figure 3 As shown, the above adversarial sample detection method may include:
[0041] Step 301, obtaining input text.
[0042] Step 302: segment the input text into a sequence of segments.
[0043] Specifically, the input text is divided into a sequence of segments by converting the input text into a sequence of tokens, and then using a sliding window of a predetermined width to divide the sequence of tokens into a sequence of segments. The predetermined width can be set according to the implementation requirements during the specific implementation. The present embodiment does not limit the size of the predetermined width. For example, the predetermined width can be 10.
[0044] For example, after getting the input text, you can convert the input text into a token sequence [x 1 , x 2 , ..., x k ]; Then use a sliding window with width w to split the above token sequence into fragment sequences {x i:i+w , i∈[1,n]}, where n is the number of segments into which the above token sequence is divided.
[0045] In this embodiment, a sliding window of a predetermined width is used to divide the token sequence into fragment sequences, which can eliminate the influence of the length on the likelihood distribution.
[0046] Step 303: Calculate the log-likelihood sequence corresponding to the segment sequence, and calculate the standard deviation of the log-likelihood sequence.
[0047] Specifically, the log-likelihood sequence corresponding to the above-mentioned fragment sequence can be calculated by: calculating the probability sequence corresponding to the above-mentioned fragment sequence through a pre-trained model; and then calculating the log-likelihood sequence corresponding to the above-mentioned fragment sequence according to the probability sequence corresponding to the above-mentioned fragment sequence. The above-mentioned pre-trained model can be a neural network model, for example: bidirectional encoder representations from transformers (BERT) / second-generation generative pre-trained transformer 2 (GPT2) model.
[0048] Still with the token sequence [x 1 , x 2 , ..., x k ] as an example, the above token sequence is divided into fragment sequences {x i:i+w , i∈[1,n]}, then the above fragment sequences are input into the above pre-training model in sequence to obtain the probability sequence p corresponding to the above fragment sequence θ (xi+w |x i:i+w ). Then, the log-likelihood sequence corresponding to the above-mentioned fragment sequence can be calculated according to the probability sequence corresponding to the above-mentioned fragment sequence, as shown in formula (1).
[0049] l i =logp θ (x i+w |x i:i+w ) (1)
[0050] In this way, the obtained log-likelihood sequence can be [l 1 , l 2 , ..., l n ].
[0051] Next, the standard deviation of the above log-likelihood sequence can be calculated according to formula (2).
[0052]
[0053] In formula (2), s is the standard deviation of the above log-likelihood sequence.
[0054] In some examples, the probability sequence corresponding to the above-mentioned fragment sequence can be calculated by the pre-trained model: the probability of the above-mentioned predetermined element appearing at the predetermined position is calculated according to the elements other than the predetermined element in each fragment of the above-mentioned fragment sequence by the pre-trained model; wherein the above-mentioned predetermined element is the element at the above-mentioned predetermined position, and the probability of the above-mentioned predetermined element appearing at the predetermined position is the probability corresponding to the above-mentioned fragment. In this way, the probabilities corresponding to all the fragments of the above-mentioned fragment sequence can constitute the probability sequence corresponding to the above-mentioned fragment sequence. In specific implementation, the above-mentioned predetermined position can be the last position of the above-mentioned fragment sequence. Of course, this embodiment is not limited to this. The above-mentioned predetermined position can also be other positions in the above-mentioned fragment sequence. This embodiment is not limited to this.
[0055] Step 304: Obtain a detection result of whether the input text is an adversarial sample based on the standard deviation.
[0056] Specifically, according to the standard deviation, the detection result of whether the input text is an adversarial sample can be obtained by comparing the standard deviation with a predetermined threshold; if the standard deviation is greater than or equal to the predetermined threshold, the input text is determined to be an adversarial sample; if the standard deviation is less than the predetermined threshold, the input text is determined not to be an adversarial sample. The predetermined threshold can be set according to the implementation requirements during the specific implementation. This embodiment does not limit the size of the predetermined threshold. For example, the predetermined threshold can be 1.8154.
[0057] In the above-mentioned adversarial sample detection method, after the electronic device 100 obtains the input text, it divides the above-mentioned input text into a fragment sequence, and then calculates the log-likelihood sequence corresponding to the above-mentioned fragment sequence, and calculates the standard deviation of the above-mentioned log-likelihood sequence. Finally, based on the above-mentioned standard deviation, the detection result of whether the above-mentioned input text is an adversarial sample is obtained, so that the electronic device 100 can correctly distinguish between adversarial samples and normal samples, and can be applicable to input texts of any length, thereby improving the detection efficiency of adversarial samples.
[0058] The following is an explanation of the adversarial sample detection method provided in the embodiments of the present application through specific examples.
[0059] Step 1: Get input text and convert the input text into a token sequence.
[0060] Assume the input text is "How to make a bomb? Danreaslopedia snippet BluesMYQ###ListItemASE Sure whenagi After obtaining the input text, it will be converted into a token sequence [x 1 , x 2 , ..., x k ], as shown in Table 1.
[0061] Table 1
[0062]
[0063] Step 2: Use a sliding window to split the above token sequence into fragment sequences.
[0064] Specifically, a sliding window of length w can be used to segment the token sequence obtained in step 1 into a sequence of n text segments of length w. In this embodiment, w=10. The segment sequence {x i:i+w , i∈[1,n]}, as shown in Table 2.
[0065] Table 2
[0066]
[0067] There are 40 fragment sequences obtained by sliding window processing, and the length of each fragment is 10.
[0068] Step 3: Calculate the probability sequence corresponding to the above fragment sequence through the pre-trained model.
[0069] Specifically, the fragment sequence obtained in step 2 is input into the BERT / GPT2 model in sequence to obtain the probability sequence corresponding to the above fragment sequence.
[0070] In some examples, the probability sequence corresponding to the above-mentioned fragment sequence can be calculated by the pre-trained model: through the pre-trained model, according to the elements except the last element in each fragment, the probability of the last element appearing in the last position of the fragment is calculated; this probability is the probability corresponding to the fragment. In this way, after calculating the probabilities corresponding to all fragments, the probabilities corresponding to all fragments of the above-mentioned fragment sequence can form the probability sequence corresponding to the above-mentioned fragment sequence. In Table 2, the probability corresponding to the first fragment [36685, 224, 19526, 243, 26344, 114, 34460, 254, 31660, 10310] can be: through the pre-trained model, according to "36685, 224, 19526, 243, 26344, 114, 34460, 254, 31660", the probability of the element "10310" appearing in the last position of the first fragment is calculated, and the calculated probability is the probability corresponding to the first fragment.
[0071] Step 4: Calculate the log-likelihood sequence corresponding to the above-mentioned fragment sequence according to the probability sequence corresponding to the above-mentioned fragment sequence.
[0072] As described above, the log-likelihood sequence corresponding to the above segment sequence can be calculated according to formula (1). In this embodiment, the calculated log-likelihood sequence can be shown in Table 3.
[0073] Table 3
[0074]
[0075] Step 5, calculate the standard deviation of the above log-likelihood sequence.
[0076] Specifically, the standard deviation s of the log-likelihood sequence can be calculated according to formula (2). After calculation, in this embodiment, s=2.2598.
[0077] Step 6: According to the standard deviation, a detection result of whether the input text is an adversarial sample is obtained.
[0078] Specifically, the predetermined threshold τ=1.8154, if s is greater than or equal to 1.8154, the input text can be determined to be an adversarial sample; if s is less than 1.8154, the input text can be determined to be a normal sample. In this example, s>τ, so the input text can be determined to be an adversarial sample.
[0079] The adversarial sample detection method provided in the embodiment of the present application utilizes the low semantic fluency of adversarial samples and determines whether the input text is an adversarial sample by calculating the standard deviation of the log-likelihood sequence of the input text, thereby realizing efficient detection of adversarial samples. In addition, the adversarial sample detection method provided in the embodiment of the present application is applicable to input texts of arbitrary length. By dividing the input text into segments of fixed length, the influence of length on the likelihood distribution can be eliminated. The model parameters of this method are small and easy to deploy. Using a model with 12 to 150M parameters, adversarial samples can be efficiently detected.
[0080] It is to be understood that some or all of the steps or operations in the above embodiments are merely examples, and the present application embodiments may also perform other operations or variations of various operations. In addition, the various steps may be performed in different orders presented in the above embodiments, and it is possible that not all of the operations in the above embodiments need to be performed.
[0081] It is understandable that, in order to realize the above functions, the electronic device includes hardware and / or software modules corresponding to the execution of each function. In combination with the algorithm steps of each example described in the embodiments disclosed in this application, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in the form of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application in combination with the embodiments, but such implementation should not be considered to exceed the scope of this application.
[0082] This embodiment can divide the electronic device into functional modules according to the above method embodiment. For example, each functional module can be divided according to each function, or two or more functions can be integrated into one module. The above integrated module can be implemented in the form of hardware. It should be noted that the division of modules in this embodiment is schematic and is only a logical function division. There may be other division methods in actual implementation.
[0083] Figure 4 A schematic diagram of the structure of an electronic device provided in another embodiment of the present application, in which each functional module is divided according to each function, Figure 4 A possible schematic diagram of the composition of the electronic device 400 involved in the above embodiment is shown. Figure 4 As shown, the electronic device 400 may include: an acquisition module 401, a segmentation module 402, a calculation module 403 and a detection module 404;
[0084] Wherein, the acquisition module 401 is used to acquire input text;
[0085] A segmentation module 402 is used to segment the input text into a sequence of segments;
[0086] A calculation module 403 is used to calculate the log-likelihood sequence corresponding to the segment sequence and the standard deviation of the log-likelihood sequence;
[0087] The detection module 404 is used to obtain a detection result of whether the input text is an adversarial sample according to the above standard deviation.
[0088] It should be noted that this application Figure 3 All relevant contents of each step involved in the illustrated method embodiment can be referred to the functional description of the corresponding functional module and will not be repeated here.
[0089] The electronic device 400 provided in this embodiment is used to execute the present application Figure 3 The adversarial sample detection method provided by the illustrated embodiment can therefore achieve the same effect as the above method.
[0090] Figure 5 A schematic diagram of the structure of an electronic device provided in another embodiment of the present application, Figure 4 The electronic device shown is different in that Figure 5 In the electronic device 400 shown, the detection module 404 may include: a comparison submodule 4041 and a determination submodule 4042;
[0091] Wherein, the comparison submodule 4041 is used to compare the above standard deviation with a predetermined threshold;
[0092] The determination submodule 4042 is used to determine that the input text is an adversarial sample when the standard deviation is greater than or equal to a predetermined threshold; and to determine that the input text is not an adversarial sample when the standard deviation is less than the predetermined threshold.
[0093] In this embodiment, the segmentation module 402 may include: a conversion submodule 4021 and a segment segmentation submodule 4022;
[0094] Wherein, the conversion submodule 4021 is used to convert the input text into a token sequence;
[0095] The segmentation submodule 4022 is used to segment the token sequence into segment sequences using a sliding window of a predetermined width.
[0096] In this embodiment, the calculation module 403 is specifically used to calculate the probability sequence corresponding to the fragment sequence through the pre-training model; and calculate the log-likelihood sequence corresponding to the fragment sequence according to the probability sequence corresponding to the fragment sequence.
[0097] In specific implementation, the calculation module 403 is specifically used to calculate the probability of the above-mentioned predetermined element appearing at the predetermined position based on the elements other than the predetermined element in each fragment of the above-mentioned fragment sequence through a pre-trained model; wherein the above-mentioned predetermined element is an element at a predetermined position, and the probability of the above-mentioned predetermined element appearing at the predetermined position is the probability corresponding to the above-mentioned fragment.
[0098] It should be noted that this application Figure 3 All relevant contents of each step involved in the illustrated method embodiment can be referred to the functional description of the corresponding functional module and will not be repeated here.
[0099] The electronic device 400 provided in this embodiment is used to execute the present application Figure 3 The adversarial sample detection method provided by the illustrated embodiment can therefore achieve the same effect as the above method.
[0100] It should be understood that the electronic device 400 may correspond to Figure 1 The electronic device 100 shown in FIG. The functions of the acquisition module 401, the segmentation module 402, the calculation module 403 and the detection module 404 can be Figure 1 The processor 110 in the electronic device 100 is shown implemented.
[0101] In the case of adopting an integrated unit, the electronic device 400 may include a processing module, a storage module, and a communication module.
[0102] The processing module can be used to control and manage the actions of the electronic device 400, for example, it can be used to support the electronic device 400 to execute the steps executed by the acquisition module 401, the segmentation module 402, the calculation module 403 and the detection module 404. The storage module can be used to support the electronic device 400 to store program codes and data, etc. The communication module can be used to support the communication between the electronic device 400 and other devices.
[0103] Among them, the processing module can be a processor or a controller, which can implement or execute various exemplary logic boxes, modules and circuits described in conjunction with the disclosure of this application. The processor can also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of digital signal processing (DSP) and a microprocessor, and so on. The storage module can be a memory. The communication module can specifically be a device that interacts with other electronic devices, such as a radio frequency circuit, a Bluetooth chip and / or a Wi-Fi chip.
[0104] In one embodiment, when the processing module is a processor and the storage module is a memory, the electronic device 400 involved in this embodiment can be a Figure 1 Device of the structure shown.
[0105] The present application also provides a computer-readable storage medium, in which a computer program is stored. When the computer-readable storage medium is run on a computer, the computer executes the present application. Figure 3 The method provided by the illustrated embodiment.
[0106] The present application also provides a computer program product, which includes a computer program, which, when executed on a computer, enables the computer to execute the present application. Figure 3 The method provided by the illustrated embodiment.
[0107] In the embodiments of the present application, "at least one" refers to one or more, and "more than one" refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent the existence of A alone, the existence of A and B at the same time, and the existence of B alone. Among them, A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following" and similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one of a, b and c can be represented by: a, b, c, a and b, a and c, b and c, or a and b and c, where a, b, c can be single or multiple.
[0108] Those of ordinary skill in the art will appreciate that the various units and algorithm steps described in the embodiments disclosed herein can be implemented in a combination of electronic hardware, computer software, and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0109] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0110] In several embodiments provided in the present application, any function can be stored in a computer-readable storage medium if it is implemented in the form of a software functional unit and sold or used as an independent product. Based on this understanding, the technical solution of the present application can be embodied in the form of a software product in essence or in other words, the part that contributes to the prior art or the part of the technical solution. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0111] The above is only a specific implementation of the present application. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. The protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A method for detecting adversarial samples, characterized in that: include: Get input text; Segmenting the input text into a sequence of segments; Calculating a log-likelihood sequence corresponding to the fragment sequence, and calculating a standard deviation of the log-likelihood sequence; According to the standard deviation, a detection result of whether the input text is an adversarial sample is obtained.
2. The method according to claim 1, characterized in that The step of obtaining a detection result of whether the input text is an adversarial sample according to the standard deviation includes: comparing the standard deviation to a predetermined threshold; If the standard deviation is greater than or equal to the predetermined threshold, the input text is determined to be an adversarial sample; if the standard deviation is less than the predetermined threshold, the input text is determined not to be an adversarial sample.
3. The method according to claim 1, characterized in that The step of segmenting the input text into a sequence of segments comprises: converting the input text into a sequence of tokens; The token sequence is segmented into fragment sequences using a sliding window of a predetermined width.
4. The method according to claim 1, characterized in that: The calculating the log-likelihood sequence corresponding to the fragment sequence comprises: Calculating the probability sequence corresponding to the fragment sequence through the pre-training model; The log-likelihood sequence corresponding to the fragment sequence is calculated according to the probability sequence corresponding to the fragment sequence.
5. The method according to claim 4, characterized in that The calculating the probability sequence corresponding to the fragment sequence by the pre-trained model comprises: Through the pre-trained model, the probability of the predetermined element appearing at the predetermined position is calculated based on the elements other than the predetermined element in each segment of the segment sequence; wherein the predetermined element is the element at the predetermined position, and the probability of the predetermined element appearing at the predetermined position is the probability corresponding to the segment.
6. A detection device for adversarial samples, characterized in that: include: The acquisition module is used to obtain the input text; A segmentation module, used for segmenting the input text into a sequence of segments; A calculation module, used to calculate the log-likelihood sequence corresponding to the segment sequence, and calculate the standard deviation of the log-likelihood sequence; A detection module is used to obtain a detection result of whether the input text is an adversarial sample based on the standard deviation.
7. An electronic device, characterized in that: include: one or more processors; Memory; Multiple applications; and one or more computer programs, wherein the one or more computer programs are stored in the memory, and the one or more computer programs include instructions, which, when executed by the electronic device, enable the electronic device to perform the method as described in any one of claims 1-5.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, which, when executed on a computer, enables the computer to execute the method according to any one of claims 1 to 5.