MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
LSTMUnitLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.param;
8
9namespace MyCaffe.layers
10{
17 public class LSTMUnitLayer<T> : Layer<T>
18 {
19 // The hidden output dimension.
20 int m_nHiddenDim;
21 Blob<T> m_blobXActs;
22
30 : base(cuda, log, p)
31 {
33 m_blobXActs = new Blob<T>(cuda, log);
34 m_blobXActs.Name = m_param.name + " xacts";
35 }
36
38 protected override void dispose()
39 {
40 if (m_blobXActs != null)
41 {
42 m_blobXActs.Dispose();
43 m_blobXActs = null;
44 }
45
46 base.dispose();
47 }
48
50 protected override void setup_internal_blobs(BlobCollection<T> col)
51 {
52 if (col.Count > 0)
53 return;
54
55 col.Add(m_blobXActs);
56 }
57
61 public override int ExactNumBottomBlobs
62 {
63 get { return 3; }
64 }
65
69 public override int ExactNumTopBlobs
70 {
71 get { return 2; }
72 }
73
80 public override bool AllowForceBackward(int nBottomIdx)
81 {
82 // Can't propagate to sequence contination indicators.
83 if (nBottomIdx != 2)
84 return true;
85
86 return false;
87 }
88
94 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
95 {
96 }
97
103 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
104 {
105 int nNumInstances = colBottom[0].shape(1);
106
107 for (int i = 0; i < colBottom.Count; i++)
108 {
109 if (i == 2)
110 m_log.CHECK_EQ(2, colBottom[i].num_axes, "There should be 2 axes at bottom[2]");
111 else
112 m_log.CHECK_EQ(3, colBottom[i].num_axes, "There should be 3 axes at bottom[" + i.ToString() + "]");
113
114 m_log.CHECK_EQ(1, colBottom[i].shape(0), "The shape(0) at bottom[" + i.ToString() + "] should be 1.");
115 m_log.CHECK_EQ(nNumInstances, colBottom[i].shape(1), "The shape(1) at bottom[" + i.ToString() + "] should equal the number of instances (" + nNumInstances.ToString() + ")");
116 }
117
118 m_nHiddenDim = colBottom[0].shape(2);
119 m_log.CHECK_EQ(4 * m_nHiddenDim, colBottom[1].shape(2), "The bottom[1].shape(2) should equal the 4 * the number of hidden dimensions (4 x " + m_nHiddenDim.ToString() + ")");
120 colTop[0].ReshapeLike(colBottom[0]);
121 colTop[1].ReshapeLike(colBottom[0]);
122 m_blobXActs.ReshapeLike(colBottom[1]);
123 }
124
150 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
151 {
152 int nCount = colTop[1].count();
153 long hC_prev = colBottom[0].gpu_data;
154 long hX = colBottom[1].gpu_data;
155 long hCont = colBottom[2].gpu_data;
156 long hX_acts = m_blobXActs.mutable_gpu_data;
157 long hC = colTop[0].mutable_gpu_data;
158 long hH = colTop[1].mutable_gpu_data;
159 int nXCount = colBottom[1].count();
160
161 m_cuda.lstm_unit_fwd(nCount, m_nHiddenDim, nXCount, hX, hX_acts, hC_prev, hCont, hC, hH);
162 }
163
194 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
195 {
196 m_log.CHECK(!rgbPropagateDown[2], "Cannot backpropagate to sequence indicators.");
197
198 if (!rgbPropagateDown[0] && !rgbPropagateDown[1])
199 return;
200
201 int nCount = colTop[1].count();
202 long hC_prev = colBottom[0].gpu_data;
203 long hX_acts = m_blobXActs.gpu_data;
204 long hCont = colBottom[2].gpu_data;
205 long hC = colTop[0].gpu_data;
206 long hH = colTop[1].gpu_data;
207 long hC_diff = colTop[0].gpu_diff;
208 long hH_diff = colTop[1].gpu_diff;
209 long hC_prev_diff = colBottom[0].mutable_gpu_diff;
210 long hX_acts_diff = m_blobXActs.mutable_gpu_diff;
211 int nXCount = colBottom[1].count();
212 long hX_diff = colBottom[1].mutable_gpu_diff;
213
214 m_cuda.lstm_unit_bwd(nCount, m_nHiddenDim, nXCount, hC_prev, hX_acts, hC, hH, hCont, hC_diff, hH_diff, hC_prev_diff, hX_acts_diff, hX_diff);
215 }
216 }
217}
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK(bool b, string str)
Test a flag for true.
Definition: Log.cs:227
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
The BlobCollection contains a list of Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
int Count
Returns the number of items in the collection.
void ReshapeLike(BlobCollection< T > src)
Reshapes all blobs in the collection to the sizes of the source.
The Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
long mutable_gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1555
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1487
void ReshapeLike(Blob< T > b, bool? bUseHalfSize=null)
Reshape this Blob to have the same shape as another Blob.
Definition: Blob.cs:648
string Name
Get/set the name of the Blob.
Definition: Blob.cs:2184
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
Definition: Blob.cs:402
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1479
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
The LSTMUnitLayer is a helper for LSTMLayer that computes a single timestep of the non-linearity of t...
override bool AllowForceBackward(int nBottomIdx)
Returns true for all but the bottom index = 2 for you can't propagate to the sequence comtinuation in...
LSTMUnitLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The LSTMUnitLayer constructor.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
override int ExactNumBottomBlobs
Returns the exact number of required bottom (intput) Blobs: prevtime, gatein, seqcon
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the LSTMUnit inputs.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation.
override void dispose()
Releases all GPU and host resources used by the Layer.
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: cellst, hiddenst
An interface for the units of computation which can be composed into a Net.
Definition: Layer.cs:31
Log m_log
Specifies the Log for output.
Definition: Layer.cs:43
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
Definition: Layer.cs:47
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
Definition: Layer.cs:39
LayerParameter.LayerType m_type
Specifies the Layer type.
Definition: Layer.cs:35
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
LayerType
Specifies the layer type.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8
The MyCaffe.layers namespace contains all layers that have a solidified code base,...
Definition: LayerFactory.cs:15
The MyCaffe.param namespace contains parameters used to create models.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12