Food safety risk assessment method based on small sample learning
By applying a prototype network model of small sample learning in the field of food safety, combined with the cosine attention mechanism, the accuracy problem of traditional methods in the case of scarcity of high-risk samples and data imbalance is solved, and high-precision food safety risk level prediction is achieved.
Patent Information
- Application Number
- CN202411595825.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-11
- Publication Date
- 2025-02-28
AI Technical Summary
Traditional food safety risk assessment methods are difficult to ensure the generalization ability and accuracy of the model when high-risk samples are scarce and data imbalance is imbalanced.
A prototype network model based on small sample learning is adopted, and the sample data is featured and classified through the cosine attention mechanism, the prototype vector of each category is constructed, and the risk level is divided according to the distance between the new sample and the prototype.
It effectively improves the identification ability of high-risk samples, improves the accuracy and robustness of food safety risk level prediction, and is suitable for safety monitoring of important foods such as grain, oil and food.
Smart Images

Figure CN119541701A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the intersection of food safety and machine learning, and in particular to a food safety risk assessment method based on Few-Shot Learning (FSL). Technical Background
[0002] Food safety is a hot issue of global concern, involving public health and social stability. As an important means to ensure food safety, food safety risk assessment has been widely used to identify and prevent potential risks in the food chain. Traditional food safety risk assessment methods mostly rely on large-scale and complete data sets, which may face many challenges in actual operations. Food safety testing data usually have problems such as data imbalance, outliers and missing data. Especially when there are few high-risk samples, the generalization ability and accuracy of traditional machine learning models are difficult to guarantee. In recent years, small sample learning has shown significant advantages in scenarios with scarce data. Small sample learning can infer unseen data by learning effective features from a small number of samples. This technology has been successful in many fields, including computer vision, biomedicine and natural language processing.
[0003] In the field of food safety, the number of high-risk samples is usually very limited. Traditional machine learning methods often cannot cope with these scarce high-risk samples because they require a large amount of labeled data to train the model. As a model designed for small sample learning, the prototype network has excellent performance by finding the prototype of each category in the feature space and classifying new samples according to their distance from these prototypes. The core advantage of the prototype network is that it can extract category feature vectors from a small number of labeled samples, which is particularly suitable for scenarios where high-risk samples are scarce.
[0004] In the prototype network model based on the cosine attention mechanism, the sample data is first mapped to a feature space through a deep neural network embedding module. Then, in order to more accurately construct the prototype of each category, CPNet calculates the weighted feature mean of each class of samples through cosine similarity, which is used as the prototype vector of the category. The category of the new sample is determined by its similarity with each prototype in the feature space, which is usually measured by cosine similarity. This cosine similarity replaces the traditional Euclidean distance and is more suitable for handling situations where the distribution of the support set and the query set is very different. CPNet can not only perform efficient classification under limited sample data conditions, but also enhance the robustness of the model under complex data distribution through the cosine attention mechanism, significantly reducing the risk of overfitting in high-risk sample shortage situations.
[0005] In order to solve the problems of scarcity of high-risk samples and data imbalance in food safety testing, the present invention proposes a food safety risk level prediction method based on small sample learning, focusing on the prototype network model to effectively deal with the limited nature of high-risk samples. This method extracts key features from a small number of high-risk samples through a prototype network, constructs a prototype vector for each category, and classifies the new sample according to the distance between the new sample and the prototype, thereby achieving high-precision risk level classification. Through this method, the recognition ability of high-risk samples can be effectively improved, which is suitable for the safety monitoring of important foods such as grains and oils, and provides a more accurate and efficient risk assessment solution. Summary of the invention
[0006] In the process of food safety risk assessment, there are problems such as the scarcity of high-risk samples and unbalanced data distribution. Traditional machine learning methods rely on a large amount of data for model training, so it is difficult to accurately assess food safety risks in the case of unbalanced data and insufficient samples. In particular, when data in high-risk categories is extremely scarce, the prediction performance of the model often drops significantly. At the same time, existing risk assessment methods have certain limitations in the extraction of sample features, resulting in insufficient generalization ability of the model. Based on this, the present invention discloses a food safety risk assessment method based on a prototype network algorithm in small sample learning, the purpose of which is to solve the problems of scarcity of high-risk samples, unbalanced data, insufficient model accuracy and robustness in traditional risk assessment methods, and can effectively improve prediction accuracy when data is limited, which is beneficial to food safety monitoring and decision-making guidance.
[0007] The food safety risk assessment method based on small sample learning provided by the present invention comprises the following steps:
[0008] Step 1) Construct a risk factor index system for processed grain products. The risk factor index system consists of two parallel branches, namely the key attribute index and the hazard index of processed grain products. The key attribute index includes the type of food, sampling location and sampling time, and each key attribute is refined into multiple third-level indicators; the hazard index system includes multiple categories such as heavy metals and mycotoxins, and each category is further subdivided into specific third-level indicators, such as the content of hazard such as lead and cadmium;
[0009] Step 2) Data preprocessing and cleaning. Standardize and preprocess the historical sampling data of processed grain products, delete invalid information not related to food safety, and use reasonable estimation methods to fill in the missing values in the test results. At the same time, for outliers and extreme values, adopt appropriate processing strategies to ensure the quality and consistency of the data;
[0010] Step 3) Weight calculation. The subjective weight and objective weight of the risk factors are calculated based on the stepwise weight assessment ratio analysis method (SWARA) and the CRITIC method. The subjective and objective weights are combined into a comprehensive weight through the weight combination method of game theory to provide a basis for the subsequent risk level classification;
[0011] Step 4) Risk level classification. The calculated comprehensive weight is input into the Gaussian mixture model (GMM) for clustering, and the samples are divided into five risk levels: micro-risk, low risk, medium risk, high risk and extremely high risk according to the content of key hazards in processed grain products. In order to ensure the objectivity of risk level classification, the three evaluation indicators of Bayesian information criterion, silhouette coefficient and Davis-Bodin index are used, and the quality of separation and homogenization under different numbers of clusters reflected by the three indicators is synthesized based on the voting method, so as to calculate the optimal number of clusters;
[0012] Step 5) Feature extraction and prototype network model training. The prototype network (ProtoNet) in the small sample learning method is used in combination with the cosine attention mechanism to extract features from the processed sample data. In the model embedding stage, CPNet introduces learnable parameters to improve the correlation between the support set features and the query set features, and uses cosine similarity to replace the traditional scaled dot product mechanism, thereby improving the accuracy of the model in mapping between complex data distributions and different sample sets. Through the above process, CPNet can effectively extract the key features of risk level samples, achieve high-precision food risk level classification when the sample size is scarce, and contribute to the intelligent development of food safety risk level assessment.
[0013] Step 6) Model optimization and evaluation: Train and optimize the prototype network model based on the cosine attention mechanism, evaluate the classification effect of the model through cross-validation and other methods, calculate the classification accuracy, precision, recall rate and F1 score, and ensure the robustness and reliability of the model in the case of high-risk sample scarcity.
[0014] In the step 1, extracting key features from a large amount of food safety sampling data is the basis for building a food safety risk assessment model. Taking the rice sampling data in grain processed products as an example, first, index screening is performed. There are many types of rice detection indicators, including acid value over-standard detection, peroxide value detection, solvent residue detection, metal pollution detection, chemical pollution detection and biological toxin detection. However, not all of the above-mentioned detection items can be used as risk assessment indicators for rice, and it is necessary to screen out indicators that have a greater impact on the risk level of rice. The present invention analyzes the detection of various indicators and selects six indicators of lead content, cadmium content, mercury content, chromium content, inorganic arsenic content and aflatoxin B1 content as key risk assessment indicators. Lead, cadmium, mercury, chromium and arsenic are common heavy metal pollutants in grain, and aflatoxin B1 is a common fungal toxin pollutant in grain. These six indicators can represent the corresponding categories of pollutants and are more easily detected than other risk indicators. Therefore, the risk level of grain can be basically judged by these six indicators.
[0015] In the step 2, the standardization of the test data includes: deleting useless information. Each sample has many descriptive characters. It is necessary to simplify this information to emphasize the characteristics that affect the degree of risk and eliminate food sensory indicators that have little to do with food safety, such as taste, color, shape, etc. 2) Delete redundant symbols. For example, the test results of a grain sample show that the content of a certain ingredient is lower than the detection limit, and the limit value of the ingredient is specified as not exceeding the maximum limit. Since the test result does not exceed the limit, remove the "<" symbol in the result and record the test value as the detection limit. Use the "method detection limit" instead of the undetected indicator data. This is to ensure that the obtained input matrix can be recognized by the evaluation model and is positive definite.
[0016] In step 3, the subjective weight of the risk factor is calculated using the SWARA method. First, the importance of the selected key risk factors should be ranked. The relative importance of each risk factor is ranked in turn through expert scoring or other methods. The most important risk factor is given the highest priority, and the remaining factors are ranked in order of importance. Each risk factor is weighted according to its relative importance. The calculation of the weight can consider the degree of influence of different factors and be adjusted in combination with expert opinions or historical data. Finally, the subjective weight of each factor is obtained. Then the CRITIC objective weighting method is used to determine the weight of the indicator. As an objective weight calculation method that reflects the correlation between indicators, the CRITIC method takes into account the variation and conflict between indicators when conducting multi-indicator evaluation, that is, it is calculated using standard deviation and correlation coefficient. Compared with the SWARA method, the CRITIC objective weighting method takes into account the correlation between indicators, making the objective weight more scientific. Finally, a mathematical model based on game theory is created. The weights calculated by the subjective weighting method and the objective weighting method are used as the strategy set of the two parties in the game. The equilibrium solution of game theory is used for optimization to achieve the optimal combination of weight distribution. This method comprehensively considers the relationship between contradiction and cooperation to obtain a more reasonable weight distribution result. Assume that there are N weighting methods, represented by ω1, ω2, …, ω n , by linearly combining these weight strategies, we get a comprehensive weight vector ω. By minimizing each strategy ω k The deviation between the comprehensive strategy ω and the optimal comprehensive weight is calculated, that is,
[0017]
[0018] The above formula is matrix processed to meet the optimized first-order derivative condition, and the coefficients (α1, α2, …, α n ), and finally get the optimal comprehensive weight ω * :
[0019]
[0020] In step 4, the GMM algorithm used is a distribution model based on probability density, which approximates the actual distribution of complex data through a linear combination of multiple Gaussian distributions. This model is suitable for dealing with situations where data distribution is uneven or has a multimodal distribution, and can better simulate the characteristics of food safety risk level samples. GMM assumes that food risk level data comes from a combination of several Gaussian distributions, so that it can effectively capture the potential structure of the data. Each Gaussian distribution represents a potential category of food safety risk level, and the weight parameter reflects the probability of occurrence of each category. By maximizing the likelihood estimate, GMM can learn the implicit distribution of the data and accurately classify the samples into the corresponding risk levels. Mathematically, the probability density function of GMM can be expressed as:
[0021]
[0022] Among them, π j represents the weight of the j-th risk level, μ j and∑ j Represent the mean vector and covariance matrix of the j-th risk level respectively. For the clustering results obtained by the GMM algorithm, three evaluation indicators, namely the Bayesian information criterion, the silhouette coefficient and the Davis-Bodin index, are used, and the quality of separation and homogeneity under different numbers of clusters reflected by the three indicators is comprehensively considered based on the voting method to calculate the optimal number of clusters. The Bayesian Information Criterion (BIC) is a criterion for model selection, which is based on the log-likelihood function of the model and combines a penalty term for model complexity. When the BIC value is small, it means that the model has achieved a good balance between data fitting and complexity. It is defined as:
[0023] BIC=-2ln(L)+pln(n) (4)
[0024] Among them, L represents the maximum likelihood estimate of the model, p is the number of parameters of the model, and n is the number of samples. The smaller the BIC value, the better the model. The silhouette coefficient is an indicator for evaluating the clustering effect. It measures the compactness and separation of clusters by comparing the distance between the sample point and its class and other classes. Its value range is [-1,1]. The closer the silhouette coefficient is to 1, the better the clustering effect. The definition of the silhouette coefficient is as follows:
[0025]
[0026] Among them, h(i) represents the average distance from sample i to other samples of the same class, and g(i) represents the average distance from sample i to the other nearest class. When S(i) is close to 1, it means that the clustering effect of sample i is good. The Davis-Bodin index is a function of the ratio of the sum of the intra-class distance and the average distance of any two clusters to the separation between different clusters. The smaller the index, the better the separation between clusters and the better the cohesion within the same cluster. The definition of this index is as follows:
[0027]
[0028] Wherein, formula (6) represents the average Euclidean distance C of k points in a cluster i Formula (7) calculates the cluster center and C i and C j Formula (8) calculates the DBI of N clusters.
[0029] After calculating the three clustering evaluation indicators, in order to improve the reliability of the evaluation method, a voting method is used, that is, based on the three clustering evaluation indicator values, the evaluation values of different cluster numbers are used to calculate the overall score according to the following scheme, where A is the largest number of clusters (taking A=5 as an example): the number of clusters with the best evaluation indicator performance is given A; the number of clusters with the second best performance is given (A-1); the number of clusters with the third best performance is given (A-2); the number of clusters with the fourth best performance is given (A-3); the number of clusters with the worst performance is given 1 point. After that, the clustering evaluation indicator scores under different cluster numbers are accumulated to determine the number of clusters with the global maximum score, so as to select the optimal grading scheme.
[0030] In step 5, the overall CPNet architecture has two main components: a learnable prototype representation and a cosine attention mechanism for small sample tasks. The learnable prototype representation computes the classification prototype representation based on random support set features, which may be at the far end of the distribution or very close to each other. Given a support set S = (X S ,Y S ) of the query Q = (X Q ,Y Q ) follows the few-shot setting (n-way, k-shot, q query samples), where Y S ,Y Q ∈n categories; X S ,X Q ∈R c×h×w ;X S and X Q First enter the feature extractor f(.|θ) to obtain the feature representation Z S ∈R n×h×w , Z Q ∈Rq×d Then, in class C, based on all k learned features Calculate the average (arithmetic mean) to obtain the prototype representation Z P ∈R n×d , where z c ∈Z P represents the centroid of category C. However, due to the small number of randomly selected support samples, when the embedding representation of the selected sample is far from the edge of the classification distribution space and too close to the embedding representation of other category samples, the calculated prototype representation is inaccurate. To solve this problem, it is recommended to convert the arithmetic mean into a weighted average, where the weight can be adjusted through the learning process, so that a more reasonable prototype representation can be obtained. The formula for the learnable prototype representation is as follows:
[0031]
[0032] Cosine Transformer, which determines the similarity matrix between the prototype representation and the query sample for few-shot classification tasks. The core of the transformer architecture is cosine attention, a cross-attention mechanism that uses cosine similarity and no softmax function to process two sets of different features. Given two vectors a, b∈R n , the cosine similarity between a and b is calculated by dividing the two dot product calculations by the product of their magnitudes, that is:
[0033]
[0034] According to the above vector formula, the definition of cosine similarity on the matrix is expanded. A and M BΤ There are two matrices A and B respectively. Τ The cosine similarity of two matrices is described as:
[0035]
[0036] According to this definition, S C Each element S of (A,B) C (A,B) i,j is the i-th row vector a of matrix A i and the j-th column vector b of matrix B j The cosine similarity score between , where i∈[1,n], j∈[1,m]. In summary, the query sample x in class C among n classes q The probability prediction p(C|h q,c θ out ), represents the score h q,c The calculation formula for the predicted label y is as follows:
[0037]
[0038] BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 is a flow chart of a food safety risk assessment method based on small sample learning of the present invention;
[0040] Figure 2 The method of the present invention uses an improved prototype network structure diagram based on the cosine attention mechanism;
[0041] Figure 3 It is a flow chart of the GMM clustering algorithm used in the method of the present invention to divide the risk levels;
[0042] Figure 4 It is the result graph of three clustering index value voting points of the GMM clustering algorithm of the present invention;
[0043] Figure 5 This is the experimental result diagram of the method of the present invention using the improved prototype network based on the cosine attention mechanism and the traditional machine learning method. DETAILED DESCRIPTION
[0044] The present invention will be further described in detail below with reference to the accompanying drawings and examples.
[0045] A method for food safety risk assessment based on small sample learning, such as Figure 1 As shown, the specific steps are as follows:
[0046] S1. Extracting key features from a large amount of food safety sampling data is the basis for building a food safety risk assessment model. Taking the rice sampling data of processed grain products as an example, the data mainly presents the following characteristics: 1) High dimensionality: The sampling information of each rice sample consists of different attributes, such as sampling number, origin information, product information, production date, inspection items, inspection results, critical limits, etc. These attributes are independent of each other, and many attributes such as sampling number are redundant for the judgment of risk level and need to be filtered out. 2) Discreteness: There are about dozens of detection indicators for a rice sample, one indicator dimension is different from another indicator dimension, and the national standards for each indicator are also different. This means that these indicators have different discrete domains, and the test results are also disordered in the discrete domain. Therefore, it is necessary to screen out the key indicators that affect the risk level from these indicators. 3) Nonlinearity: Each inspection information includes numerical information and descriptive information. The numerical information contains a large number of null values, and the descriptive information also contains some discrete values. In the complete inspection information, the information distribution between different attributes is asymmetric and unbalanced, which increases the difficulty of risk analysis. Therefore, the work of indicator screening is particularly important. First, index screening is performed. There are many types of rice detection indicators, including acid value over-standard detection, peroxide value detection, solvent residue detection, metal pollution detection, chemical pollution detection and biological toxin detection. Due to space limitations, only some of the original detection data are listed, as shown in Table 1. However, not all of the above-mentioned detection items can be used as risk assessment indicators for rice, and it is necessary to screen out indicators that have a greater impact on the risk level of rice. The present invention analyzes the detection of various indicators and selects six indicators of lead content, cadmium content, mercury content, chromium content, inorganic arsenic content and aflatoxin B1 content as key risk assessment indicators. Lead, cadmium, mercury, chromium and arsenic are common heavy metal pollutants in grains, and aflatoxin B1 is a common fungal toxin pollutant in grains. These six indicators can represent the corresponding categories of pollutants and are more easily detected than other risk indicators. Therefore, the risk level of grain can be basically judged by these six indicators.
[0047] Table 1. Raw data of rice
[0048]
[0049] S2. Standardization of test data includes: deleting useless information. Each sample has many descriptive characters. It is necessary to simplify this information to emphasize the characteristics that affect the degree of risk and eliminate food sensory indicators that have little to do with food safety, such as taste, color, shape, etc. 2) Delete redundant symbols. For example, the test results of a grain sample show that the content of a certain ingredient is lower than the detection limit, and the limit value of the ingredient is stipulated not to exceed the maximum limit. Since the test result does not exceed the limit, remove the "<" symbol in the result and record the test value as the detection limit. Use "method detection limit" instead of undetected indicator data. This is to ensure that the obtained input matrix can be recognized by the evaluation model and is positive definite. At this point, the screening of indicators and data preprocessing are completed, as shown in Table 2 and Table 3.
[0050] Table 2. Some original data of rice after index screening
[0051]
[0052] Table 3 Rice data after pretreatment
[0053]
[0054]
[0055] S3. In view of the problem that the existing national standard only has corresponding risk standards for a single hazard, but there is no clear pattern for the joint impact of multiple hazards on food safety risks, the SWARA-CRITIC method is used to calculate the subjective and objective weights of the six risk factors, and the game theory combined weight method is used to calculate the comprehensive weights of the six risk factors to facilitate the classification of risk levels.
[0056] The specific method is as follows:
[0057] S31. Use the SWARA method to calculate the subjective weight of risk factors. First, the importance of the selected key risk factors should be ranked. The relative importance of each risk factor is ranked in turn through expert scoring or other methods. The most important risk factor is given the highest priority, and the remaining factors are ranked in order of importance. Each risk factor is weighted according to its relative importance. The calculation of the weight can take into account the degree of influence of different factors and be adjusted in combination with expert opinions or historical data. Finally, the subjective weight of each factor is obtained.
[0058] S32. Use the CRITIC objective weighting method to determine the weight of the indicators. As an objective weight calculation method that reflects the correlation between indicators, the CRITIC method takes into account the variation and conflict between indicators when evaluating multiple indicators, that is, it uses standard deviation and correlation coefficient to calculate. Compared with the SWARA method, the CRITIC objective weighting method takes into account the correlation between indicators, making the objective weight more scientific.
[0059] S33. Create a mathematical model based on game theory, use the weights calculated by the subjective weighting method and the objective weighting method as the strategy set of the two parties in the game, and use the equilibrium solution of game theory to optimize to achieve the optimal combination of weight distribution. This method comprehensively considers the relationship between contradiction and cooperation to obtain a more reasonable weight distribution result. Assume that there are N weighting methods, represented by ω1, ω2, …, ω n , by linearly combining these weight strategies, we get a comprehensive weight vector ω. By minimizing each strategy ω k The deviation between the comprehensive strategy ω and the optimal comprehensive weight is calculated, that is,
[0060]
[0061] The above formula is matrix processed to meet the optimized first-order derivative condition, and the coefficients (α1, α2, …, α n ), and finally get the optimal comprehensive weight ω * :
[0062]
[0063] S4. Cluster the comprehensive risk weights using a clustering algorithm, as shown in Table 4, to obtain the risk level of each sample in a preliminary and coarse-grained manner. Specifically, the clustering algorithm is the GMM algorithm, which uses the comprehensive risk weight as the characteristic variable for clustering. The risk level is divided as follows: Figure 3 As shown, the specific method is as follows:
[0064] S41. The GMM algorithm is a distribution model based on probability density, which approximates the actual distribution of complex data through a linear combination of multiple Gaussian distributions. This model is suitable for dealing with situations where data distribution is uneven or has multimodal distribution, and can better simulate the characteristics of food safety risk level samples. GMM assumes that food risk level data comes from a combination of several Gaussian distributions, so that it can effectively capture the potential structure of the data. Each Gaussian distribution represents a latent category of food safety risk level, and the weight parameter reflects the probability of occurrence of each category. By maximizing the likelihood estimation, GMM can learn the implicit distribution of the data and accurately classify the samples into the corresponding risk levels. Mathematically, the probability density function of GMM can be expressed as:
[0065]
[0066] Among them, π j represents the weight of the j-th risk level, μ j and Σ j represent the mean vector and covariance matrix of the j-th risk level respectively.
[0067] S42. For the clustering results obtained by the GMM algorithm, the Bayesian information criterion, silhouette coefficient and Davis-Bodin index are used as three evaluation indicators. Based on the voting method, the quality of separation and homogeneity under different numbers of clusters reflected by the three indicators is comprehensively analyzed to calculate the optimal number of clusters, such as Figure 4 As shown. The Bayesian Information Criterion (BIC) is a criterion for model selection, which is based on the log-likelihood function of the model and combines the penalty term of model complexity. When the BIC value is small, it means that the model has achieved a good balance between data fitting and complexity. It is defined as:
[0068] BIC=-2ln(L)+pln(n) (4)
[0069] Where L represents the maximum likelihood estimate of the model, p is the number of parameters in the model, and n is the number of samples. The smaller the BIC value, the better the model.
[0070] S43. Silhouette is an indicator for evaluating clustering effect. It measures the compactness and separation of clusters by comparing the distance between the sample point and its class and other classes. Its value range is [-1,1]. The closer the silhouette coefficient is to 1, the better the clustering effect. The definition of silhouette coefficient is as follows:
[0071]
[0072] Among them, a(i) represents the average distance from sample i to other samples of the same class, and b(i) represents the average distance from sample i to the other closest class. When S(i) is close to 1, it means that the clustering effect of sample i is good.
[0073] S44, Davis-Bodin index is a function of the ratio of the sum of the intra-cluster distance and the average distance between any two clusters to the separation between different clusters. The smaller the index, the better the separation between clusters and the better the cohesion within the same cluster. The definition of this index is as follows:
[0074]
[0075] Wherein, formula (6) represents the average Euclidean distance C of k points in a cluster i Formula (7) calculates the cluster center and C i and Cj Formula (8) calculates the DBI of N clusters.
[0076] S45. After calculating the three cluster evaluation indicators, in order to improve the reliability of the evaluation method, a voting method is used, that is, based on the three cluster evaluation indicator values, the evaluation values of different numbers of cluster clusters are used to calculate the overall score according to the following scheme, where A is the largest number of cluster clusters (taking A=5 as an example): the number of clusters with the best evaluation indicator performance is given a score of A; the number of clusters with the second best performance is given a score of (A-1); the number of clusters with the third best performance is given a score of (A-2); the number of clusters with the fourth best performance is given a score of (A-3); the number of clusters with the worst performance is given a score of 1. Thereafter, the cluster evaluation indicator scores under different numbers of clusters are accumulated to determine the number of cluster clusters with the global maximum score, thereby selecting the optimal grading scheme.
[0077] Table 4 Coarse-grained classification of risk levels
[0078]
[0079] S5. Although the prototype network is very effective in dealing with small sample learning problems, it still faces two challenges: (1) The number of support samples is limited and may not be able to fully represent the food risk level under different conditions. The number of high-risk samples in the field of food safety is scarce and often has a class imbalance problem. Since support samples are usually randomly selected, it is difficult to fully cover all possible combinations of risk factors, such as different concentration levels of specific heavy metals, mycotoxins and other hazards. Therefore, effectively dealing with these hard samples is crucial for risk level classification. (2) The distribution difference between the support set and the query set is large, which affects the accuracy of the model. In food safety testing, data samples from different years or regions may have significant differences due to random sampling, resulting in inconsistent distributions of the support set and the query set. To ensure that the model can accurately assess different risk levels, the key is to construct an effective relationship to balance the difference between the support set and the query set, thereby improving the robustness of the model and the accuracy of risk prediction. The overall architecture of CPNet has two main components: a learnable prototype representation and a cosine attention mechanism for small sample tasks, such as Figure 2 shown.
[0080] S51. Learnable prototype representations, which compute classification prototype representations based on random support set features, which may be at the far end of the distribution or very close to each other. Given a support set S = (X S ,Y S ) of the query Q = (X Q ,Y Q ) follows the few-shot setting (n-way, k-shot, q query samples), where Y S ,Y Q∈n categories; X S ,X Q ∈R c×h×w ;X S and X Q First enter the feature extractor f(.|θ) to obtain the feature representation Z S ∈R n×h×w , Z Q ∈R q×d Then, in class C, based on all k learned features Calculate the average (arithmetic mean) to obtain the prototype representation Z P ∈R n×d , where z c ∈Z P represents the centroid of category C. However, due to the small number of randomly selected support samples, when the embedding representation of the selected sample is far from the edge of the classification distribution space and too close to the embedding representation of other category samples, the calculated prototype representation is inaccurate. To solve this problem, it is recommended to convert the arithmetic mean into a weighted average, where the weight can be adjusted through the learning process, so that a more reasonable prototype representation can be obtained. The formula for the learnable prototype representation is as follows:
[0081]
[0082] S52, Cosine Transformer, which determines the similarity matrix between the prototype representation and the query sample for few-shot classification tasks. The core of the transformer architecture is cosine attention, a cross-attention mechanism that uses cosine similarity and no softmax function to process two sets of different features. Given two vectors a, b∈R n , the cosine similarity between a and b is calculated by dividing the two dot product calculations by the product of their magnitudes, that is:
[0083]
[0084] According to the above vector formula, the definition of cosine similarity on the matrix is expanded. A and There are two matrices A and B respectively. Τ The cosine similarity of two matrices is described as:
[0085]
[0086] According to this definition, S C Each element S of (A,B) C (A,B) i,j is the i-th row vector a of matrix A i and the j-th column vector b of matrix Bj The cosine similarity score between , where i∈[1,n], j∈[1,m]. In general, the query sample x in class C among n classes q The probability prediction p(C|h q,c θ out ), represents the score h q,c The calculation formula for the predicted label y is as follows:
[0087]
[0088] S6. Model optimization and evaluation. The prototype network model based on the cosine attention mechanism is trained and optimized, and the classification effect of the model is evaluated by cross-validation and other methods. The classification accuracy, precision, recall rate and F1 score are calculated to ensure the robustness and reliability of the model in the case of high-risk sample scarcity. At the same time, several traditional machine learning methods, such as LSTM, ELM and BPNN, are used to compare the model effects with the prototype network based on the cosine attention mechanism proposed in the present invention, and 1-shot 5-way and 5-shot 5-way are compared, as shown in Tables 5 and 6. Figure 5 What is shown is the comparison of the model effects in the 5-shot 5-way case.
[0089] Table 5 Comparison of 1-shot 5-way model effects
[0090]
[0091] Table 6 Comparison of 5-shot 5-way model effects
[0092]
[0093] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the above embodiments, ordinary technicians in the relevant field should understand that the specific implementation methods of the present invention can still be modified or replaced by equivalents. Any modification or equivalent replacement that does not depart from the spirit and scope of the present invention should be covered within the scope of protection of the claims of the present invention.
Claims
1. A food safety risk assessment method based on small sample learning, characterized in that: include: Obtain a historical sampling data set of grain and oil food samples. Each grain and oil sample data in the food sampling data set contains the elements of each component hazard in each sample, and perform standard preprocessing on it to extract key risk factors in grain and oil food, including heavy metal pollutants (such as lead, cadmium, mercury, etc.) and fungal toxins (such as aflatoxin B1); The obtained risk factor indicators of food hazards are calculated using the step-wise weight assessment ratio analysis (SWARA) and CRITIC methods to calculate the subjective and objective weights, and the comprehensive weights of key risk factors are calculated by combining weights through game theory. In addition, the comprehensive weights are input into the Gaussian mixture clustering (GMM) algorithm to divide the risk level; Through the prototype network (Prototype Networks, ProtoNet) in the small sample learning model, a small number of known high-risk samples are used for feature extraction and sample feature vector learning. At the same time, the cosine attention mechanism is introduced to improve the prototype network to solve the problem of misclassification caused by the limited number of supported samples and the large difference in distribution between the query set and the query set. According to the classification results of the prototype network, the Euclidean distance between the sample to be classified and the prototype of each category is calculated, and the sample is classified into the nearest risk level category; Food samples are divided into multiple risk levels, including micro-risk, low risk, medium risk, high risk, and extremely high risk, to complete the food safety risk level assessment.
2. The food safety risk assessment method based on small sample learning according to claim 1 is characterized in that: The food safety sampling data preprocessing step includes: deleting useless information. Each sample has many descriptive characters. It is necessary to simplify this information to emphasize the characteristics that affect the degree of risk and eliminate food sensory indicators that have little to do with food safety, such as taste, color, shape, etc. 2) Delete redundant symbols. For example, the test result of a grain sample shows that the content of a certain ingredient is lower than the detection limit, and the limit value of the ingredient is stipulated not to exceed the maximum limit. Since the test result does not exceed the limit, remove the "<" symbol in the result and record the test value as the detection limit. Use "method detection limit" to replace the undetected indicator data. This is to ensure that the obtained input matrix can be recognized by the evaluation model and is positive definite. At this point, the screening of indicators and data preprocessing are completed.
3. The food safety risk assessment method based on small sample learning according to claim 1, characterized in that: The subjective weight of risk factors is calculated using the SWARA method. First, the importance of the selected key risk factors should be ranked. The relative importance of each risk factor is ranked in turn through expert scoring or other methods. The most important risk factor is given the highest priority, and the remaining factors are ranked in order of importance. Each risk factor is weighted according to its relative importance. The calculation of the weight can take into account the degree of influence of different factors and be adjusted in combination with expert opinions or historical data. Finally, the subjective weight of each factor is obtained.
4. The food safety risk assessment method based on small sample learning according to claim 1, characterized in that: The CRITIC objective weighting method is used to determine the weights of indicators. As an objective weight calculation method that reflects the correlation between indicators, the CRITIC method takes into account the variation and conflict between indicators when evaluating multiple indicators, that is, it uses standard deviation and correlation coefficient to calculate. Compared with the SWARA method, the CRITIC objective weighting method takes into account the correlation between indicators, making the objective weight more scientific.
5. The food safety risk assessment method based on small sample learning according to claim 1, characterized in that: Create a mathematical model based on game theory, use the weights calculated by the subjective weighting method and the objective weighting method as the strategy set of the two parties in the game, and use the equilibrium solution of game theory to optimize to achieve the optimal combination of weight distribution. This method comprehensively considers the relationship between contradiction and cooperation to obtain a more reasonable weight distribution result. Assume that there are N weighting methods, represented by ω1, ω2, …, ω n , by linearly combining these weight strategies, we get a comprehensive weight vector ω. By minimizing each strategy ω k The deviation between the comprehensive strategy ω and the optimal comprehensive weight is calculated, that is, The above formula is matrix processed to meet the optimized first-order derivative condition, and the coefficients (α1, α2, …, α n ), and finally get the optimal comprehensive weight ω * :
6. The food safety risk assessment method based on small sample learning according to claim 1, characterized in that: The clustering algorithm is the GMM algorithm, which is a distribution model based on probability density. It approximates the actual distribution of complex data through a linear combination of multiple Gaussian distributions. This model is suitable for dealing with situations where data distribution is uneven or has a multimodal distribution, and can better simulate the characteristics of food safety risk level samples. GMM assumes that food risk level data comes from a combination of several Gaussian distributions, so that it can effectively capture the potential structure of the data. Each Gaussian distribution represents a potential category of food safety risk level, and the weight parameter reflects the probability of occurrence of each category. By maximizing the likelihood estimate, GMM can learn the implicit distribution of the data and accurately classify the samples into the corresponding risk levels. Mathematically, the probability density function of GMM can be expressed as: Among them, π j represents the weight of the j-th risk level, μ j and∑ j Represent the mean vector and covariance matrix of the jth risk level respectively. For the clustering results obtained by the GMM algorithm, the three evaluation indicators of Bayesian information criterion, silhouette coefficient and Davis-Bodin index are used, and the quality of separation and homogeneity under different numbers of clusters reflected by the three indicators is comprehensively considered based on the voting method, so as to calculate the objective optimal number of clusters.
7. The food safety risk assessment method based on small sample learning according to claim 1, characterized in that: The prototype network (CPNet) based on the cosine attention mechanism is used to realize risk level prediction and classification. The prototype vector is generated by cosine similarity calculation and feature embedding, so that the classification accuracy can be improved even when the data is scarce and the difference is large. First, the method maps the data sample to the feature space and generates the prototype vector p of each category. k , calculate the Euclidean distance between the feature representation f(x) of the sample to be classified x and the prototype of each category: d(x,c k )=||f(x)-p k ||2 (4) Among them, f(x) is the feature representation of sample x, p k is the prototype of category k. In order to improve the stability of feature mapping between support set and query set samples, CPNet uses cosine similarity CosSim instead of traditional dot product similarity to ensure classification performance: Combined with the results of cosine similarity calculation, the cross entropy loss function is used to minimize the classification error, thereby enhancing the robustness of the model. Finally, the system predicts the risk level of the sample x to be classified by minimizing the distance between the sample x and the prototype of each category:
8. The food safety risk assessment method based on small sample learning according to claim 1, characterized in that: The method further includes evaluating the model results, verifying the model's accuracy, recall rate, F1-score and other indicators using real sampling data to ensure that the model has high predictive performance in small sample conditions; and conducting horizontal comparisons of food samples from different years and regions to verify the applicability and robustness of the model.
Citation Information
Cited By
Multi-parameter rapid screening food detection system
CN120494604A
Analysis method for constructing pesticide and veterinary drug residue risk assessment model in key edible agricultural products based on previous detection data set
CN121093058A