# Bidirectional-Feature-Learning-Based Adversarial Domain Adaptation with Generative Network
## 3 BiFLP-AdvDA
### Loss Functions
The adversarial loss function, represented by Equations (4) and (5), was calculated by utilizing Equations (1) and (2) or Equation (3) as appropriate. First of all, Equation (1) is a task to classify the label of the source domain as best as possible, as mentioned earlier, and the corresponding loss function is learned to be maximized; while Equations (2) and (3) are the main tasks to learn to distinguish whether the input data are from the source domain or the target domain as much as possible. Corresponding loss functions are learned to be minimized. In this respect, it is similar to the existing minmax loss. Thus, it is called adversarial loss. In addition, the alpha value of GRL, borrowed from the model proposed for the domain adaptation task, which is the subject of this paper, is multiplied by the domain classification loss to adjust how much to use characteristics of the domain. Contrastive loss is a loss function to measure similarity between two features [30; 31], which is defined as the Euclidean distance between two features with the following formula:  
\[D_{st}(f(X_{\text{src}}),f(X_{\text{tar}}))=\sqrt{\sum_{i,j=1}^{n}((f(X_{\text {src}}^{i})-f(X_{\text{tar}}^{j}))^{2})} \tag{6}\]  
\[L_{\text{contrastive}}(y,D_{st})=y*D_{st}^{2}+(1-y)*max(m-D_{st},0)^{2} \tag{7}\]  
where \(f\) indicates the feature extraction function mapping the input data to feature embeddings; \(X_{\text{src}}\) and \(X_{\text{tar}}\) denotes the original source data \(X_{s}\) and the original target data \(X_{t}\) reconstructed by the pretrained autoencoder; \(D_{st}\) indicates the Euclidean distance, obtained by measuring the distance of features between the source data \(X_{\text{src}}\) and the target data \(X_{\text{tar}}\) using the feature embeddings generated by the function; f and n denote the size of one batch of the source and target domains and the number of data points within that batch; \(i\) and \(j\) denote the index variables of the source and target data; \(X_{\text{src}}^{i}\) denotes the ith data point of one batch of \(X_{\text{src}}\); \(X_{\text{tar}}^{j}\) denotes the jth data point of one batch of \(X_{\text{tar}}\); the means of D are different for each loss function. One of the D's, in Equations (2) and (3), indicatesthe domain discriminator and the other, in Equations (6) and (7), indicates the Euclidean distance; y denotes a binary label that takes the value 0 or 1, 1 if the two features are similar and 0 if they are different, for example, in traditional research, the loss function is computed in such a way that if a pair of data enters as input a pair of data of the same class, the value of y is 1, while if a pair of data enters as input a pair of data of a different class, the value of y is 0; and m denotes the margin value. This hyperparameter m is not learnable. However, it can be tuned to an appropriate value through experimentation. The total loss function is calculated by summing the loss functions of all components. After all, minimizing it is the goal of generalizing the model across domains and guiding it to extract similar features. Here is the formula for the total loss function. \[L_{\text{total}}=\cdot L_{\text{adv\_src}}+\cdot L_{\text{adv\_tar}}+\cdot L _{\text{contrastive}} \tag{8}\]  
In the learning process of the model proposed in this paper, the loss function is finally composed of adversarial loss and contrastive loss, which are arbitrarily configured for the model as in Equation (8). By learning the loss function for the source domain and the target domain adversarially, the feature extractor extracts domain-invariant features that work well in both domains, i.e., features with similar characteristics to both domains. The contrastive loss is calculated according to the Euclidean distance between the two extracted features so that the similarity of the data points in the vector space increases. Eventually, the boundary between the two domains is blurred.