# Task-Aware Feature Composition for Few-Shot Relation Classification
## 4 Methodology
### Context Encoder  
This module is used to extract feature representations of query and support sentences. Given a support or query sentence mentioning two entities, we first apply an embedding layer to map words of them into embedding sequences, and then the MLMAN [12] is adopted as the context encoder to encode the embedding sequences into continuous low-dimensional sentence representations, aiming to capture the sentence semantics. **Embedding Layer. ** For a query or support sentence, we first map each word \(w_{t}\) in the sentence into a \(d_{w}\)-dimensional word embedding \(e_{t}\)[26; 27]. To incorporate the position information of the two mentioned entities in the sentence, the position embeddings [5], which indicate the relative distances between the current word and the two entities, are also adopted. Denote the position embeddings of \(w_{t}\) as \(p_{1t}\), \(p_{2t}\in\mathbb{R}^{p}\). By concatenating the three embeddings, we finally obtain the word embedding \(w_{t}=[e_{t};p_{1t};p_{2t}]\) of \(d_{w}+2d_{p}\) dimensions. Then, we gather all the input word embeddings in the sentence, and we have an embedding sequence ready for the encoding layers as follows:  
\[x=\{w_{1},w_{2},\ldots w_{T}\}=\{[e_{1};p_{11};p_{21}],\ldots,[e_{T};p_{1T};p_{ 2T}]\} \tag{5}\]  
where \(x\) represents the input embedding sequence for a query or a support sentence. \(T\) denotes the sentence length of the sentence. **Encoding Layer. ** In the encoding layer, we take the embedding sequence \(x\) as input and select MLMAN to encode \(x\) into the final instance representation. We first employ the  
Figure 2: Overall framework for task-aware few-shot relation classification. convolution neural network [28] to slide \(d_{c}\) number of filters with the window size \(m\) over the input embedding sequence and obtain a context representation sequence as follows:  
\[\bar{x}=\{\bar{w}_{1},\bar{w}_{2},\ldots\bar{w}_{T}\} \tag{6}\]  
\[\bar{w}_{t}=Conv(w_{t-\frac{m-1}{2}},\ldots,w_{t-\frac{m+1}{2}}),\ \bar{w}_{t}\in\mathbb{R}^{d_{c}} \tag{7}\]  
we then adopt a local matching and aggregation mechanism to obtain the matching information between query and support sentences. Let us use \(\bar{x}_{q}=\{\bar{w}_{1},\bar{w}_{2},\ldots\bar{w}_{T_{q}}\}\) and \(\bar{x}_{s}=\{\bar{w}_{1},\bar{w}_{2},\ldots\bar{w}_{T_{s}}\}\) to represent the context representation sequences for query and support sentences, respectively. \(T_{q}\) and \(T_{s}\) represent the sentence length of query and support sentences. Denote \(q_{i}\) and \(s_{j}\) as the context representation positions of query and support sentences, so we can compute the match score between them as follows. \[\alpha_{ij}=\bar{w}_{q_{i}}{}^{\top}\bar{w}_{s_{j}} \tag{8}\]  
Once obtaining the match scores between each query context representation and support context representation, we normalize them and use them as weights to aggregate context representations. In more detail, for \(\bar{w}_{q_{i}}\) of a query sentence, we first compute the match score between \(\bar{w}_{q_{i}}\) and every single context representation in the support sentence according to Equation (7). Then, we normalize these scores by employing a softmax function and use the normalized weights to aggregate the context representations of the support sentence. Finally, we can obtain the match representation of \(\bar{w}_{q_{i}}\) and the whole match representation sequence of query as follows:  
\[\bar{w}_{q_{i}}=\frac{\exp(\alpha_{ij})}{\sum_{j^{\prime}=1}^{T_{s}}\exp( \alpha_{ij^{\prime}})}w_{s_{j}} \tag{9}\]  
\[\bar{x}_{q}=\{\bar{w}_{1},\bar{w}_{2},\ldots\bar{w}_{T_{q}}\} \tag{10}\]  
where \(\bar{w}_{q_{i}}\) represents the match representation in the \(q_{i}\) position of query. The same operation also applies to the support sentence, and we can obtain the match representation sequence \(\bar{x}_{s}=\{\bar{w}_{1},\bar{w}_{2},\ldots\bar{w}_{T_{s}}\}\) of support sentence. Denote \(\hat{x}\) as the match representation sequence of query or support sentences.