## 3 Methodology
### Two-Step Training
#### 3.3.2 Training Step #2  
Benefitting from our DAT encoder, training step #1 alone can give a satisfactory classification result, but it would suffer from two severe drawbacks: (1) The speed of the network can be encumbered seriously owing to the tremendous comparison times. For instance, given a new sample, \(M\) categories, it takes \(MN\) times comparison to give the final label, according to Algorithm 1. (2) The number of samples selected from each category during testing time is hard to trade off. Fewer samples may cause noise because the randomly selected samples cannot represent the whole set, while more samples could lead to inference speed degradation, as stated in the drawback.  
Inspired by Prototypical Network [41], we propose training step #2 to solve the issues. Instead of using several random samples, we utilize a Normal Vector to denote the characteristic of each category, which is more time-saving and robust. During training step 2, one of the feature extractors is replaced with a Normal Vector layer. Note that all parameters of the network except Normal Vector layer are frozen since what we want from training step #2 is a Normal Vector.  
Training step #2 can be illustrated using Algorithm 2. Given that category \(i\) needs to generate the Normal Vector \(V_{i}\) and the trained model, the algorithm has to initialize the training dataset for category \(i\). Specifically, for each sample in training set, if it belongs to category \(i\), we label it with 1, while 0 is assigned to the sample if it is not in category \(i\). When training is executed, every sample \(x\) in training set is sent to the feature extractor, i.e., the dual attention Transformer, to obtain the embedded feature vector \(V_{x}\). After that, subtraction and multiplication are applied to \(V_{x}\) and the Normal Vector \(V_{i}\). As the same in training step #1, the outputs of subtraction and multiplication, \(V_{x}\) and \(V_{i}\), are concatenated and are passed through two linear layers and a softmax activation function, obtaining the final prediction. The Normal Vector is then trained to minimize the gap between the prediction and the label. Once the training is finished, the weight in the Normal Vector layer is drawn out to serve as the Normal Vector of the category.  
**Input:** The malware category \(i\) which needs to generate the Normal Vector.  
**Output:** The generated Normal Vector \(V_{i}\).  
**Init:**\(trainSet\): the training set in this work, for training sample \(x\), \(x.x\) denotes its API call sequence, and \(x.y\) is the corresponding label, _model_: The trained model whose parameters are fixed except for NormalVector layer.  
function generateNormalVector(\(i\)):  
\(input\leftarrow[]\)  
\(label\leftarrow[]\)  
for \(x\) in \(trainSet\) do:  
\(input.append(x.x)\)  
If \(x.y=i\), then  
\(label.append(1)\)  
else  
\(label.append(0)\)  
end if  
end for  
\(model.fit(input,\ output)\)  
return \(model.getLayer('NormalVector').getWeight()\)  
end function  
**Algorithm 2** Normal Vector optimization  
During inference time, the API call sequence is sent to the feature extractor, and the network will predict similarities between the extracted feature and all Normal Vectors. If the highest probability is produced by the Normal Vector \(V_{i}\), the new sample will be classified into category \(i\). This procedure can be formulated using Algorithm 3.  
```
Input: The API call sequence \(x\) of the malware needed to be classified. Output: The category of \(x\). Init:\(classes\): All the classes of malware, \(model\): The trained model function getNormalVector(\(m\)): if \(m\) not exist in NormalVectorSet then NormalVectorSet[\(m\)] \(\leftarrow\) generatorNormalVector(\(m\)) return NormalVectorSet[\(m\)] end function function predict (\(x\)): \(probability\leftarrow[]\) for \(i\) in \(classes\) do: \(probability[i]\gets model\_predict(x\), getNormalVector(\(i\))) end for return \(argmax(probability)\) end function
```  
**Algorithm 3** Feature extraction and classification