/******************************************************************** ** Image Component Library (ICL) ** ** ** ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** ** Neuroinformatics Group ** ** Website: www.iclcv.org and ** ** http://opensource.cit-ec.de/projects/icl ** ** ** ** File : ICLMath/src/ICLMath/LLM.cpp ** ** Module : ICLMath ** ** Authors: Christof Elbrechter ** ** ** ** ** ** GNU LESSER GENERAL PUBLIC LICENSE ** ** This file may be used under the terms of the GNU Lesser General ** ** Public License version 3.0 as published by the ** ** ** ** Free Software Foundation and appearing in the file LICENSE.LGPL ** ** included in the packaging of this file. Please review the ** ** following information to ensure the license requirements will ** ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** ** ** ** The development of this software was supported by the ** ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** ** Forschungsgemeinschaft (DFG) in the context of the German ** ** Excellence Initiative. ** ** ** ********************************************************************/ #include #include #include #include #include using namespace icl::utils; namespace icl{ namespace math{ namespace{ inline float square_vec(const float *a,const float *b,unsigned int dim){ // {{{ open float sum = 0; for(unsigned int i=0;i(v[i])+ ((iinputDim, this->w_in ); std::copy(w_out, w_out+this->outputDim,this->w_out); std::copy(w_in, w_in+this->inputDim*this->outputDim, this->A); } LLM::Kernel &LLM::Kernel::operator=(const LLM::Kernel &k){ // {{{ open inputDim = k.inputDim; outputDim = k.outputDim; if(inputDim){ if(w_in) delete [] w_in; if(dw_in) delete [] dw_in; if(var) delete [] var; w_in = new float [inputDim]; dw_in = new float [inputDim]; var = new float [inputDim]; memcpy(w_in,k.w_in,inputDim*sizeof(float)); memcpy(dw_in,k.dw_in,inputDim*sizeof(float)); memcpy(var,k.var,inputDim*sizeof(float)); }else{ w_in = 0; dw_in = 0; var = 0; } if(outputDim){ if(w_out)delete [] w_out; w_out = new float [outputDim]; memcpy(w_out,k.w_out,outputDim*sizeof(float)); }else{ w_out = 0; } if(inputDim && outputDim){ if(A) delete [] A; A = new float [inputDim*outputDim]; memcpy(A,k.A,inputDim*outputDim*sizeof(float)); }else{ A = 0; } return *this; } // }}} LLM::Kernel::Kernel(const LLM::Kernel &k): // {{{ open inputDim(k.inputDim),outputDim(k.outputDim){ if(inputDim){ w_in = new float [inputDim]; dw_in = new float [inputDim]; var = new float [inputDim]; memcpy(w_in,k.w_in,inputDim*sizeof(float)); memcpy(dw_in,k.dw_in,inputDim*sizeof(float)); memcpy(var,k.var,inputDim*sizeof(float)); }else{ w_in = 0; dw_in = 0; var = 0; } if(outputDim){ w_out = new float [outputDim]; memcpy(w_out,k.w_out,outputDim*sizeof(float)); }else{ w_out = 0; } if(inputDim && outputDim){ A = new float [inputDim*outputDim]; memcpy(A,k.A,inputDim*outputDim*sizeof(float)); }else{ A = 0; } } // }}} void LLM::Kernel::show(unsigned int idx) const{ // {{{ open printf("K:%d (%d>%d) w_in=%s w_out=%s A=%s sigma² = %s\n", idx,inputDim,outputDim, vecToStr(w_in,inputDim).c_str(), vecToStr(w_out,outputDim).c_str(), vecToStr(A,inputDim*outputDim).c_str(), vecToStr(var,inputDim).c_str()); } // }}} void LLM::init_private(unsigned int inputDim, unsigned int outputDim){ m_inputDim = inputDim; m_outputDim = outputDim; // m_bUseSoftMax = true; m_outBuf.resize(outputDim); m_gBuf.resize(outputDim); m_errorBuf.resize(outputDim); /* m_epsilonIn = 0.01; m_epsilonOut = 0.01; m_epsilonA = 0;//0.000001; m_epsilonSigma = 0.001; */ addProperty("epsilon In","range","[0,0.1]",0.01,0,"input weight learning rate"); addProperty("epsilon Out","range","[0,0.5]",0.01,0,"output weight learning rate"); addProperty("epsilon A","range","[0,0.1]",0.001,0,"slope matrix learning rate"); addProperty("epsilon Sigma","range","[0,0.1]",0.0,0,"kernel variance learning rate"); addProperty("soft max enabled","flag","",true,0,"enables the soft-max interpolation"); } LLM::LLM(unsigned int inputDim, unsigned int outputDim){ // {{{ open init_private(inputDim,outputDim); } // }}} LLM::LLM(unsigned int inputDim, unsigned int outputDim, unsigned int numCenters, const std::vector > &ranges, const std::vector &var){ init_private(inputDim,outputDim); init(numCenters, ranges, var); } void LLM::init(unsigned int numCenters, const std::vector > &ranges,const std::vector &var){ // {{{ open ICLASSERT_RETURN(ranges.size() == m_inputDim); std::vector centers(numCenters); for(unsigned int i=0;i ¢ers,const std::vector &var){ // {{{ open m_gBuf.resize(centers.size()); m_kernels.resize(centers.size()); for(unsigned int i=0;i no "steigung?" std::fill(k.dw_in,k.dw_in+m_inputDim,0); std::copy(var.begin(),var.end(), k.var); } } // }}} void LLM::showKernels() const{ // {{{ open printf("llm kernels: \n"); for(unsigned int i=0;i> 1) & 1){ trainSigmasIntern(x,g); } const float eIn = getPropertyValue("epsilon In"); const float *dy = 0; if( (trainflags >> 2) & 1){ dy = getErrorVecIntern(y,applyIntern(x, g)); trainOutputsIntern(x,y,g,dy,((trainflags&1)&&eIn)?true:false); } if( (trainflags >> 3) & 1){ if(!dy) dy = getErrorVecIntern(y,applyIntern(x, g)); trainMatricesIntern(x,y,g,dy); } } // }}} void LLM::trainCenters(const float *x){ // {{{ open trainCentersIntern(x,updateGs(x)); } // }}} void LLM::trainSigmas(const float *x){ // {{{ open trainSigmasIntern(x,updateGs(x)); } // }}} void LLM::trainOutputs(const float *x,const float *y){ // {{{ open trainOutputsIntern(x,y,updateGs(x),getErrorVec(x,y),false); } // }}} void LLM::trainMatrices(const float *x,const float *y){ // {{{ open trainMatricesIntern(x,y,updateGs(x),getErrorVec(x,y)); } // }}} const float *LLM::updateGs(const float *x){ // {{{ open float *g = m_gBuf.data(); if(getPropertyValue("soft max enabled")){ //e/ calculate g_i(x) = (exp(-beta*|x-w_i^in|)) / (sum_j exp(-beta*|x-w_j^in|)) float sum_gi = 0; for(unsigned int i=0;i