Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

neuron.cc

Go to the documentation of this file.
00001 
00025 #include <magic/mmath.h>
00026 #include <magic/mgdev-eps.h>
00027 #include <magic/mclass.h>
00028 
00029 #include "inanna/initializer.h"
00030 
00031 
00032 // Implementations for initializer.h
00033 impl_abstract (NeuronInitializer, {Object});
00034 impl_dynamic (DummyInitializer, {NeuronInitializer});
00035 impl_dynamic (GaussianInitializer, {NeuronInitializer});
00036 
00037 // Local implementations
00038 impl_dynamic (BiNode, {Object});
00039 impl_dynamic (Neuron, {BiNode});
00040 
00041 
00042 
00044 //                 ___          o                  __  ___                  //
00045 //                |   | |          ___   ___   |  /  \ |  \                 //
00046 //                |   | |---    | /   ) |   \ -+-  __/ |   |                //
00047 //                |   | |   )   | |---  |      |     \ |   |                //
00048 //                `___´ |__/  \_|  \__   \__/   \ \__/ |__/                 //
00050 
00051 void Object3D::copy (const Object3D& other) {
00052     mCoord = other.mCoord;
00053 }
00054 
00055 void Object3D::getPlace (double& x, double& y, double& z) const {
00056     x = mCoord.x;
00057     y = mCoord.y;
00058     z = mCoord.z;
00059 }
00060 
00061 
00062 
00064 //                      ----  o |   |          |                            //
00065 //                      |   )   |\  |          |  ___                       //
00066 //                      |---  | | \ |  __   ---| /   )                      //
00067 //                      |   ) | |  \| /  \ (   | |---                       //
00068 //                      |___  | |   | \__/  ---|  \__                       //
00070 
00071 BiNode::~BiNode () {
00072 }
00073 
00074 void BiNode::copy (const BiNode& other) {
00075     mIncoming = other.mIncoming;
00076     mOutgoing = other.mOutgoing;
00077 
00078 #ifdef CMP_WARNINGS
00079 #warning "TODO: BiNode::copy()"
00080 #endif
00081     /*
00082     for (int j=0; j<mIncoming.size; j++) {
00083         ASSERTWITH (other.mIncoming.getp (j), format("%d, %d, %d", c0, j, other.mIncoming.size));
00084         ASSERTWITH (mIncoming.getp (j), format("%d, %d", j, other.mIncoming.size));
00085         mIncoming[j];
00086     }
00087     */
00088 }
00089 
00090 void BiNode::connectFrom (const BiNode& source) {
00091     Connection* newconn = new Connection (dynamic_cast<Neuron*>(&const_cast<BiNode&>(source)),
00092                                           dynamic_cast<Neuron*>(this));
00093     mIncoming.add (newconn);
00094     const_cast<BiNode&>(source).addOutgoing (newconn);
00095 }
00096 
00097 bool BiNode::connectedFrom (const BiNode& other) const {
00098     for (int i=0; i<mIncoming.size(); i++)
00099         if (&mIncoming[i].source() == &other)
00100             return true;
00101     return false;
00102 }
00103 
00104 // Disconnecting
00105 
00106 void BiNode::disconnectFrom (const Connection& connection) {
00107     for (int i=0; i<mIncoming.size(); i++)
00108         if (&mIncoming[i] == &connection) {
00109             mIncoming[i].setTarget (NULL);
00110 
00111             // Order the source unit to cut its link
00112             if (!isnull(connection.source()))
00113                 mIncoming[i].source().disconnectTo (connection);
00114             
00115             // We are the target, so we delete it.
00116             mIncoming.removeFill (i);
00117         }
00118 }
00119 
00120 void BiNode::disconnectTo (const Connection& connection) {
00121     for (int i=0; i<mOutgoing.size(); i++)
00122         if (&mOutgoing[i] == &connection) {
00123             mOutgoing[i].setSource (NULL);
00124 
00125             // We are the source, so we let the target to delete the
00126             // connection
00127             if (!isnull(mOutgoing[i].target()))
00128                 mOutgoing[i].target().disconnectFrom (connection);
00129 
00130             // We just cut our reference to it
00131             mOutgoing.cut (i);
00132             mOutgoing.removeFill (i);
00133         }
00134 }
00135 
00136 void BiNode::disconnectFrom (const BiNode& node) {
00137     for (int i=0; i<mIncoming.size(); i++)
00138         if (&mIncoming[i].source() == &node) {
00139             mIncoming[i].setTarget (NULL);
00140 
00141             // Order the source unit to cut its link
00142             if (!isnull(mIncoming[i].source()))
00143                 mIncoming[i].source().disconnectTo (mIncoming[i]);
00144             
00145             // We are the target, so we delete it.
00146             mIncoming.removeFill (i);
00147         }
00148 }
00149 
00150 void BiNode::disconnectTo (const BiNode& node) {
00151     for (int i=0; i<mOutgoing.size(); i++)
00152         if (&mOutgoing[i].target() == &node) {
00153             mOutgoing[i].setSource (NULL);
00154 
00155             // We are the source, so we let the target to delete the
00156             // connection
00157             if (!isnull(mOutgoing[i].target()))
00158                 mOutgoing[i].target().disconnectFrom (mOutgoing[i]);
00159 
00160             // We just cut our reference to it
00161             mOutgoing.cut (i);
00162             mOutgoing.removeFill (i);
00163         }
00164 }
00165 
00166 void BiNode::disconnectAll () {
00167     for (int i=0; i<mIncoming.size(); i++) {
00168         mIncoming[i].setTarget (NULL);
00169         
00170         // Order the source unit to cut its link
00171         if (!isnull(mIncoming[i].source()))
00172             mIncoming[i].source().disconnectTo (mIncoming[i]);
00173         
00174         // We are the target, so we delete it.
00175         mIncoming.remove (i);
00176     }
00177     mIncoming.make (0);
00178 
00179     for (int i=0; i<mOutgoing.size(); i++) {
00180         mOutgoing[i].setSource (NULL);
00181         
00182         // We are the source, so we let the target to delete the
00183         // connection
00184         if (!isnull(mOutgoing[i].target()))
00185             mOutgoing[i].target().disconnectFrom (mOutgoing[i]);
00186         
00187         // We just cut our reference to it
00188         mOutgoing.cut (i);
00189     }
00190     mOutgoing.make (0);
00191 }
00192 
00193 void BiNode::shallowDisconnectAll () {
00194     for (int i=0; i<mIncoming.size(); i++)
00195         mIncoming.cut (i);
00196     mIncoming.make (0);
00197     
00198     for (int i=0; i<mOutgoing.size(); i++)
00199         mOutgoing.cut (i);
00200     mOutgoing.make (0);
00201 }
00202 
00203 /*virtual*/ void BiNode::check (int netSize) const {
00204     for (int i=0; i<incomings(); i++)
00205         incoming(i).check (netSize);
00206 }
00207 
00208 /*
00209 void BiNode::writeXML (OStream& out) const {
00210     sout.printf ("\t<%s ID=%d>\n", (CONSTR) getclassname(), id());
00211     for (int i=0; i<mIncoming.size(); i++)
00212         mIncoming[i].writeXML (out);
00213     sout.printf ("\t</%s>\n", (CONSTR) getclassname());
00214 }
00215 */
00216 
00217 
00219 //                     |   |                                                //
00220 //                     |\  |  ___                   _                       //
00221 //                     | \ | /   ) |   | |/\  __  |/ \                      //
00222 //                     |  \| |---  |   | |   /  \ |   |                     //
00223 //                     |   |  \__   \__! |   \__/ |   |                     //
00225 
00226 Neuron::Neuron () : BiNode(), Object3D()
00227 {
00228     mActivation = 0.0;
00229     mBias = 0.0;
00230     mType = HIDDENUNIT;
00231     mTransferFunc = LOGISTIC_TF;
00232     mExists = true;
00233 }
00234 
00235 Neuron::~Neuron () {
00236     ;
00237 }
00238 
00239 void Neuron::copy (const Neuron& other) {
00240     Object3D::copy (other);
00241     BiNode::copy (other);
00242 
00243     // int j,c0=-666;
00244 
00245     mActivation = other.mActivation;
00246     mBias.copy (other.mBias);
00247     mType = other.mType;
00248     mTransferFunc = other.mTransferFunc;
00249     mExists = other.mExists;
00250 }
00251 
00252 void Neuron::init (double r)
00253 {
00254     if (mExists) {
00255         for (int i=0; i<incomings(); i++)
00256             incoming(i).init (r);
00257     }
00258     mBias.init (r);
00259 }
00260 
00261 void Neuron::transfer (ANNetwork& net)
00262 {
00263     if (mExists) {
00264         register double sum = mBias.weight ();
00265         for (register int i=0; i<incomings(); i++)
00266             sum += incoming(i).weight()*incoming(i).source().activation();
00267 
00268         // NOTE: The sigmoid function is currently hard-coded, as this
00269         // makes backprop A LOT faster
00270         mActivation = sigmoid (sum);
00271     } else
00272         mActivation = 0.0;
00273 }
00274 
00275 void Neuron::check (int netSize) const
00276 {
00277     ASSERT (mType>=0 && mType<=2);
00278     ASSERT (mTransferFunc>=0 && mTransferFunc<=2);
00279     mBias.check (1);
00280     ASSERT (mActivation>=-10000 && mActivation<=10000); // Sensible range
00281 
00282     BiNode::check (netSize);
00283 }
00284 
00285 void Neuron::operator= (const Neuron& other) {
00286     copy (other);
00287 }
00288 
00289 

Generated on Thu Feb 10 20:06:44 2005 for Inanna by doxygen1.2.18