ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkLayerBase.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkLayerBase_h
00019 #define __itkLayerBase_h
00020 
00021 #include <iostream>
00022 #include "itkTransferFunctionBase.h"
00023 #include "itkInputFunctionBase.h"
00024 
00025 #include "itkWeightSetBase.h"
00026 
00027 namespace itk
00028 {
00029 namespace Statistics
00030 {
00037 template<class TMeasurementVector, class TTargetVector>
00038 class LayerBase : public LightProcessObject
00039 {
00040 
00041 public:
00042   typedef LayerBase                Self;
00043   typedef LightProcessObject       Superclass;
00044   typedef SmartPointer<Self>       Pointer;
00045   typedef SmartPointer<const Self> ConstPointer;
00046 
00048   itkTypeMacro(LayerBase, LightProcessObject);
00049 
00050   typedef TMeasurementVector InputVectorType;
00051   typedef TTargetVector      OutputVectorType;
00052 
00053   typedef typename TMeasurementVector::ValueType ValueType;
00054   typedef ValueType*                             ValuePointer;
00055   typedef const ValueType*                       ValueConstPointer;
00056   typedef vnl_vector<ValueType>                  NodeVectorType;
00057   typedef Array<ValueType>                       InternalVectorType;
00058 
00059   typedef LayerBase                                       LayerInterfaceType;
00060   typedef WeightSetBase<TMeasurementVector,TTargetVector> WeightSetType;
00061   typedef WeightSetBase<TMeasurementVector,TTargetVector> WeightSetInterfaceType;
00062   typedef InputFunctionBase<ValueType*, ValueType>        InputFunctionInterfaceType;
00063   typedef TransferFunctionBase<ValueType>                 TransferFunctionInterfaceType;
00064 
00065   //The only valid layer types
00066   typedef enum {  INVALIDLAYER=0, INPUTLAYER=1, HIDDENLAYER=2, OUTPUTLAYER=3 } LayerTypeCode;
00067 
00068   virtual void SetNumberOfNodes(unsigned int);
00069   unsigned int GetNumberOfNodes() const;
00070 
00071   virtual ValueType GetInputValue(unsigned int) const = 0;
00072   virtual ValueType GetOutputValue(unsigned int) const = 0;
00073   virtual ValuePointer GetOutputVector() = 0;
00074 
00075   virtual void ForwardPropagate(){};
00076   virtual void ForwardPropagate(TMeasurementVector){};
00077 
00078   virtual void BackwardPropagate(){};
00079   virtual void BackwardPropagate(InternalVectorType){};
00080 
00081   virtual ValueType GetOutputErrorValue(unsigned int) const = 0;
00082   virtual void SetOutputErrorValues(TTargetVector) {};
00083 
00084   virtual ValueType GetInputErrorValue(unsigned int) const = 0;
00085   virtual ValuePointer GetInputErrorVector() = 0;
00086   virtual void SetInputErrorValue(ValueType, unsigned int) {};
00087 
00088   //itkSetObjectMacro(InputWeightSet, WeightSetInterfaceType);
00089   void SetInputWeightSet(WeightSetInterfaceType*);
00090   itkGetObjectMacro(InputWeightSet, WeightSetInterfaceType);
00091   itkGetConstObjectMacro(InputWeightSet, WeightSetInterfaceType);
00092 
00093   //itkSetObjectMacro(OutputWeightSet, WeightSetInterfaceType);
00094   void SetOutputWeightSet(WeightSetInterfaceType*);
00095   itkGetObjectMacro(OutputWeightSet, WeightSetInterfaceType);
00096   itkGetConstObjectMacro(OutputWeightSet, WeightSetInterfaceType);
00097 
00098   void SetNodeInputFunction(InputFunctionInterfaceType* f);
00099   itkGetObjectMacro(NodeInputFunction, InputFunctionInterfaceType);
00100   itkGetConstObjectMacro(NodeInputFunction, InputFunctionInterfaceType);
00101 
00102   void SetTransferFunction(TransferFunctionInterfaceType* f);
00103   itkGetObjectMacro(ActivationFunction, TransferFunctionInterfaceType);
00104   itkGetConstObjectMacro(ActivationFunction, TransferFunctionInterfaceType);
00105 
00106   virtual ValueType Activation(ValueType) = 0;
00107   virtual ValueType DActivation(ValueType) = 0;
00108 
00109   itkSetEnumMacro(LayerTypeCode, LayerTypeCode);
00110   itkGetEnumMacro(LayerTypeCode, LayerTypeCode);
00111 
00112   //#define __USE_OLD_INTERFACE  Comment out to ensure that new interface works
00113 #ifdef __USE_OLD_INTERFACE
00114   void SetLayerType(const LayerTypeCode value) { SetLayerTypeCode(value); }
00115   LayerTypeCode GetLayerType(void) { return GetLayerTypeCode(); }
00116   //For backwards compatibility
00117   void SetLayerType(const unsigned int value)
00118     {
00119     switch(value)
00120       {
00121       case 0:
00122         SetLayerType(INVALIDLAYER);
00123         break;
00124       case 1:
00125         SetLayerType(INPUTLAYER);
00126         break;
00127       case 2:
00128         SetLayerType(HIDDENLAYER);
00129         break;
00130       case 3:
00131         SetLayerType(OUTPUTLAYER);
00132         break;
00133       default:
00134         //Throw Exception Here
00135         break;
00136       }
00137     }
00138 #endif
00139   itkSetMacro(LayerId,unsigned int);
00140   itkGetConstReferenceMacro(LayerId,unsigned int);
00141 
00142   //virtual void SetBias(const ValueType) = 0;
00143   //virtual const ValueType & GetBias() const = 0;
00144 
00145 protected:
00146   LayerBase();
00147   ~LayerBase();
00148 
00150   virtual void PrintSelf( std::ostream& os, Indent indent ) const;
00151 
00152   LayerTypeCode m_LayerTypeCode; //input, hidden, output
00153   unsigned int  m_LayerId;
00154   unsigned int  m_NumberOfNodes;
00155 
00156   typename WeightSetInterfaceType::Pointer m_InputWeightSet;
00157   typename WeightSetInterfaceType::Pointer m_OutputWeightSet;
00158 
00159   typename TransferFunctionInterfaceType::Pointer m_ActivationFunction;
00160   typename InputFunctionInterfaceType::Pointer    m_NodeInputFunction;
00161 
00162 }; //class layer base
00163 
00164 } //namespace itk
00165 } //namespace statistics
00166 
00167 #ifndef ITK_MANUAL_INSTANTIATION
00168 #include "itkLayerBase.hxx"
00169 #endif
00170 
00171 #endif
00172