# Domain-Invariant Few-Shot Contrastive Learning for Hyperspectral Image Classification
## 3 Proposed Method
### MSAAN Model
#### 3.3.2 Multi-Scale Adaptive Attention Module  
The MSAA (Multi-Scale Adaptive Attention) module is designed to process the multi-scale information of the input, capturing various spatial features of the image through convolution layers at different scales. As shown in Figure 3, the output from each scale is weighted by an independent attention module, which emphasizes important information and suppresses unimportant information. Each convolution layer in the MSAA (Multi-Scale Adaptive Attention) module is followed by a batchnorm layer to promote training stability and enhance the model's expressive capacity. The Multi-Scale Adaptive Attention (MSAA) module is an innovative attention mechanism, aimed at capturing feature information across different scales. As shown in Figure 3, the structure and workflow of this module are as follows:  
Figure 3: The architecture of the proposed MSAA module. Figure 2: The architecture of MSAAN model. Initial Feature Extraction:  
Initial representations for query, key, and value are generated through \(1\times 1\) convolutions, setting up the base for further processing. Multi-Scale Feature Acquisition:  
The initial representations are fed into convolution operations at different scales to extract multi-scale features, allowing the model to capture a wide range of contextual information. Soft Key Attention (SKA) Computation:  
Soft Key Attention (SKA) is applied independently to features obtained from each scale to dynamically focus on relevant features. Feature Fusion:  
Features across different scales are integrated using an adaptive feature fusion module. This step is crucial for merging contextual information from various scales. Feature Normalization:  
The fused features are normalized via a \(1\times 1\) convolution layer, then pass through BatchNorm to stabilize the learning process of the model. Three MSAA modules are utilized, with multi-scale convolution kernel sizes \(K\) of 9, 7, and 5, and corresponding paddings of 4, 3, and 2. The design of SKA takes into account the requirements for computational efficiency and the handling of multi-scale features. The fundamental concept is to directly utilize the softmax of the key as the attention weights. The computation steps of SKA include:  
1. The QKV tensor is reconfigured and rearranged into the dimensions \((\text{B},N,H\times W,3d)\), where \(B\) is the size of the batch, \(N\) denotes the number of attention groups, \(H\) and \(W\) indicate the feature map's height and width, respectively, \(d\) is the dimension of each feature within an attention group, and \(N=\frac{C}{d}\) with \(C\), with C representing the total number of input channels. 2. QKV Split: Divide the reshaped tensor into three parts: Q, K, and V, each with dimensions \((\text{B},N,H\times W,d)\). 3. Attention Score Calculation: \[K_{soft}=softmax(K)\] (7) The specific calculation method of softmax is: \[K_{soft,ij}=\frac{\text{exp}(K_{ij})}{\sum_{l=1}^{H\times W}\text{exp}(K_{lj})}\] (8) The key-value product and the final attention output are calculated: \[KV=K_{soft}^{T}V,Output=Q(KV)\] (9) The complete soft key attention expression is: \[SKA(Q,K,V)=Q(softmax(K)^{T}V)\] (10) Different from the traditional calculation of the attention mechanism, the traditional calculation of attention is: \[Attention(Q,K,V)=softmax(\frac{QK^{T}}{\sqrt{d_{k}}})V\] (11) Traditional attention mechanisms usually require matrix multiplication between the query and the key, and then the application of the softmax function. Our SKA design aims to adapt to multi-scale feature scenarios. In our MSAAN, the Query, Key, and Value (Q, K, V) are obtained via linear layers and multi-scale convolution. This means they already contain rich multi-scale information. In this context, traditional attention computation might introduce unnecessary complexity. SKA maintains the dynamic allocation of attention weights by directly leveraging the information of the key, while simplifying the computational process.