Skip to content

Vector (Deprecated)

Kelvin Ng edited this page Feb 24, 2017 · 2 revisions

As per the discussion at issue #197, Vector is deprecated and we should use Eigen library instead. Some utilities, such as serialization and deserialization, are already added in lib/vector.hpp.

Vector is a class for representing mathematical vectors for machine learning.

Vector has two template parameters, namely T and is_sparse. T is for specifying the type of the elements inside Vector. is_sparse is for specifying whether the Vector is sparse or not. If the Vector is sparse, the zero elements will not be stored. Each element will be stored as a pair of (feature_id, value).

The APIs of sparse and dense vectors are mostly the same. In fact, the API of sparse vector is (nearly) the subset of dense vector. The philosophy is that, we should always 'one once, done for both dense and sparse vector'.

Arithmetic

Users can do arithmetic operation between vectors and scalars easily with Vector in the way like x = y + z, x += y, x = y.dot(z), x = 5.0 * y, etc. Note that some arithmetic operations are not available, like x += y with both of them being sparse vector. Also, x + y with both of them being sparse will give a dense vector. For the details, please refer to the header at lib/vector.hpp.

Set elements

x.set(feature_id, value) will do. Note that for sparse vector, it is indeed 'add'. So, setting the same feature_id twice will break the vector. Don't do that. For obeying the philosophy above, you should always set the value of a feature_id once, unless you know what you are doing.

Resize

x.resize(size) will do. The new elements will be initialized as zero.

Iterators

This is the most important feature allowing the philosophy of 'one once, done for both dense and sparse vector'.

1. ValueIterator and ConstValueIterator

It can be got by begin_value() and end_value(). The following program will print out all the elements if is_sparse == false or print out all the non-zero elements if is_sparse == true.

const bool is_sparse = true; // Can also be false! Everything else would be the same!
Vector<double, is_sparse> x(10);
// Set some values on x
for (auto it = x.begin_value(); it != x.end_value(); ++it) {
    printf("%f ", *it);
}
printf("\n");

2. FeaValIterator and ConstFeaValIterator

It can be got by begin_feaval() and end_feaval(). The following program will print out all the (feature_id, value) if is_sparse == false or print out all the (feature_id, value) with non-zero values if is_sparse == true.

const bool is_sparse = true; // Can also be false! Everything else would be the same!
Vector<double, is_sparse> x(10);
// Set some values on x
for (auto it = x.begin_feaval(); it != x.end_feaval(); ++it) {
    printf("(%d, %f) ", it->fea, it->val);
}
printf("\n");

3. Iterator and ConstIterator (Deprecated)

It can be got by begin() and end(). For dense vector, it is the same as ValueIteraotr. For sparse vector, it is the same as FeaValIterator. It is deprecated as it violates the 'one once, done for both dense and sparse vector' philosophy.