SHOGUN
4.1.0
|
class that implements the Random Kitchen Sinks (RKS) for the DotFeatures as mentioned in http://books.nips.cc/papers/files/nips21/NIPS2008_0885.pdf.
RKS input:
RKS output: A function \(\hat{f}(x) = \sum_{k=1}^{K} \phi(x; w_k)\alpha_k\)
\[ \underset{\alpha \in \mathbf{R}^K}{\text{minimize}} \quad \frac{1}{m}\sum_{i=1}^{m} c(\alpha^{\top} z_i, y_i) \]
\[ \text{s.t.} \quad \|\alpha\|_{\infty} \leq C/K. \]
for vector \(\alpha\), either through least squares when \(c(y', y)\) is the quadratic loss or through a linear SVM when \(c(y', y)\) is the hinge loss.This class implements the vector transformation on-the-fly whenever it is needed. In order for it to work, the class expects the user to implement a subclass of CRKSFunctions and implement in there the functions \(\phi\) and \(p\) and then pass an instantiated object of that class to the constructor. For example, in the derived class CRandomFourierDotFeatures, random fourier features are implemented as \( z(x) = \sqrt{2/K}\cos(w^{\top}x + b)\), where \(w\) drawn from a Gaussian distribution and \(b\) from a uniform distribution.
Further useful resources, include : http://www.shloosl.com/~ali/random-features/ https://research.microsoft.com/apps/video/dl.aspx?id=103390&l=i
Definition at line 50 of file RandomKitchenSinksDotFeatures.h.
Static Public Member Functions | |
static void * | dense_dot_range_helper (void *p) |
static SGVector< float64_t > | get_mean (CDotFeatures *lhs, CDotFeatures *rhs) |
static SGMatrix< float64_t > | compute_cov (CDotFeatures *lhs, CDotFeatures *rhs) |
Public Attributes | |
SGIO * | io |
Parallel * | parallel |
Version * | version |
Parameter * | m_parameters |
Parameter * | m_model_selection_parameters |
Parameter * | m_gradient_parameters |
uint32_t | m_hash |
Protected Member Functions | |
virtual float64_t | dot (index_t vec_idx, index_t par_idx) |
virtual float64_t | post_dot (float64_t dot_result, index_t par_idx) |
virtual SGVector< float64_t > | generate_random_parameter_vector ()=0 |
void | display_progress (int32_t start, int32_t stop, int32_t v) |
virtual void | load_serializable_pre () throw (ShogunException) |
virtual void | load_serializable_post () throw (ShogunException) |
virtual void | save_serializable_pre () throw (ShogunException) |
virtual void | save_serializable_post () throw (ShogunException) |
Protected Attributes | |
CDotFeatures * | feats |
int32_t | num_samples |
SGMatrix< float64_t > | random_coeff |
float64_t | combined_weight |
feature weighting in combined dot features More... | |
CSubsetStack * | m_subset_stack |
default constructor
Definition at line 20 of file RandomKitchenSinksDotFeatures.cpp.
CRandomKitchenSinksDotFeatures | ( | CDotFeatures * | dataset, |
int32_t | K | ||
) |
constructor Subclasses should call generate_random_coefficients() on their own if they choose to use this constructor.
dataset | the dataset to work on |
K | the number of samples to draw |
Definition at line 26 of file RandomKitchenSinksDotFeatures.cpp.
CRandomKitchenSinksDotFeatures | ( | CDotFeatures * | dataset, |
int32_t | K, | ||
SGMatrix< float64_t > | coeff | ||
) |
constructor
dataset | the dataset to work on |
K | the number of samples to draw |
coeff | the random coefficients to use |
Definition at line 32 of file RandomKitchenSinksDotFeatures.cpp.
CRandomKitchenSinksDotFeatures | ( | CFile * | loader | ) |
constructor loading features from file
loader | File object via which to load data |
Definition at line 55 of file RandomKitchenSinksDotFeatures.cpp.
CRandomKitchenSinksDotFeatures | ( | const CRandomKitchenSinksDotFeatures & | orig | ) |
copy constructor
Definition at line 60 of file RandomKitchenSinksDotFeatures.cpp.
|
virtual |
destructor
Definition at line 67 of file RandomKitchenSinksDotFeatures.cpp.
|
virtualinherited |
Adds a subset of indices on top of the current subsets (possibly subset of subset). Every call causes a new active index vector to be stored. Added subsets can be removed one-by-one. If this is not needed, add_subset_in_place() should be used (does not store intermediate index vectors)
Calls subset_changed_post() afterwards
subset | subset of indices to add |
Reimplemented in CCombinedFeatures.
Definition at line 310 of file Features.cpp.
Sets/changes latest added subset. This allows to add multiple subsets with in-place memory requirements. They cannot be removed one-by-one afterwards, only the latest active can. If this is needed, use add_subset(). If no subset is active, this just adds.
Calls subset_changed_post() afterwards
subset | subset of indices to replace the latest one with. |
Definition at line 316 of file Features.cpp.
|
virtual |
add vector 1 multiplied with alpha to dense vector2
possible with subset
alpha | scalar alpha |
vec_idx1 | index of first vector |
vec2 | pointer to real valued vector |
vec2_len | length of real valued vector |
abs_val | if true add the absolute value |
Implements CDotFeatures.
Definition at line 127 of file RandomKitchenSinksDotFeatures.cpp.
|
inherited |
run benchmark for add_to_dense_vec
Definition at line 318 of file DotFeatures.cpp.
|
inherited |
run benchmark for dense_dot_range
Definition at line 341 of file DotFeatures.cpp.
|
inherited |
Builds a dictionary of all parameters in SGObject as well of those of SGObjects that are parameters of this object. Dictionary maps parameters to the objects that own them.
dict | dictionary of parameters to be built. |
Definition at line 597 of file SGObject.cpp.
|
inherited |
check feature compatibility
f | features to check for compatibility |
Definition at line 283 of file Features.cpp.
|
inherited |
clears all preprocs
Definition at line 116 of file Features.cpp.
|
virtualinherited |
Creates a clone of the current object. This is done via recursively traversing all parameters, which corresponds to a deep copy. Calling equals on the cloned object always returns true although none of the memory of both objects overlaps.
Definition at line 714 of file SGObject.cpp.
|
staticinherited |
compute the covariance of two CDotFeatures together
Definition at line 469 of file DotFeatures.cpp.
Creates a new CFeatures instance containing only the dimensions of the feature vector which are specified by the provided indices.
This method is needed for feature selection tasks NOT IMPLEMENTED!
dims | indices of feature dimensions to copy |
Reimplemented in CDenseFeatures< ST >, CDenseFeatures< uint32_t >, CDenseFeatures< float64_t >, CDenseFeatures< T >, and CDenseFeatures< uint16_t >.
Definition at line 348 of file Features.cpp.
Creates a new CFeatures instance containing copies of the elements which are specified by the provided indices.
This method is needed for a KernelMachine to store its model data. NOT IMPLEMENTED!
indices | indices of feature elements to copy |
Reimplemented in CStringFeatures< ST >, CStringFeatures< T >, CStringFeatures< uint8_t >, CStringFeatures< char >, CStringFeatures< uint16_t >, CSparseFeatures< ST >, CSparseFeatures< float64_t >, CSparseFeatures< T >, CDenseFeatures< ST >, CDenseFeatures< uint32_t >, CDenseFeatures< float64_t >, CDenseFeatures< T >, CDenseFeatures< uint16_t >, and CCombinedFeatures.
Definition at line 340 of file Features.cpp.
Takes a list of feature instances and returns a new instance being a concatenation of a copy of this instace's data and the given instancess data. Note that the feature types have to be equal.
NOT IMPLEMENTED!
others | list of feature objects to append |
Reimplemented in CDenseFeatures< ST >, CDenseFeatures< uint32_t >, CDenseFeatures< float64_t >, CDenseFeatures< T >, and CDenseFeatures< uint16_t >.
Definition at line 235 of file Features.h.
Convenience method for method with same name and list as parameter.
NOT IMPLEMENTED!
other | feature object to append |
Reimplemented in CDenseFeatures< ST >, CDenseFeatures< uint32_t >, CDenseFeatures< float64_t >, CDenseFeatures< T >, CDenseFeatures< uint16_t >, and CCombinedFeatures.
Definition at line 249 of file Features.h.
|
virtualinherited |
A deep copy. All the instance variables will also be copied.
Definition at line 198 of file SGObject.cpp.
|
virtualinherited |
delete preprocessor from list
num | index of preprocessor in list |
Definition at line 122 of file Features.cpp.
compute dot product between vector1 and a dense vector
possible with subset
vec_idx1 | index of first vector |
vec2 | pointer to real valued vector |
vec2_len | length of real valued vector |
Implements CDotFeatures.
Definition at line 110 of file RandomKitchenSinksDotFeatures.cpp.
|
virtualinherited |
Compute the dot product for a range of vectors. This function makes use of dense_dot alphas[i] * sparse[i]^T * w + b
output | result for the given vector range |
start | start vector range from this idx |
stop | stop vector range at this idx |
alphas | scalars to multiply with, may be NULL |
vec | dense vector to compute dot product with |
dim | length of the dense vector |
b | bias |
note that the result will be written to output[0...(stop-start-1)]
Reimplemented in CCombinedDotFeatures, and CHashedWDFeaturesTransposed.
Definition at line 67 of file DotFeatures.cpp.
|
staticinherited |
Compute the dot product for a range of vectors. This function is called by the threads created in dense_dot_range
Definition at line 231 of file DotFeatures.cpp.
|
virtualinherited |
Compute the dot product for a subset of vectors. This function makes use of dense_dot alphas[i] * sparse[i]^T * w + b
sub_index | index for which to compute outputs |
num | length of index |
output | result for the given vector range |
alphas | scalars to multiply with, may be NULL |
vec | dense vector to compute dot product with |
dim | length of the dense vector |
b | bias |
Reimplemented in CCombinedDotFeatures, and CHashedWDFeaturesTransposed.
Definition at line 153 of file DotFeatures.cpp.
compute dot product between vector1 and a dense vector
vec_idx1 | index of first vector |
vec2 | dense vector |
Reimplemented in CHashedDocDotFeatures.
Definition at line 62 of file DotFeatures.cpp.
|
protectedinherited |
display progress output
start | minimum value |
stop | maximum value |
v | current value |
Definition at line 528 of file DotFeatures.cpp.
|
virtual |
compute dot product between vector1 and vector2, appointed by their indices
possible with subset
vec_idx1 | index of first vector |
df | DotFeatures (of same kind) to compute dot product with |
vec_idx2 | index of second vector |
Implements CDotFeatures.
Definition at line 90 of file RandomKitchenSinksDotFeatures.cpp.
Method used before computing the dot product between a feature vector and a parameter vector
vec_idx | the feature vector index |
par_idx | the parameter vector index |
Definition at line 199 of file RandomKitchenSinksDotFeatures.cpp.
|
virtual |
duplicate
Implements CFeatures.
Reimplemented in CRandomFourierDotFeatures.
Definition at line 188 of file RandomKitchenSinksDotFeatures.cpp.
Recursively compares the current SGObject to another one. Compares all registered numerical parameters, recursion upon complex (SGObject) parameters. Does not compare pointers!
May be overwritten but please do with care! Should not be necessary in most cases.
other | object to compare with |
accuracy | accuracy to use for comparison (optional) |
tolerant | allows linient check on float equality (within accuracy) |
Definition at line 618 of file SGObject.cpp.
|
virtual |
clean up iterator call this function with the iterator returned by get_first_feature
iterator | as returned by get_first_feature |
Implements CDotFeatures.
Definition at line 163 of file RandomKitchenSinksDotFeatures.cpp.
generate the random coefficients and return them in a matrix where each column is a parameter vector
Definition at line 39 of file RandomKitchenSinksDotFeatures.cpp.
Generates a random parameter vector, subclasses must override this
Implemented in CRandomFourierDotFeatures.
|
inherited |
|
inherited |
get combined feature weight
Definition at line 154 of file DotFeatures.h.
compute the feature matrix in feature space
Definition at line 284 of file DotFeatures.cpp.
compute the feature vector in feature space
Definition at line 305 of file DotFeatures.cpp.
|
virtual |
obtain the dimensionality of the feature space
(not mix this up with the dimensionality of the input space, usually obtained via get_num_features())
Implements CDotFeatures.
Definition at line 85 of file RandomKitchenSinksDotFeatures.cpp.
|
virtual |
get feature class
Implements CFeatures.
Definition at line 173 of file RandomKitchenSinksDotFeatures.cpp.
|
virtualinherited |
Given a class in right hand side, does this class support compatible computation?
for example, is this->dot(rhs_prt) valid, where rhs_prt is the class in right hand side
rhs | the class in right hand side |
Reimplemented in CDenseSubSamplesFeatures< ST >.
Definition at line 355 of file Features.cpp.
|
virtual |
iterate over the non-zero features
call get_feature_iterator first, followed by get_next_feature and free_feature_iterator to cleanup
possible with subset
vector_index | the index of the vector over whose components to iterate over |
Implements CDotFeatures.
Definition at line 150 of file RandomKitchenSinksDotFeatures.cpp.
|
virtual |
get feature type
Implements CFeatures.
Definition at line 168 of file RandomKitchenSinksDotFeatures.cpp.
|
inherited |
|
inherited |
|
inherited |
|
staticinherited |
get mean of two CDotFeature objects
Definition at line 402 of file DotFeatures.cpp.
|
inherited |
Definition at line 498 of file SGObject.cpp.
|
inherited |
Returns description of a given parameter string, if it exists. SG_ERROR otherwise
param_name | name of the parameter |
Definition at line 522 of file SGObject.cpp.
|
inherited |
Returns index of model selection parameter with provided index
param_name | name of model selection parameter |
Definition at line 535 of file SGObject.cpp.
|
virtual |
Implements CSGObject.
Definition at line 183 of file RandomKitchenSinksDotFeatures.cpp.
|
virtual |
iterate over the non-zero features
call this function with the iterator returned by get_first_feature and call free_feature_iterator to cleanup
possible with subset
index | is returned by reference (-1 when not available) |
value | is returned by reference |
iterator | as returned by get_first_feature |
Implements CDotFeatures.
Definition at line 156 of file RandomKitchenSinksDotFeatures.cpp.
|
virtual |
get number of non-zero features in vector
num | which vector |
Implements CDotFeatures.
Definition at line 145 of file RandomKitchenSinksDotFeatures.cpp.
|
inherited |
get the number of applied preprocs
Definition at line 103 of file Features.cpp.
|
inherited |
get number of preprocessors
Definition at line 155 of file Features.cpp.
|
virtual |
get number of feature vectors
Implements CFeatures.
Definition at line 178 of file RandomKitchenSinksDotFeatures.cpp.
|
inherited |
get specified preprocessor
num | index of preprocessor in list |
Definition at line 93 of file Features.cpp.
returns the random function parameters that were generated through the function p
Definition at line 194 of file RandomKitchenSinksDotFeatures.cpp.
|
virtualinherited |
|
inherited |
check if features have given property
p | feature property |
Definition at line 295 of file Features.cpp.
|
virtualinherited |
If the SGSerializable is a class template then TRUE will be returned and GENERIC is set to the type of the generic.
generic | set to the type of the generic if returning TRUE |
Definition at line 296 of file SGObject.cpp.
|
inherited |
get whether specified preprocessor was already applied
num | index of preprocessor in list |
Definition at line 149 of file Features.cpp.
|
inherited |
list feature object
Definition at line 171 of file Features.cpp.
|
inherited |
print preprocessors
Definition at line 131 of file Features.cpp.
|
virtualinherited |
load features from file
loader | File object via which data shall be loaded |
Reimplemented in CDenseFeatures< ST >, CDenseFeatures< uint32_t >, CDenseFeatures< float64_t >, CDenseFeatures< T >, CDenseFeatures< uint16_t >, CSparseFeatures< ST >, CSparseFeatures< float64_t >, CSparseFeatures< T >, CStringFeatures< ST >, CStringFeatures< T >, CStringFeatures< uint8_t >, CStringFeatures< char >, and CStringFeatures< uint16_t >.
Definition at line 269 of file Features.cpp.
|
virtualinherited |
Load this object from file. If it will fail (returning FALSE) then this object will contain inconsistent data and should not be used!
file | where to load from |
prefix | prefix for members |
Definition at line 369 of file SGObject.cpp.
|
protectedvirtualinherited |
Can (optionally) be overridden to post-initialize some member variables which are not PARAMETER::ADD'ed. Make sure that at first the overridden method BASE_CLASS::LOAD_SERIALIZABLE_POST is called.
ShogunException | will be thrown if an error occurs. |
Reimplemented in CKernel, CWeightedDegreePositionStringKernel, CList, CAlphabet, CLinearHMM, CGaussianKernel, CInverseMultiQuadricKernel, CCircularKernel, and CExponentialKernel.
Definition at line 426 of file SGObject.cpp.
|
protectedvirtualinherited |
Can (optionally) be overridden to pre-initialize some member variables which are not PARAMETER::ADD'ed. Make sure that at first the overridden method BASE_CLASS::LOAD_SERIALIZABLE_PRE is called.
ShogunException | will be thrown if an error occurs. |
Reimplemented in CDynamicArray< T >, CDynamicArray< float64_t >, CDynamicArray< float32_t >, CDynamicArray< int32_t >, CDynamicArray< char >, CDynamicArray< bool >, and CDynamicObjectArray.
Definition at line 421 of file SGObject.cpp.
|
virtualinherited |
Definition at line 262 of file SGObject.cpp.
subclass must override this to perform any operations on the dot result between a feature vector and a parameter vector w
dot_result | the result of the dot operation |
par_idx | the idx of the parameter vector |
Reimplemented in CRandomFourierDotFeatures.
Definition at line 205 of file RandomKitchenSinksDotFeatures.cpp.
|
inherited |
prints all parameter registered for model selection and their type
Definition at line 474 of file SGObject.cpp.
|
virtualinherited |
prints registered parameters out
prefix | prefix for members |
Definition at line 308 of file SGObject.cpp.
|
virtualinherited |
removes all subsets Calls subset_changed_post() afterwards
Reimplemented in CCombinedFeatures.
Definition at line 328 of file Features.cpp.
|
virtualinherited |
removes that last added subset from subset stack, if existing Calls subset_changed_post() afterwards
Reimplemented in CCombinedFeatures.
Definition at line 322 of file Features.cpp.
|
virtualinherited |
in case there is a feature matrix allow for reshaping
NOT IMPLEMENTED!
num_features | new number of features |
num_vectors | new number of vectors |
Reimplemented in CDenseFeatures< ST >, CDenseFeatures< uint32_t >, CDenseFeatures< float64_t >, CDenseFeatures< T >, and CDenseFeatures< uint16_t >.
Definition at line 165 of file Features.cpp.
|
virtualinherited |
save features to file
writer | File object via which data shall be saved |
Reimplemented in CStringFeatures< ST >, CStringFeatures< T >, CStringFeatures< uint8_t >, CStringFeatures< char >, CStringFeatures< uint16_t >, CDenseFeatures< ST >, CDenseFeatures< uint32_t >, CDenseFeatures< float64_t >, CDenseFeatures< T >, CDenseFeatures< uint16_t >, CSparseFeatures< ST >, CSparseFeatures< float64_t >, and CSparseFeatures< T >.
Definition at line 276 of file Features.cpp.
|
virtualinherited |
Save this object to file.
file | where to save the object; will be closed during returning if PREFIX is an empty string. |
prefix | prefix for members |
Definition at line 314 of file SGObject.cpp.
|
protectedvirtualinherited |
Can (optionally) be overridden to post-initialize some member variables which are not PARAMETER::ADD'ed. Make sure that at first the overridden method BASE_CLASS::SAVE_SERIALIZABLE_POST is called.
ShogunException | will be thrown if an error occurs. |
Reimplemented in CKernel.
Definition at line 436 of file SGObject.cpp.
|
protectedvirtualinherited |
Can (optionally) be overridden to pre-initialize some member variables which are not PARAMETER::ADD'ed. Make sure that at first the overridden method BASE_CLASS::SAVE_SERIALIZABLE_PRE is called.
ShogunException | will be thrown if an error occurs. |
Reimplemented in CKernel, CDynamicArray< T >, CDynamicArray< float64_t >, CDynamicArray< float32_t >, CDynamicArray< int32_t >, CDynamicArray< char >, CDynamicArray< bool >, and CDynamicObjectArray.
Definition at line 431 of file SGObject.cpp.
|
inherited |
set combined kernel weight
nw | new combined feature weight |
Definition at line 160 of file DotFeatures.h.
|
inherited |
Definition at line 41 of file SGObject.cpp.
|
inherited |
Definition at line 46 of file SGObject.cpp.
|
inherited |
Definition at line 51 of file SGObject.cpp.
|
inherited |
Definition at line 56 of file SGObject.cpp.
|
inherited |
Definition at line 61 of file SGObject.cpp.
|
inherited |
Definition at line 66 of file SGObject.cpp.
|
inherited |
Definition at line 71 of file SGObject.cpp.
|
inherited |
Definition at line 76 of file SGObject.cpp.
|
inherited |
Definition at line 81 of file SGObject.cpp.
|
inherited |
Definition at line 86 of file SGObject.cpp.
|
inherited |
Definition at line 91 of file SGObject.cpp.
|
inherited |
Definition at line 96 of file SGObject.cpp.
|
inherited |
Definition at line 101 of file SGObject.cpp.
|
inherited |
Definition at line 106 of file SGObject.cpp.
|
inherited |
Definition at line 111 of file SGObject.cpp.
|
inherited |
set generic type to T
|
inherited |
|
inherited |
set the parallel object
parallel | parallel object to use |
Definition at line 241 of file SGObject.cpp.
|
inherited |
set the version object
version | version object to use |
Definition at line 283 of file SGObject.cpp.
|
inherited |
set applied flag for preprocessor
num | index of preprocessor in list |
Definition at line 143 of file Features.cpp.
|
inherited |
|
virtualinherited |
A shallow copy. All the SGObject instance variables will be simply assigned and SG_REF-ed.
Reimplemented in CGaussianKernel.
Definition at line 192 of file SGObject.cpp.
|
virtualinherited |
method may be overwritten to update things that depend on subset
Reimplemented in CStringFeatures< ST >, CStringFeatures< T >, CStringFeatures< uint8_t >, CStringFeatures< char >, and CStringFeatures< uint16_t >.
Definition at line 293 of file Features.h.
|
virtualinherited |
does this class support compatible computation bewteen difference classes? for example, this->dot(rhs_prt), can rhs_prt be an instance of a difference class?
Reimplemented in CDenseSubSamplesFeatures< ST >.
Definition at line 323 of file Features.h.
|
inherited |
unset generic type
this has to be called in classes specializing a template class
Definition at line 303 of file SGObject.cpp.
|
inherited |
|
virtualinherited |
Updates the hash of current parameter combination
Definition at line 248 of file SGObject.cpp.
|
protectedinherited |
feature weighting in combined dot features
Definition at line 249 of file DotFeatures.h.
|
protected |
the dataset to work on
Definition at line 240 of file RandomKitchenSinksDotFeatures.h.
|
inherited |
io
Definition at line 369 of file SGObject.h.
|
inherited |
parameters wrt which we can compute gradients
Definition at line 384 of file SGObject.h.
|
inherited |
Hash of parameter values
Definition at line 387 of file SGObject.h.
|
inherited |
model selection parameters
Definition at line 381 of file SGObject.h.
|
inherited |
parameters
Definition at line 378 of file SGObject.h.
|
protectedinherited |
subset used for index transformations
Definition at line 352 of file Features.h.
|
protected |
the number of samples to use
Definition at line 243 of file RandomKitchenSinksDotFeatures.h.
|
inherited |
parallel
Definition at line 372 of file SGObject.h.
random coefficients of the function phi, drawn from p
Definition at line 246 of file RandomKitchenSinksDotFeatures.h.
|
inherited |
version
Definition at line 375 of file SGObject.h.