A large model defense method based on semantic logic
By using a semantic logic-based approach, a new language model is trained by replacing the sensitive layer with a linear regression classifier and a low-rank adaptive decomposer (LoRA). This solves the problem of bias identification and blocking in large language models, achieves unbiased output and efficient resource utilization, and enhances the model's interpretability and cross-task adaptability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- ZHEJIANG UNIV
- Filing Date
- 2024-12-30
- Publication Date
- 2026-04-17
AI Technical Summary
Existing technologies have failed to effectively identify and block biases in pre-training data in large-scale language models, resulting in undesirable attributes and biases in model outputs, a lack of interpretability, a large amount of manual annotation and computational resources, and limited cross-task generalization ability.
A semantic logic-based approach is adopted, replacing the sensitive layer with a linear regression classifier and a low-rank adaptive decomposer LoRA, and combining unbiased paraphrasing and prediction reconstruction loss functions to train a new language model, reducing the influence of bias and improving the model's transparency and generalization ability.
It reduces bias in model output, improves model interpretability and resource efficiency, enhances generalization ability across different tasks and domains, and provides insights into the consistency between generated output and expected direction.
Smart Images

Figure CN119378613B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence security, specifically to a large model defense method based on semantic logic. Background Technology
[0002] By analyzing semantic logic, this study addresses potential undesirable properties or biases that large language models (LLMs) may exhibit in natural language tasks, such as the impact of semantic bias, the unbiasedness of guided representations, the interpretability of the guided process, and improving the security and reliability of LLMs.
[0003] For example, semantically segmenting a sentence input into an LLM will generate many small semantic variables. Let's say there are three semantic variables C, X, and Y. Figure 1 As shown.
[0004] If C is a common cause of both X and Y, then C is called a confounding variable. For example... Figure 1 As stated, in this graph, the path from X to Y forms a backdoor path through C, because C affects both X and Y, thus introducing a spurious correlation between X and Y.
[0005] Figure 2 This demonstrates how to eliminate the influence of confounding variables by blocking backdoor paths. In this diagram, by blocking the arrow from X to Z (i.e., blocking the backdoor path), we attempt to eliminate the interference of C on the causal relationship between X and Y. This allows for a more accurate estimation of the semantic logical effect of X on Y. This example illustrates how to identify and block the influence of bias in large language models. Our method is based on this semantic logic to identify and block sources of bias that may affect the model output, thereby obtaining a more accurate and unbiased guided representation. In this way, we can reduce bias in the model output and improve the model's safety and reliability.
[0006] Fine-tuning on Curated Datasets: This approach reduces the likelihood of the model generating harmful output by training the language model on filtered and cleaned datasets to remove offensive, biased, or misleading content. This method may not capture all subtle biases and requires significant human annotation and computational resources.
[0007] Reinforcement learning from human feedback (RLHF) utilizes human evaluators to provide feedback on the quality and safety of the generated output, thereby training the language model. This method requires a large amount of human feedback, is costly, and may introduce human bias.
[0008] Reinforcement Learning from AI Feedback (RLAIF) uses AI systems to provide feedback instead of human evaluators to train language models. While this method reduces human costs, it still requires significant computational resources and may lack the intuitive understanding and judgment of humans.
[0009] The closest existing techniques often do not fully consider the impact of biases in pre-training data on the guidance process, which may cause the guidance vector to be confused by these biases and thus fail to accurately capture the required attributes; existing methods lack interpretability, which limits the consistency and generalization ability of model performance and makes the model's decision-making process less transparent; they require a large amount of labeled data and computational resources, especially in reinforcement learning based on human feedback, where the human cost is very high; existing methods may perform well on specific tasks, but have limitations in their ability to generalize across tasks or domains. Summary of the Invention
[0010] To address the shortcomings of existing technologies, this invention provides a text-enhanced image generation method based on a diffusion model. This invention is achieved through the following technical solution:
[0011] This invention discloses a large model defense method based on semantic logic, comprising:
[0012] S1: Obtain sufficient training text;
[0013] S2: Input the text into the Transformer layer to obtain the feature map calculated by each layer in the Transformer layer;
[0014] S3: Using a linear regression classifier The feature maps calculated for each layer are evaluated, and the linear regression classifier is used for evaluation. After calculating each feature map, a vector is obtained. The values in the vector represent the probability of being classified into the corresponding category, resulting in several vectors.
[0015] S4: Sort several vectors from low to high to determine which layers are most sensitive to the target attribute, thus obtaining several intervention layers;
[0016] S5: Replace several intervention layers with a low-rank adaptive decomposer LoRA to obtain a new language model;
[0017] S6: Use the unbiased paraphrased data to train a new language model and obtain the trained language model.
[0018] S7: Obtain interactive text;
[0019] S8: Extract representations from the interactive text to obtain the extracted representations.
[0020] S9: Input the extracted text into the trained language model to obtain a safe and unbiased output.
[0021] As a further improvement, the present invention uses a low-rank adaptive decomposer (LoRA) to replace several intervention layers to obtain a new language model. Specifically, if the original intermediate layer is... The replaced unbiased intermediate layer is Mathematically, this is expressed as:
[0022]
[0023] Where, B and A are respectively of size and The matrix, and , This indicates the layer preceding the intermediate layer.
[0024] As a further improvement, the unbiased description described in this invention specifically means: the unbiased description specifically means: given input prompts Where S is a prefix hint and C is a semantic hint, the token lengths of S and C are calculated first. and ,when After the first step of the original language model When layering, an intermediate representation is obtained. After the above-mentioned first After the LoRA layer, we get an intermediate representation that has undergone iteration. Define the set of intermediate representations of the original language as R = [ Let Rˆ = {} },in Indicates the non-intervention layer. Indicates the intervention layer. ={ },in This is the non-intervention layer. ={ },in For the intervention layer, define the domain detection module: [- : ] is equivalent to the semantic prompt part of the entire prompt.
[0025] As a further improvement, the training of the new language model described in this invention specifically involves: the training is bias-reduction training, which is achieved by defining a prediction reconstruction loss: And removing bias loss: This forms the final loss function: The model is trained based on the aforementioned loss function, where CEloss represents the cross-entropy function. This represents the output of the original model. Representing a new language model, This represents the input after unbiased paraphrasing. This indicates the use of a gradient inversion layer to reverse the direction of the gradient, so that the gradient direction is reversed during backpropagation. This refers to a multilayer perceptron used to identify confused concepts. and It's a hyperparameter. The output direction label indicates whether the attribute or concept is expected or not expected, rather than a specific category (e.g., true or false, harmful or harmless, etc.).
[0026] As a further improvement, the extraction representation in S8 of this invention specifically involves using LoRA to calculate the difference between the positive and negative debiased representations for each label, and then calculating the average of these values to obtain the guiding direction, i.e.:
[0027] .
[0028] The beneficial effects of this invention are as follows:
[0029] (1) Reduce the impact of bias. This method systematically identifies and blocks the confusion effect of bias by using semantic logic analysis and model learning techniques, thereby reducing the impact of bias in the pre-training data on the guidance process.
[0030] (2) Improve interpretability. The method includes an interpretable component that provides insight into the alignment between the generated output and the desired direction, enhancing the interpretability of the bootstrapping process and increasing the transparency of the model and user trust.
[0031] (3) Improved resource efficiency. Compared with the RLHF and RLAIF methods, which require a lot of manual feedback, this method may reduce the dependence on manual annotation and computational resources, thereby improving resource efficiency.
[0032] (4) Strong generalization ability. This method improves the generalization ability of the model in different tasks and domains through unbiased guided representation, which contrasts with the limited generalization ability of existing technologies and has high scalability and adaptability.
[0033] (5) A semantic logic analysis was performed on the turning process in the language model to determine the confounding effect of semantic prompts and its impact on the turning representation. Finally, a new framework for obtaining unbiased turning representations in the language model was proposed. This framework innovatively uses adversarial learning techniques to separate the influence of semantic bias on the turning process and can effectively avoid the language model from generating biased outputs.
[0034] (6) The framework is an interpretable component that can be applied to different language models to gain a deeper understanding of the consistency between the generated output and the expected direction, thereby enhancing the interpretability of the turning process and mitigating the impact of bias. Attached Figure Description
[0035] Figure 1 This is a schematic diagram illustrating the source of the confusion variables in this invention;
[0036] Figure 2 This is a schematic diagram illustrating the elimination of confusing variables in this invention;
[0037] Figure 3 This is a flowchart illustrating the logical process of characterization extraction in this invention;
[0038] Figure 4 This is the overall flowchart of the processor of this invention. Detailed Implementation
[0039] The technical solution of the present invention will be further described below with reference to the accompanying drawings and specific embodiments:
[0040] Figure 4 This is an overall flowchart of a large-scale model defense method based on semantic logic according to the present invention.
[0041] This invention discloses a large model defense method based on semantic logic, comprising:
[0042] S1: Obtain sufficient training text;
[0043] S2: Input the text into the Transformer layer to obtain the feature map calculated by each layer in the Transformer layer;
[0044] S3: Using a linear regression classifier The feature maps calculated for each layer are evaluated, and the linear regression classifier is used for evaluation. After calculating each feature map, a vector is obtained. The values in the vector represent the probability of being classified into the corresponding category, resulting in several vectors.
[0045] S4: Sort the vectors from low to high to determine which layers are most sensitive to the target attribute, thus obtaining several intervention layers; use a linear regression classifier. Each layer of the language model is evaluated to determine which layers are most sensitive to the target attribute. Here, n is the length of the input sentence, d is the hidden dimension of a certain layer, and c is the number of target classification attributes. Using such a classifier to select the leading layers, these layers are called intervention layers.
[0046] S5: Replace several intervention layers with a low-rank adaptive decomposer LoRA to obtain a new language model; if the original intermediate layers are The replaced unbiased intermediate layer is Mathematically, this is expressed as:
[0047]
[0048] Where B and A are of size 1 and 2 respectively. and The matrix, and , This indicates the layer preceding the intermediate layer.
[0049] S6: Using the unbiased paraphrased data, train a new language model to obtain the trained language model; unbiased paraphrasing specifically means: given input prompts... Where S is a prefix hint and C is a semantic hint, the token lengths of S and C are calculated first. and ,when After the first step of the original language model When layering, an intermediate representation is obtained. After the above-mentioned first After the LoRA layer, we get an intermediate representation that has undergone iteration. Define the set of intermediate representations of the original language as R = [ Let Rˆ = { },in Indicates the non-intervention layer. Indicates the intervention layer. ={ },in This is the non-intervention layer. ={ },in For the intervention layer, define the domain detection module: [- : ] is equivalent to the semantic prompt part of the entire prompt.
[0050] Training the new language model specifically involves: the training being a bias-reduction training, which is achieved by defining a prediction reconstruction loss: And removing bias loss: This forms the final loss function: The model is trained based on the aforementioned loss function, where CEloss represents the cross-entropy function. This represents the output of the original model. Representing a new language model, This represents the input after unbiased paraphrasing. This indicates the use of a gradient inversion layer to reverse the direction of the gradient, so that the gradient direction is reversed during backpropagation. This refers to a multilayer perceptron used to identify confused concepts. and It's a hyperparameter. This function represents the directional label of the output, i.e., the expected or unexpected attribute or concept, rather than a specific category (e.g., true or false, harmful or harmless, etc.); with this function, the model can be trained.
[0051] S7: Obtain interactive text;
[0052] S8: Figure 3 This is a logical process diagram of the representation extraction in this invention; The interactive text is represented to obtain the text after representation extraction. Specifically, representation extraction involves using LoRA to calculate the difference between positive and negative unbiased representations for each marker, and then calculating the average of these values to obtain the guiding direction, i.e.:
[0053] .
[0054] S9: Input the extracted text into the trained language model to obtain a safe and unbiased output.
[0055] Finally, during the model's inference phase, the representation of the generated token for the current input is obtained, and the previously obtained guiding representation is applied to the current token representation through projection. Finally, the adjusted output can be evaluated to see if it meets expectations. If necessary, the model or guiding representation can be adjusted based on the evaluation results.
[0056] Example description
[0057] If we have an LLM (Large Model for Text Generation) and want the model to avoid gender bias when generating text, a semantic logic-based large model defense approach can help guide the model's output to be more neutral. To ensure the model generates a gender-neutral complete sentence, we first use a sufficient amount of gender-biased training text (e.g., text like "programmers are always male"). The processor then inputs this training text into a Transformer layer, obtaining all feature maps of the training samples after processing by each layer of the Transformer. Finally, a linear regression classifier is used. The feature maps calculated for each layer are evaluated, and the linear regression classifier is used for evaluation. After calculating each feature map, a vector is obtained, where the values represent the probability of being classified into the corresponding category. Several vectors are obtained. The processor sorts these vectors from low to high to determine which layers are most sensitive to the target attribute, resulting in several intervention layers. The processor automatically constructs and uses a low-rank adaptive decomposer (LoRA) to replace these intervention layers, resulting in a new language model. The new language model is trained using unbiased paraphrased data, resulting in the trained model. If the model is given the interactive text: "He is an excellent programmer because ___.", the final generated sentence is: "He is an excellent programmer because he always learns new technologies." In this example, the generated sentence is neutral and does not show gender bias. To avoid racial bias when generating descriptions of people, first use sufficient training text containing racial bias (such as "That suspicious person looks like race A"). The processor inputs the training text into a Transformer layer, obtaining all feature maps calculated by each layer of the Transformer layer for all training samples; then, a linear regression classifier is used... The feature maps calculated for each layer are evaluated, and the linear regression classifier is used for evaluation. After calculating each feature map, a vector is obtained, where the values represent the probability of being classified into the corresponding category. Several vectors are obtained. The processor sorts these vectors from low to high to determine which layers are most sensitive to the target attribute, resulting in several intervention layers. The processor automatically constructs and replaces these intervention layers with a low-rank adaptive decomposer (LoRA), resulting in a new language model. The new language model is trained using unbiased paraphrased data, resulting in the trained model. The model is then given the interactive text: "That suspicious person looks like ___." The model generates: "That suspicious person looks like an ordinary person." To avoid age bias when generating descriptions of job suitability, sufficient training text containing age bias (e.g., "That person's poor work ability is entirely due to his advanced age") is first used. The processor then inputs this training text into a Transformer layer, obtaining all feature maps calculated by each layer of the Transformer layer for all training samples; a linear regression classifier is then used to... The feature maps calculated for each layer are evaluated, and the linear regression classifier is used for evaluation. After calculating each feature map, a vector is obtained, where the values represent the probability of being classified into the corresponding category. Several vectors are generated. The processor sorts these vectors from low to high sensitivity to determine which layers are most sensitive to the target attribute, resulting in several intervention layers. The processor automatically constructs and replaces these intervention layers with a low-rank adaptive decomposer (LoRA), obtaining a new language model. The new language model is trained using unbiased paraphrased data, resulting in the trained model. The model is input with the interactive text: "This new employee adapts very well to a fast-paced work environment, although he ___." The model outputs: "This new employee adapts very well to a fast-paced work environment, although he has just joined the team."
[0058] It should be understood that the above description of the preferred embodiments is quite detailed, but it should not be considered as a limitation on the scope of protection of this invention. Those skilled in the art, under the guidance of this invention, can make substitutions or modifications without departing from the scope of protection of the claims of this invention, and all such substitutions or modifications fall within the scope of protection of this invention. The scope of protection of this invention should be determined by the appended claims.
Claims
1. A method for defending large models based on semantic logic, characterized in that, include: S1: Obtain sufficient training text; S2: Input the text into the Transformer layer to obtain the feature map calculated by each layer in the Transformer layer; S3: The feature map calculated at each layer is evaluated by a linear regression classifier g. After calculating each feature map, the linear regression classifier g can obtain a vector. The value in the vector represents the probability of being classified into the corresponding category, resulting in several vectors. S4: Sort several vectors from low to high to determine which layers are most sensitive to the target attribute, and obtain several intervention layers; S5: Use a low-rank adaptive decomposer LoRA to replace several intervention layers to obtain a new language model; S6: Use the unbiased paraphrased data to train a new language model and obtain the trained language model. S7: Obtain interactive text; S8: Extract representations from the interactive text to obtain the text after extraction and representation; S9: Input the extracted text into the trained language model to obtain a safe and unbiased output; the unbiased paraphrasing specifically involves: given an input cue I = [S, C], where S is a prefix guidance cue and C is a semantic cue, first calculate the token lengths of S and C, which are Ls and Lc respectively. When I passes through the l-th layer of the original language model, the intermediate representation r is obtained. l After passing through the l-th LoRA layer mentioned above, an intermediate representation that has undergone iterations will be obtained. Define the set of intermediate representations of the original language as R = [r 0 ,r 1 ,r l ,…,r D The set of intermediate representations after unbiased paraphrasing is... Where -* represents the non-intervention layer, * represents the intervention layer, and r -* ={r l1 }, where l1 is the non-intervention layer, Where l2 is the intervention layer, defining the domain detection module: This is equivalent to the semantic hint portion of the entire prompt; The method of replacing several intervention layers with a low-rank adaptive decomposer (LoRA) to obtain a new language model is as follows: if the original intermediate layer is r l The replaced unbiased intermediate layer is Mathematically represented as: where B and A are matrices of size d x m and m x d, respectively, and m « d, r l-1 previous layer representing the intermediate layer; The training of the new language model specifically refers to: the training is bias-reduction training, which is achieved by defining a prediction reconstruction loss: And removing bias loss: The final loss function is: L = L pre +αL debias The model is trained based on the aforementioned loss function, where CEloss represents the cross-entropy function, and y output This represents the output of the original model. Representing a new language model, The input is represented by the unbiased transcriber, GradRev indicates that the gradient inversion layer is used to reverse the direction of the gradient, so that the direction of the gradient is reversed during backpropagation, f represents the multilayer perceptron used to identify confused concepts, η and α are hyperparameters, and y direction The output direction label represents the expected or unexpected attribute or concept, rather than a specific category; the extraction representation in S8 specifically involves using LoRA to calculate the difference between positive and negative unbiased representations for each label, and then calculating the average of these values to obtain the guiding direction, i.e.: