summaryrefslogtreecommitdiff
path: root/src/CuTNetLib/cuCompDisc.cc
blob: 9eb08a94c691f8ad9430c11dfa13ae89f089018c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

#include <stdio.h>
#include "cuCompDisc.h"
#include "cuNetwork.h"

#include "Error.h"


namespace TNet
{
  
  void 
  CuDiscrete::
  Propagate()
  {
    for (int i=0;i<inID.size(); i++)
      mBlocks[inID[i].block]->SetInput(GetInput(i),inID[i].pos);
    for (int i=0; i<mBlocks.size(); i++)
      mBlocks[i]->Propagate();
    mErrorOutput.Init(GetOutput());
  }

  void 
  CuDiscrete::
  PropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
  {
    Error("Not applicable");
  }

  void 
  CuDiscrete::
  Backpropagate()
  {
    for (int i=0;i<outID.size(); i++)
      mBlocks[outID[i].block]->SetErrorInput(GetOutput(i),outID[i].pos);
    for(int i=0; i<mBlocks.size(); i++)
      mBlocks[i]->Backpropagate();
    mOutput.Init(GetOutput());
  }
  
  void 
  CuDiscrete::
  BackpropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
  {
    Error("Not applicable");
  }

  void 
  CuDiscrete::
  Update() 
  {
    printf("update+\n");
    for(int i=0; i<mBlocks.size(); i++)
    if ( mBlocks[i]->IsUpdatable() )
    {
      CuUpdatableComponent& rComp = dynamic_cast<CuUpdatableComponent&>(*mBlocks[i]);
      rComp.Update();
    }
    printf("update-\n");
  }


  void
  CuDiscrete::
  ReadFromStream(std::istream& rIn)
  {
    int i;
    for(i=0; i<mBlocks.size(); i++) {
      delete mBlocks[i];
    }
    mBlocks.clear();
    inID.clear();
    outID.clear();
    CuComponent* comp;
    i=0;
    while ( NULL != (comp=CuNetwork::ComponentReader(rIn,NULL)) )
    {
      mBlocks.push_back(comp);
      for (int j=0;j<(comp->GetInSect());++j)
        inID.push_back(posID(i,j));
      for (int j=0;j<(comp->GetOutSect());++j)
        outID.push_back(posID(i,j));
      ++i;
    }
  }

   
  void
  CuDiscrete::
  WriteToStream(std::ostream& rOut)
  {
    for(int i=0; i<mBlocks.size(); i++)
      CuNetwork::ComponentDumper(rOut,*mBlocks[i]);
    rOut << "<endblock>\n";
  }

  void 
  CuCompound::
  PropagateF(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
  {
    int iLoc=0,oLoc=0;
    CuMatrix<BaseFloat> In;
    CuMatrix<BaseFloat> Out;
    for(int i=0; i<mBlocks.size(); i++)
    {
      In.Init(X,iLoc,mBlocks[i]->GetNInputs());
      Out.Init(Y,oLoc,mBlocks[i]->GetNOutputs());
      mBlocks[i]->PropagateF(In,Out);
      iLoc+=mBlocks[i]->GetNInputs();
      oLoc+=mBlocks[i]->GetNOutputs();
    }
  }
  
  void 
  CuCompound::
  BackpropagateF(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
  {
    int iLoc=0,oLoc=0;
    CuMatrix<BaseFloat> In;
    CuMatrix<BaseFloat> Out;
    for(int i=0; i<mBlocks.size(); i++)
    {
      In.Init(X,iLoc,mBlocks[i]->GetNOutputs());
      Out.Init(Y,oLoc,mBlocks[i]->GetNInputs());
      mBlocks[i]->BackpropagateF(In,Out);
      iLoc+=mBlocks[i]->GetNOutputs();
      oLoc+=mBlocks[i]->GetNInputs();
    }
  }
  
  void 
  CuCompound::
  PropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
  {
    Error("Not applicable");
  }
  
  void 
  CuCompound::
  BackpropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y)
  {
    Error("Not applicable");
  }

  void 
  CuCompound::
  Update() 
  {
    for(int i=0; i<mBlocks.size(); i++)
    if ( mBlocks[i]->IsUpdatable() )
    {
      CuUpdatableComponent& rComp = dynamic_cast<CuUpdatableComponent&>(*mBlocks[i]);
      rComp.Update();
    }
  }


  void
  CuCompound::
  ReadFromStream(std::istream& rIn)
  {
    for(int i=0; i<mBlocks.size(); i++) {
      delete mBlocks[i];
    }
    mBlocks.clear();
    CuComponent* comp;
    while ( NULL != (comp=CuNetwork::ComponentReader(rIn,NULL)) )
      mBlocks.push_back(comp);
  }

   
  void
  CuCompound::
  WriteToStream(std::ostream& rOut)
  {
    for(int i=0; i<mBlocks.size(); i++)
      CuNetwork::ComponentDumper(rOut,*mBlocks[i]);
    rOut << "<endblock>\n";
  }
 
} //namespace