Suppose that I have the following vectors $v_1$, $v_2$, $v_3$ $\in R^n$,$n= 9$. What I want to do exactly is to combine these $3$ vectors into $1$ representative vector $V$.
According to the following article: Document Classification and Page Stream Segmentation for Digital Mailroom Applications
- they Perform at first average pooling or just the mean of the $3$ vectors to obtain a single representation of the $3$ vectors
- The new vector is then L2-Normalized
This is how I interpreted these steps by using the following example:
$v_1 = \ [ x_1,x_2,...,x_n \ ]$
$v_2 = \ [ y_1,y_2,...,y_n \ ]$
$v_3 = \ [ z_1,z_2,...,z_n \ ]$
Step 1: Average Pooling
$V = \frac{v_1 + v_2 + v_3}{3}$
$V = \ [ w_1,w_2,...,w_n \ ]$
$V$ is the vector after the average pooling or mean
Step 2: L2-Norm
$Norm = \sqrt{w_1^2+\ldots+w_n^2}$
Step 3 : divide the components $w_i$ of $V$ by the $Norm$
Are these steps correct?
Code Sample :
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
const int size = 5;
vector<float> v1{1,1,1,1,1};
vector<float> v2{1,1,1,1,1};
vector<float> v3{1,1,1,1,1};
vector<vector<float>> V;
V.push_back(v1);
V.push_back(v2);
V.push_back(v3);
vector<float> doc(size,0.0);
//============================================//
//STEP 1: Average Pooling
for(size_t i = 0; i < V.size();i++)
{
for(size_t j = 0; j < 5;j++)
{
doc[j] += V[i][j];
}
}
for(size_t i = 0; i < doc.size();i++)
{
doc[i]= doc[i]/(float) V.size();
}
//============================================//
//STEP 2: L2-Normalization
float y = *max_element(std::begin(doc), std::end(doc));
float m_sum = 0.0;
for (int k = 0; k < doc.size(); k++)
{
m_sum += pow(doc[k]/y,2);
}
//STEP 3: Divide components by the Norm (m_sum)
for(size_t i = 0; i < V.size();i++)
{
cout << doc[i]/sqrt(m_sum)<<" ";
}
cout << endl;
return 0;
}