Main Page | Namespace List | Data Structures | File List | Data Fields | Globals

neuron.cpp

Go to the documentation of this file.
00001 # include <neuron.h>
00002 # include <network.h>
00003 # include <qgl.h>
00004 # include <qglviewer.h>
00005 # include <inn.h>
00006 # include <interface.h>
00007 
00008 using namespace qglviewer ;
00009 
00010 extern Inn * inn ;
00011 extern Network * network ;
00012 
00013 Neuron::Neuron(int _id) {
00014    id = _id;
00015    axonsIn = new list<Axon*>();
00016    axonsOut = new list<Axon*>();
00017    frame_ = new ManipulatedFrame() ;
00018    outValue = 0.0 ;
00019 }
00020 
00021 int Neuron::getId() {
00022         return id;
00023 }
00024 
00025 double Neuron::getOutValue() {
00026         return outValue;
00027 }
00028 
00029 double Neuron::getGradientValue() {
00030         return gradientValue;
00031 }
00032 
00033 void Neuron::setOutValue(double v) {
00034         outValue = v;
00035 }
00036 
00037 void Neuron::setGradientValue(double v) {
00038         gradientValue = v;
00039 }
00040 
00041 void Neuron::draw( int selected, const bool names ) {
00042         if( network->displayneurons() ) {
00043                 Vec position = frame_->position() ;
00044                 position.y = network->getLayerNumTheNeuronIsOn(id) ;
00045                 frame_->setPosition( position ) ;
00046                 glPushMatrix() ;
00047                         glMultMatrixd(frame_->matrix());      
00048                         if( names ) glPushName(id);
00049                                 if( selected != id ) glColor4f( 0.0+outValue, 1.0-outValue, 0.0, 0.9 ) ;
00050                                 else glColor4f( 1.0, 0.0, 1.0, 0.8 ) ;
00051                                 glutSolidSphere( network->neuronsize(), network->shininess(), network->shininess() ) ;
00052                                 if( network->displayvalues() ) {
00053                                         glDisable(GL_LIGHTING) ;
00054                                         glColor3ub( 200, 200, 200 ) ;
00055                                         QString value ; value.setNum(outValue, 'f', 3) ;
00056                                         float x, y, z ;
00057                                         inn->viewer->camera()->getPosition( x, y, z ) ;
00058                                         inn->viewer->drawText( network->neuronsize()*-0.5, network->neuronsize()*-0.5, network->neuronsize()*1.20, value, QFont( "Arial", 30/((int)z+1) ) );  
00059                                         glEnable(GL_LIGHTING) ;
00060                                 }
00061                         if( names ) glPopName() ;
00062                 glPopMatrix() ;
00063         }
00064 }
00065 
00066 float Neuron::getX( void ) { return frame_->position().x ; }
00067 
00068 float Neuron::getY( void ) { return frame_->position().y ; }
00069 
00070 float Neuron::getZ( void ) { return frame_->position().z ; }
00071 
00072 void Neuron::setX(float x) { 
00073         Vec position = frame_->position() ;
00074         position.x = x ;
00075         frame_->setPosition( position ) ;
00076 }
00077 
00078 void Neuron::setY( float y) {
00079         Vec position = frame_->position() ;
00080         position.y = y ;
00081         frame_->setPosition( position ) ;
00082 }
00083 
00084 void Neuron::setZ(float z) { 
00085         Vec position = frame_->position() ;
00086         position.z = z ;
00087         frame_->setPosition( position ) ;
00088 }
00089 
00090 ManipulatedFrame * Neuron::frame( void ) {
00091         return frame_ ;
00092 }
00093 
00094 void Neuron::addAxonIn(Axon *a) {
00095         axonsIn->push_back(a);
00096 }
00097 
00098 void Neuron::removeAxonIn(int id_ax) {
00099         bool found = false;
00100         list<Axon*>::iterator iter = axonsIn->begin();
00101         while (iter != axonsIn->end() && !found) {
00102                 if((*iter)->getId() == id_ax) found = true;
00103                 if (!found) iter++;
00104         }
00105         if (iter != axonsIn->end())
00106                 axonsIn->erase(iter);
00107 }
00108 
00109 void Neuron::addAxonOut(Axon *a) {
00110         axonsOut->push_back(a);
00111 }
00112 
00113 void Neuron::removeAxonOut(int id_ax) {
00114         bool found = false;
00115         list<Axon*>::iterator iter = axonsOut->begin();
00116         while (iter != axonsOut->end() && !found) {
00117                 if((*iter)->getId() == id_ax) found = true;
00118                 if (!found) iter++;
00119         }
00120         if (iter != axonsOut->end())
00121                 axonsOut->erase(iter);
00122 }
00123 
00124 void Neuron::removeAxons() {
00125         list<Axon*>::iterator iterI, iterO;
00126         
00127         for (iterI = axonsIn->begin(); iterI != axonsIn->end(); iterI++) {
00128                 (*iterI)->getNeuronIn()->removeAxonOut((*iterI)->getId());
00129                 network->removeAxon((*iterI)->getId(), false);
00130         }
00131         for (iterO = axonsOut->begin(); iterO != axonsOut->end(); iterO++) {
00132                 (*iterO)->getNeuronOut()->removeAxonIn((*iterO)->getId());
00133                 network->removeAxon((*iterO)->getId(), false);
00134         }
00135         axonsIn->clear();
00136         axonsOut->clear();
00137 }
00138 
00139 void Neuron::setOutValueTemp(double val) {
00140         outValueTemp = val;
00141 }
00142 
00143 void Neuron::incOutValueTemp(double val) {
00144         outValueTemp += val;
00145 }
00146 
00147 void Neuron::setGradientValueTemp(double val) {
00148         gradientValueTemp = val;
00149 }
00150 
00151 void Neuron::incGradientValueTemp(double val) {
00152         gradientValueTemp += val;
00153 }
00154 
00155 double Neuron::sigmoide(double val) {
00156         return (1.0 / (1 + exp(-1.0 * network->getK() * val)));
00157 }
00158 
00159 double Neuron::sigmoideDerived(double val) {
00160         //return ((exp(-1.0*val))/((1+exp(-1.0*val))*(1+exp(-1.0*val))));
00161         return (sigmoide(val) * (1 - sigmoide(val)));
00162 }
00163 
00164 void Neuron::computeOutValue() {
00165         outValue = sigmoide(outValueTemp);
00166 }
00167 
00168 void Neuron::computeGradientValue(double err) {
00169         //gradientValue = 2 * sigmoideDerived(outValueTemp) * err;
00170         gradientValue = 2 * sigmoideDerived(outValue) * err;
00171 }
00172 
00173 void Neuron::forwardPropagation() {
00174         list<Axon*>::iterator iter;
00175         for (iter = axonsIn->begin(); iter != axonsIn->end(); iter++)
00176                 (*iter)->forwardPropagation();
00177         outValue = sigmoide(outValueTemp);
00178 }
00179 
00180 void Neuron::gradientBackPropagation() {
00181         list<Axon*>::iterator iter;
00182         for (iter = axonsOut->begin(); iter != axonsOut->end(); iter++)
00183                 (*iter)->gradientPropagation();
00184         //gradientValue = sigmoideDerived(outValueTemp) * gradientValueTemp;
00185         gradientValue = sigmoideDerived(outValue) * gradientValueTemp;
00186 }

Generated on Fri Dec 3 14:57:50 2004 for INN by doxygen 1.3.6