• Main Page
  • Related Pages
  • Classes
  • Files
  • Examples
  • File List
  • File Members

createConnectionMatrix.h

Go to the documentation of this file.
00001 //===========================================================================
00043 //===========================================================================
00044 
00045 
00046 #ifndef CREATE_CONNECTION_MATRIX_H
00047 #define CREATE_CONNECTION_MATRIX_H
00048 
00049 #include <Array/ArrayIo.h>
00050 #include <fstream>
00051 
00052 
00053 //===========================================================================
00090 void createConnectionMatrix(Array<int> &con,
00091                             Array<unsigned> &layers,
00092                             bool ff_layer  = true,   // all connections
00093                             // between layers?
00094                             bool ff_in_out = true,   // shortcuts from in to
00095                             // out?
00096                             bool ff_all    = true,   // all shortcuts?
00097                             bool bias      = true)   // bias?
00098 {
00099     unsigned N = 0;  // total number of neurons:
00100     unsigned row,    // connection matrix row    (target neuron)
00101     column, // connection matrix column (start neuron)
00102     k,      // counter.
00103     z_pos,  // first target neuron in next layer
00104     s_pos;  // first start neuron in current layer
00105 
00106 
00107     //
00108     // Calculate total number of neurons from the
00109     // number of neurons per layer:
00110     //
00111     for (k = 0; k < layers.dim(0); k++) N += layers(k);
00112     con.resize(N, N + 1);
00113     con = 0;
00114 
00115     //
00116     // set connections from each neuron of layer i to each
00117     // neuron of layer i + 1 for all layers:
00118     //
00119     if (ff_layer)
00120     {
00121         z_pos = layers(0);
00122         s_pos = 0;
00123         for (k = 0; k < layers.dim(0) - 1; k++)
00124         {
00125             for (row = z_pos; row < z_pos + layers(k + 1); row++)
00126                 for (column = s_pos; column < s_pos + layers(k); column++)
00127                     con(row, column) = 1;
00128             s_pos += layers(k);
00129             z_pos += layers(k + 1);
00130         }
00131     }
00132 
00133     //
00134     // set connections from all input neurons to all output neurons:
00135     //
00136     if (ff_in_out)
00137     {
00138         for (row = N - layers(layers.dim(0) - 1); row < N; row++)
00139             for (column = 0; column < layers(0); column++)
00140                 con(row, column) = 1;
00141     }
00142 
00143     //
00144     // set connections from all neurons of layer i to
00145     // all neurons of layers j with j > i for all layers i:
00146     //
00147     if (ff_all)
00148     {
00149         z_pos = layers(0);
00150         s_pos = 0;
00151         for (k = 0; k < layers.dim(0) - 1; k++)
00152         {
00153             for (row = z_pos; row < z_pos + layers(k + 1); row++)
00154                 for (column = 0; column < s_pos + layers(k); column++)
00155                     con(row, column) = 1;
00156             s_pos += layers(k);
00157             z_pos += layers(k + 1);
00158         }
00159     }
00160 
00161     //
00162     // set connections from all neurons (except the input neurons)
00163     // to the bias values:
00164     //
00165     if (bias)
00166         for (k = layers(0); k < N; k++) con(k, N) = 1;
00167 
00168 }
00169 
00170 
00171 //===========================================================================
00212 void createConnectionMatrix(Array<int> &con,
00213                             unsigned in, unsigned hidden, unsigned out,
00214                             bool ff_layer  = true,  // all connections beteween layers?
00215                             bool ff_in_out = true,  // shortcuts from in to out?
00216                             bool ff_all    = true,  // all shortcuts?
00217                             bool bias      = true)
00218 { // bias?
00219     Array<unsigned> layer(3);
00220     layer(0) = in;
00221     layer(1) = hidden;
00222     layer(2) = out;
00223     createConnectionMatrix(con, layer, ff_layer, ff_in_out, ff_all, bias);
00224 }
00225 
00226 
00227 //===========================================================================
00269 void createConnectionMatrix(Array<int> &con,
00270                             unsigned in, unsigned hidden1, unsigned hidden2, unsigned out,
00271                             bool ff_layer  = true,  // all connections beteween layers?
00272                             bool ff_in_out = true,  // shortcuts from in to out?
00273                             bool ff_all    = true,  // all shortcuts?
00274                             bool bias      = true)
00275 { // bias?
00276     Array<unsigned> layer(4);
00277     layer(0) = in;
00278     layer(1) = hidden1;
00279     layer(2) = hidden2;
00280     layer(3) = out;
00281     createConnectionMatrix(con, layer, ff_layer, ff_in_out, ff_all, bias);
00282 }
00283 
00284 //===========================================================================
00295 void createConnectionMatrixRNN(Array<int> &con,
00296                                unsigned in, unsigned hidden, unsigned out,
00297                                unsigned memory = 1, bool layered = false,
00298                                bool recurrentInputs = true, bool bias = true, bool elman = false, bool previousInputs = false)
00299 {
00300     unsigned N;  // total number of neurons:
00301     unsigned k, i, j;
00302 
00303     //
00304     // Calculate total number of neurons from the
00305     // number of neurons per layer:
00306     //
00307     N = in + hidden + out;
00308     con.resize(1 + memory, N, N);
00309     con[0] = 0;
00310 
00311     if (!layered)
00312     {
00313         for (i = in; i < N; i++)
00314         {
00315             for (j = 0; j < i; j++)
00316                 con(0, i, j) = 1;
00317             if (bias) con(0, i, i) = 1;
00318         }
00319     }
00320     else
00321     {
00322         for (i = in; i < N - out; i++)
00323         {
00324             for (j = 0; j < in; j++)
00325                 con(0, i, j) = 1;
00326             if (bias) con(0, i, i) = 1;
00327         }
00328         if (elman)
00329             for (i = N - out; i < N; i++)
00330             {
00331                 for (j = 0; j < N - out; j++)
00332                     con(0, i, j) = 1;
00333                 if (bias) con(0, i, i) = 1;
00334             }
00335         else
00336             for (i = N - out; i < N; i++)
00337             {
00338                 for (j = 0; j < in; j++)
00339                     con(0, i, j) = 1;
00340                 if (bias) con(0, i, i) = 1;
00341             }
00342     }
00343     if (recurrentInputs)
00344         for (i = 1; i <= memory; i++) con[i] = 1;
00345     else
00346     {
00347         for (i = 1; i <= memory; i++)
00348         {
00349             con[i] = 1;
00350             for (j = 0; j < in; j++)
00351             {
00352                 (con[i])[j] = 0;
00353                 if (!previousInputs) for (k = in; k < N; k++) con(i, k, j) = 0;
00354             }
00355         }
00356     }
00357 }
00358 
00359 #endif
00360 
00361 
00362 
  • Shark Main Page
  • Array
  • Rng
  • LinAlg
  • FileUtil
  • EALib
  • MOO-EALib
  • ReClaM
  • Fuzzy
  • Mixture
  • Tutorials
  • FAQ