SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
RandomFourierDotFeatures.cpp
Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2013 Evangelos Anagnostopoulos
00008  * Copyright (C) 2013 Evangelos Anagnostopoulos
00009  */
00010 
00011 #include <shogun/base/Parameter.h>
00012 #include <shogun/mathematics/Math.h>
00013 #include <shogun/features/RandomFourierDotFeatures.h>
00014 
00015 namespace shogun {
00016 
00017 enum KernelName;
00018 
00019 CRandomFourierDotFeatures::CRandomFourierDotFeatures()
00020 {
00021     init(NOT_SPECIFIED, SGVector<float64_t>());
00022 }
00023 
00024 CRandomFourierDotFeatures::CRandomFourierDotFeatures(CDotFeatures* features,
00025     int32_t D, KernelName kernel_name, SGVector<float64_t> params)
00026 : CRandomKitchenSinksDotFeatures(features, D)
00027 {
00028     init(kernel_name, params);
00029     random_coeff = generate_random_coefficients();
00030 }
00031 
00032 CRandomFourierDotFeatures::CRandomFourierDotFeatures(CDotFeatures* features,
00033     int32_t D, KernelName kernel_name, SGVector<float64_t> params,
00034     SGMatrix<float64_t> coeff)
00035 : CRandomKitchenSinksDotFeatures(features, D, coeff)
00036 {
00037     init(kernel_name, params);
00038 }
00039 
00040 CRandomFourierDotFeatures::CRandomFourierDotFeatures(CFile* loader)
00041 {
00042     SG_NOTIMPLEMENTED;
00043 }
00044 
00045 CRandomFourierDotFeatures::CRandomFourierDotFeatures(const CRandomFourierDotFeatures& orig)
00046 : CRandomKitchenSinksDotFeatures(orig)
00047 {
00048     init(orig.kernel, orig.kernel_params);
00049 }
00050 
00051 CRandomFourierDotFeatures::~CRandomFourierDotFeatures()
00052 {
00053 }
00054 
00055 void CRandomFourierDotFeatures::init(KernelName kernel_name, SGVector<float64_t> params)
00056 {
00057     kernel = kernel_name;
00058     kernel_params = params;
00059 
00060     constant = num_samples>0 ? CMath::sqrt(2.0 / num_samples) : 1;
00061     m_parameters->add(&kernel_params, "kernel_params",
00062             "The parameters of the kernel to approximate");
00063     SG_ADD((machine_int_t* ) &kernel, "kernel",
00064             "The kernel to approximate", MS_NOT_AVAILABLE);
00065     SG_ADD(&constant, "constant", "A constant needed",
00066             MS_NOT_AVAILABLE);
00067 }
00068 
00069 CFeatures* CRandomFourierDotFeatures::duplicate() const
00070 {
00071     return new CRandomFourierDotFeatures(*this);
00072 }
00073 
00074 const char* CRandomFourierDotFeatures::get_name() const
00075 {
00076     return "RandomFourierDotFeatures";
00077 }
00078 
00079 float64_t CRandomFourierDotFeatures::post_dot(float64_t dot_result, index_t par_idx)
00080 {
00081     dot_result += random_coeff(random_coeff.num_rows-1, par_idx);
00082     return CMath::cos(dot_result) * constant;
00083 }
00084 
00085 SGVector<float64_t> CRandomFourierDotFeatures::generate_random_parameter_vector()
00086 {
00087     SGVector<float64_t> vec(feats->get_dim_feature_space()+1);
00088     switch (kernel)
00089     {
00090         case GAUSSIAN:
00091             for (index_t i=0; i<vec.vlen-1; i++)
00092             {
00093                 vec[i] = CMath::sqrt((float64_t) 1/kernel_params[0]) *
00094                             CMath::sqrt(2.0) * CMath::normal_random(0.0, 1);
00095             }
00096 
00097             vec[vec.vlen-1] = CMath::random(0.0, 2 * CMath::PI);
00098             break;
00099 
00100         default:
00101             SG_SERROR("Unknown kernel\n");
00102     }
00103     return vec;
00104 }
00105 
00106 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation