Orfeo Toolbox  3.16
itkWeightSetBase.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkWeightSetBase.txx,v $
5  Language: C++
6  Date: $Date: 2007-08-27 17:38:59 $
7  Version: $Revision: 1.12 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkWeightSetBase_txx
18 #define __itkWeightSetBase_txx
19 
20 #include "itkWeightSetBase.h"
21 #include "stdlib.h"
22 
23 namespace itk
24 {
25 namespace Statistics
26 {
27 
28 template<class TMeasurementVector, class TTargetVector>
31 {
32  m_FirstPass = true;
33  m_SecondPass = true;
34  m_Range=1.0;
35  m_Momentum = 0;
36  m_Bias = 1;
37  m_NumberOfInputNodes = 0;
38  m_NumberOfOutputNodes = 0;
39  m_RandomGenerator = RandomVariateGeneratorType::New();
40  RandomVariateGeneratorType::IntegerType randomSeed = 14543;
41  m_RandomGenerator->Initialize( randomSeed );
42  m_InputLayerId = 0;
43  m_OutputLayerId = 0;
44  m_WeightSetId = 0;
45 }
46 
47 template<class TMeasurementVector, class TTargetVector>
50 {
51 }
52 
53 template<class TMeasurementVector, class TTargetVector>
54 void
57 {
58  m_Range = r;
59  this->Modified();
60 }
61 
62 template<class TMeasurementVector, class TTargetVector>
63 void
65 ::SetNumberOfInputNodes(unsigned int n)
66 {
67  m_NumberOfInputNodes = n + 1; //including bias
68  this->Modified();
69 }
70 
71 template<class TMeasurementVector, class TTargetVector>
72 unsigned int
75 {
76  return m_NumberOfInputNodes;
77 }
78 
79 template<class TMeasurementVector, class TTargetVector>
80 void
82 ::SetNumberOfOutputNodes(unsigned int n)
83 {
84  m_NumberOfOutputNodes = n;
85  this->Modified();
86 }
87 
88 template<class TMeasurementVector, class TTargetVector>
89 unsigned int
92 {
93  return m_NumberOfOutputNodes;
94 }
95 
96 template<class TMeasurementVector, class TTargetVector>
97 void
100 {
101  m_OutputValues.set_size(m_NumberOfOutputNodes, m_NumberOfInputNodes);
102  m_OutputValues.fill(0);
103  m_WeightMatrix.set_size(m_NumberOfOutputNodes, m_NumberOfInputNodes);
104  m_WeightMatrix.fill(0);
105 
106  m_DW.set_size(m_NumberOfOutputNodes, m_NumberOfInputNodes);
107  m_DW.fill(0);
108  m_DW_new.set_size(m_NumberOfOutputNodes, m_NumberOfInputNodes);
109  m_DW_new.fill(0);
110 
111  m_DW_m_1.set_size(m_NumberOfOutputNodes, m_NumberOfInputNodes);
112  m_DW_m_1.fill(0);
113  m_DW_m_2.set_size(m_NumberOfOutputNodes, m_NumberOfInputNodes);
114  m_DW_m_2.fill(0);
115 
116  m_DB_new.set_size(m_NumberOfOutputNodes);
117  m_DB_new.fill(0.0);
118  m_DB.set_size(m_NumberOfOutputNodes);
119  m_DB.fill(0.0);
120 
121  m_DB_m_1.set_size(m_NumberOfOutputNodes);
122  m_DB_m_1.fill(0);
123  m_DB_m_2.set_size(m_NumberOfOutputNodes);
124  m_DB_m_2.fill(0);
125 
126  m_Del.set_size(m_NumberOfOutputNodes, m_NumberOfInputNodes);
127  m_Del.fill(0);
128  m_Del_new.set_size(m_NumberOfOutputNodes, m_NumberOfInputNodes);
129  m_Del_new.fill(0);
130  m_Del_m_1.set_size(m_NumberOfOutputNodes, m_NumberOfInputNodes);
131  m_Del_m_1.fill(0);
132  m_Del_m_2.set_size(m_NumberOfOutputNodes, m_NumberOfInputNodes);
133  m_Del_m_2.fill(0);
134 
135  m_Delb.set_size(m_NumberOfOutputNodes);
136  m_Delb.fill(0);
137  m_Delb_new.set_size(m_NumberOfOutputNodes);
138  m_Delb_new.fill(0);
139  m_Delb_m_1.set_size(m_NumberOfOutputNodes);
140  m_Delb_m_1.fill(0);
141  m_Delb_m_2.set_size(m_NumberOfOutputNodes);
142  m_Delb_m_2.fill(0);
143 
144  m_InputLayerOutput.set_size(1, m_NumberOfInputNodes - 1);
145  m_InputLayerOutput.fill(0);
146 }
147 
148 template<class TMeasurementVector, class TTargetVector>
149 void
152 {
153  unsigned int num_rows = m_WeightMatrix.rows();
154  unsigned int num_cols = m_WeightMatrix.cols();
155  std::cout<<num_rows <<" "<<num_cols<<std::endl;
156  std::cout<<"conectivity matrix size = "<<m_ConnectivityMatrix.rows()<<" "
157  << m_ConnectivityMatrix.cols()<<std::endl;
158 
159  for (unsigned int i = 0; i < num_rows; i++)
160  {
161  for (unsigned int j = 0; j < num_cols; j++)
162  {
163  if(m_ConnectivityMatrix[i][j]==1)
164  {
165  m_WeightMatrix(i, j) = RandomWeightValue(-1*m_Range,m_Range);
166  }
167  else
168  {
169  m_WeightMatrix(i, j) = 0;
170  }
171  }
172  }
173 }
174 
175 template<class TMeasurementVector, class TTargetVector>
179 {
180  return static_cast<ValueType>(m_RandomGenerator->GetUniformVariate(low,high));
181 }
182 
183 template<class TMeasurementVector, class TTargetVector>
184 void
186 ::ForwardPropagate(ValueType* inputlayeroutputvalues)
187 {
188  vnl_vector<ValueType> layeroutput;
189 
190  layeroutput.set_size(m_NumberOfInputNodes - 1);
191  layeroutput.copy_in(inputlayeroutputvalues);
192  m_InputLayerOutput.set_row(0, layeroutput);
193 }
194 
195 template<class TMeasurementVector, class TTargetVector>
196 void
198 ::BackwardPropagate(ValueType* itkNotUsed(inputerrorvalues))
199 {
200 }
201 
202 template<class TMeasurementVector, class TTargetVector>
203 void
206 {
207  m_Delb.copy_in(d);
208  m_Delb_new += m_Delb;
209  this->Modified();
210 }
211 
212 template<class TMeasurementVector, class TTargetVector>
213 void
216 {
217  m_Del.copy_in(d);
218  m_Del_new += m_Del;
219  ValueType v = 0.0;
220  m_Del.set_column( m_NumberOfInputNodes-1,v);
221  m_Del_new.set_column( m_NumberOfInputNodes-1,v);
222  this->Modified();
223 }
224 
225 template<class TMeasurementVector, class TTargetVector>
226 void
229 {
230  vnl_matrix<ValueType> W_temp;
231  W_temp.set_size(m_NumberOfOutputNodes, m_NumberOfInputNodes);
232  W_temp.fill(0);
233  W_temp.copy_in(w);
234 
235  m_WeightMatrix = W_temp;
236  // ValueType v=0.0;
237  // m_WeightMatrix.set_column( m_NumberOfInputNodes-1,v);
238  this->Modified();
239 }
240 
241 template<class TMeasurementVector, class TTargetVector>
242 void
245 {
246  vnl_matrix<ValueType> DW_temp;
247  DW_temp.set_size(m_NumberOfOutputNodes, m_NumberOfInputNodes);
248  DW_temp.fill(0);
249  DW_temp.copy_in(dw);
250 
251  m_DW = DW_temp;
252  ValueType v=0.0;
253  m_DW.set_column( m_NumberOfInputNodes-1,v);
254  this->Modified();
255 }
256 
257 template<class TMeasurementVector, class TTargetVector>
258 void
261 {
262  vnl_vector<ValueType> db_temp;
263  db_temp.set_size(m_NumberOfOutputNodes);
264  db_temp.fill(0);
265  db_temp.copy_in(db);
266 
267  m_DB=db_temp;
268  this->Modified();
269 }
270 
271 template<class TMeasurementVector, class TTargetVector>
275 {
276  return m_Del_new.data_block();
277 }
278 
279 template<class TMeasurementVector, class TTargetVector>
283 {
284  return m_Delb_new.data_block();
285 }
286 
287 template<class TMeasurementVector, class TTargetVector>
291 {
292  return m_Del.data_block();
293 }
294 
295 template<class TMeasurementVector, class TTargetVector>
299 {
300  return m_Del_m_1.data_block();
301 }
302 
303 template<class TMeasurementVector, class TTargetVector>
307 {
308  return m_Del_m_2.data_block();
309 }
310 
311 template<class TMeasurementVector, class TTargetVector>
315 {
316  return m_Delb_m_1.data_block();
317 }
318 
319 template<class TMeasurementVector, class TTargetVector>
323 {
324  return m_DW.data_block();
325 }
326 
327 template<class TMeasurementVector, class TTargetVector>
331 {
332  return m_DB_m_1.data_block();
333 }
334 
335 template<class TMeasurementVector, class TTargetVector>
339 {
340  return m_DW_m_1.data_block();
341 }
342 
343 template<class TMeasurementVector, class TTargetVector>
347 {
348  return m_DW_m_2.data_block();
349 }
350 
351 template<class TMeasurementVector, class TTargetVector>
355 {
356  return m_InputLayerOutput.data_block();
357 }
358 
359 template<class TMeasurementVector, class TTargetVector>
363 {
364  return m_OutputValues.data_block();
365 }
366 
367 template<class TMeasurementVector, class TTargetVector>
371 {
372  return m_Delb.data_block();
373 }
374 
375 template<class TMeasurementVector, class TTargetVector>
379 {
380  return m_WeightMatrix.data_block();
381 }
382 
383 template<class TMeasurementVector, class TTargetVector>
387 {
388  return m_WeightMatrix.data_block();
389 }
390 
391 template<class TMeasurementVector, class TTargetVector>
392 void
395 {
396  m_ConnectivityMatrix = c;
397  this->Modified();
398 }
399 
400 template<class TMeasurementVector, class TTargetVector>
401 void
403 ::UpdateWeights(ValueType itkNotUsed(LearningRate))
404 {
405  m_Del_m_2 = m_Del_m_1; // save last weight update;
406  m_Del_m_1 = m_Del_new; // save last weight update;
407 
408  m_Delb_m_2 = m_Delb_m_1; // save last weight update;
409  m_Delb_m_1 = m_Delb_new; // save last weight update;
410 
411  m_Del_new.fill(0);
412  m_Delb_new.fill(0);
413 
414  m_DW.set_column(m_NumberOfInputNodes - 1, m_DB);
415  m_WeightMatrix += m_DW;
416  m_DW.set_column(m_NumberOfInputNodes - 1, m_Delb_new);
417 
418  m_DB_m_2 = m_DB_m_1;
419  m_DB_m_1 = m_DB;
420 
421  m_DW_m_2 = m_DW_m_1;
422  m_DW_m_1 = m_DW;
423 
424  if(m_FirstPass == true)
425  {
426  m_FirstPass = false;
427  }
428  else if(m_FirstPass == false && m_SecondPass==true)
429  {
430  m_SecondPass = false;
431  }
432 }
433 
434 
436 template<class TMeasurementVector, class TTargetVector>
437 void
439 ::PrintSelf( std::ostream& os, Indent indent ) const
440 {
441  Superclass::PrintSelf( os, indent );
442 
443  os << indent << "WeightSetBase(" << this << ")"
444  << std::endl;
445 
446  os << indent << "m_RandomGenerator = " << m_RandomGenerator
447  << std::endl;
448  os << indent << "m_NumberOfInputNodes = " << m_NumberOfInputNodes
449  << std::endl;
450  os << indent << "m_NumberOfOutputNodes = " << m_NumberOfOutputNodes
451  << std::endl;
452  os << indent << "m_OutputValues = " << m_OutputValues
453  << std::endl;
454  os << indent << "m_InputErrorValues = " << m_InputErrorValues
455  << std::endl;
456 
457  os << indent << "m_DW = " << m_DW
458  << std::endl;
459  os << indent << "m_DW_new = " << m_DW_new
460  << std::endl;
461  os << indent << "m_DW_m_1 = " << m_DW_m_1
462  << std::endl;
463  os << indent << "m_DW_m_2 = " << m_DW_m_2
464  << std::endl;
465  os << indent << "m_DW_m = " << m_DW_m
466  << std::endl;
467 
468  os << indent << "m_DB = " << m_DB
469  << std::endl;
470  os << indent << "m_DB_new = " << m_DB_new
471  << std::endl;
472  os << indent << "m_DB_m_1 = " << m_DB_m_1
473  << std::endl;
474  os << indent << "m_DB_m_2 = " << m_DB_m_2
475  << std::endl;
476 
477  os << indent << "m_Del = " << m_Del
478  << std::endl;
479  os << indent << "m_Del_new = " << m_Del_new
480  << std::endl;
481  os << indent << "m_Del_m_1 = " << m_Del_m_1
482  << std::endl;
483  os << indent << "m_Del_m_2 = " << m_Del_m_2
484  << std::endl;
485 
486  os << indent << "m_Delb = " << m_Delb
487  << std::endl;
488  os << indent << "m_Delb_new = " << m_Delb_new
489  << std::endl;
490  os << indent << "m_Delb_m_1 = " << m_Delb_m_1
491  << std::endl;
492  os << indent << "m_Delb_m_2 = " << m_Delb_m_2
493  << std::endl;
494 
495  os << indent << "m_InputLayerOutput = " << m_InputLayerOutput
496  << std::endl;
497  os << indent << "m_WeightMatrix = " << m_WeightMatrix
498  << std::endl;
499  os << indent << "m_ConnectivityMatrix = " << m_ConnectivityMatrix
500  << std::endl;
501 
502  os << indent << "m_Momentum = " << m_Momentum
503  << std::endl;
504  os << indent << "m_Bias = " << m_Bias
505  << std::endl;
506  os << indent << "m_FirstPass = " << m_FirstPass
507  << std::endl;
508  os << indent << "m_SecondPass = " << m_SecondPass
509  << std::endl;
510  os << indent << "m_Range = " << m_Range
511  << std::endl;
512 
513 }
514 
515 } // end namespace Statistics
516 } // end namespace itk
517 
518 #endif

Generated at Sun Feb 3 2013 00:15:00 for Orfeo Toolbox with doxygen 1.8.1.1