# Transfer Convolutional Neural Network for Cross-Project Defect Prediction
## 3 Methodology
### TCNN Building and DL-Generated Feature Extracting  
Relying on CNNs' powerful feature generation capabilities, we hope to use it to capture the semantics and local structure of the source code [8]. In this step, we extended the standard CNN architecture to the TCNN with a matching layer to extract transferable DL-generated features for CPDP. In particular, our TCNN consists of an embedded layer, a convolutional layer, a maximum pooling layer, a fully-connected layer, a matching layer, and an output layer with a single unit that ultimately acts as a classifier. The architecture of the TCNN is shown in Figure 3. In addition to using the sigmoid as the activation function of the output layer, all other layers adopted the ReLU as the activation function. We implemented our model by using _Pytorch_ ([[https://pytorch.org](https://pytorch.org)]([https://pytorch.org](https://pytorch.org))), which is an open-source DL platform that provides efficient tools for neural networks' construction and flexible development. In short, our TCNN model is a variant of the traditional CNN. The key difference between them is that the former adds a matching process after the fully-connected layer to measure the divergence between source and target projects, and the divergence is added to the loss calculation when the network is training. In other words, when CNN is training through labeled integer vectors from the source project, we simultaneously require that the distribution of the source and target projects becomes similar under the hidden representation of the fully-connected layer [12]. Specifically, we aim to optimize two complementary objective functions as follows: (1) minimizing the classification error \(J_{C}\) on the source labeled data and (2) minimizing the distribution divergence \(J_{D}\) between the source and target projects. The final optimization goal is represented as the following formula:  
\[\min J_{C}+\lambda J_{D} \tag{1}\]  
where \(\lambda\) in the formula is a positive regularization parameter. Regarding classification errors \(J_{C}\), letting \(\Theta=\{W,b\}\) denote the set of all CNN parameters, the empirical risk of CNN is:  
\[J_{C}=\frac{1}{m}\sum_{i=1}^{m}J(\Theta(X_{i},y_{i})) \tag{2}\]  
where \(J(\cdot)\) is the cross-entropy loss function, \(\Theta(X_{i})\) is the conditional probability that the CNN assigns \(X_{i}\) to label \(y_{i}\), and \(m\) represents the number of source project instances. Regarding the measurement of distribution divergence \(J_{D}\), we adopted maximum mean discrepancy (MMD) [23; 24], which could compare different distributions based on the distance between sample means of two datasets in an RKHS. Given \(P_{s}\) and \(P_{t}\) as the marginal distribution of source and target projects, \(m\) and \(n\) as the number of source and target project instances, let \begin{table}
\begin{tabular}{l l} \hline
**Node Category** & **Node Type** \\ \hline Nodes of method invocations and & MethodInvocation, SuperMethodInvocation, ClassCreator \\ instance creations & \\ \hline Declaration-related nodes & PackageDeclaration, InterfaceDeclaration, ClassDeclaration, \\  & ConstructorDeclaration, MethodDeclaration, VariableDeclarator, \\  & FormalParameter \\ \hline Control-flow-related nodes & IfStatement, ForStatement, WhileStatement, DoStatement, \\  & AsserStatement, BreakStatement, ContinueStatement, \\  & ReturnStatement, ThrowStatement, TryStatement, \\  & SynchronizedStatement, SwitchStatement, BlockStatement, \\  & CatchClauseParameter, TryResource, CatchClause, \\  & SwitchStatementCase, ForControl, EnhancedForControl \\ \hline Other nodes & BasicType, MemberReference, ReferenceType, \\  & SuperMemberReference, StatementExpression, \\ \hline \end{tabular}
\end{table} 
Table 1: The types of AST nodes we used. \(\mathbf{x_{s}}=\{x_{1},\ldots,x_{m}\}\in\mathbb{R}^{m\to d}\) denote the instances of the source project and \(\mathbf{x_{t}}=\{x_{m+1},\ldots,x_{m+n}\}\in\mathbb{R}^{n\to d}\) denote the instances of target projects.