# Bearing Fault Diagnosis Based on Small Sample Learning of Maml-Triplet
## 2 Maml-Triplet Learning Method
### Depth Metric Learning Method Based on Triplet Loss Function  
For the fault classification task with more categories and less data in the category, the depth metric learning method based on triple loss function can effectively solve this problem. First, the triplet neural network \(f_{2}(\theta)\) is constructed according to the trained Maml neural network \(f_{1}(\theta)\), and the triplet neural network weights \(\varphi\) are initialized using the trained Maml neural network weight \(\theta\). Then, the triplet is selected to build the input data, including anchor example \(A\), positive example \(P\), and negative example \(N\). The positive example is a signal of the same category as the anchor example, and the negative example is a signal of a different category from the anchor example. The triplet neural network is used to map the triplet signal into three eigenvectors, namely, anchor example eigenvector \(a\), positive example eigenvector \(p\), and negative example eigenvector \(n\). The triplet loss function is defined according to the Euclidean distance between the triplet eigenvectors, as shown in Formulas (8) and (9).  
\[\left\{\begin{array}{ll}d(a,p)=&||f_{2}(A)-f_{2}(P)||_{2}\\ d(a,\ n)=&||f_{2}(A)-f_{2}(N)||_{2}\end{array}\right. \tag{8}\]  
\[\mathcal{L}(\varphi,(A,P,\ N))=\max(d(a,p)-d(a,\ n)+\gamma,\ 0) \tag{9}\]  
where \(a=f_{2}(A)\), \(p=f_{2}(P)\), \(n=f_{2}(N)\). \(d(a,p)\) represents the euclidean distance between the anchor example feature vector and the positive example feature vector and \(d(a,\ n)\) represents the Euclidean distance between the anchor example feature vector and the negative example feature vector, and maximum spacing \(\gamma\).  
Then, the gradient descent method is used to update the weight \(\varphi\). As shown in Formula (10), \(\beta\) is the super parameter learning rate.  
\[\varphi\leftarrow\varphi-\beta\
abla_{\varphi}\mathcal{L}(\varphi,(A,P,\ N)) \tag{10}\]  
If all of the samples in the training set participate in the construction of triples, then the number of triples is very large and there are a large number of triples with their own loss function of 0. These triples will cause slow convergence of the model and the characteristics do not have uniqueness. Therefore, it is necessary to select the triad that is meaningful for training, that is, in each training iteration, the difficult triad is selected according to Formula (11).  
\[d(a,p)>d(a,\ n) \tag{11}\]  
Next, the process of diagnosing unknown fault signals after training the triplet neural network is introduced. The \(n\)-way \(k\)-shot is taken as an example. First, as shown in Formulas (12) and (13), a template library is constructed based on the signal data in the training set. That is, the triplet neural network is used to extract eigenvectors for all signals of the same fault type and the average value of all eigenvectors is taken. Secondly, the triplet neural network is used to extract the eigenvectors of unknown signals and the eigenvectors in the template library are used to measure the Euclidean distance. Finally, as shown in Formula (14), various Euclidean distances are compared to find the feature vector with the minimum distance from the unknown signal feature vector in the template library \(\mathcal{T}\), and the corresponding fault type \(o\) is the fault type of the unknown signal.  
\[x_{j}=\frac{1}{k}\sum_{i=1}^{k}f_{2}(X_{j}^{i}),1\leq j\leq n \tag{12}\]\[\mathcal{T}=(x_{1},x_{2},\ldots x_{N}) \tag{13}\]  
\[\mathrm{d}(x,x_{o})=\min\bigl{(}\mathrm{d}\bigl{(}x,x_{j}\bigr{)}\bigr{)},\ x_{j}\in \mathcal{T},\ 1\leq o\leq n \tag{14}\]  
where \(X\) represents the input signal data, \(x_{j}\) represents the eigenvector of the \(j\)-th fault type, \(x\) represents unknown data, and \(\mathcal{T}\) represents the template library.