MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
ComputeGraph.cs
1using MyCaffe.basecode;
2using System;
3using System.Collections.Generic;
4using System.Diagnostics;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8
9namespace MyCaffe.common
10{
21 public class ComputeGraph<T> : IDisposable
22 {
23 Blob<T> m_blobWork;
24 CudaDnn<T> m_cuda;
25 Log m_log;
26 List<Tuple<string, Action>> m_rgBackprop = new List<Tuple<string, Action>>();
27 Dictionary<string, Blob<T>[]> m_rgDebug = new Dictionary<string, Blob<T>[]>();
28 bool m_bNeedsBackprop = true;
29 bool m_bCheckForNans = false;
30 bool m_bClipGradients = false;
31 bool m_bAddDebug = false;
32 int m_nAxis = 0;
33 string m_strMarker = null;
34
45 public ComputeGraph(CudaDnn<T> cuda, Log log, int nAxis, bool bNeedsBackprop = true, bool bClipGradients = false, bool bCheckNans = false, bool bAddDebug = false)
46 {
47 m_cuda = cuda;
48 m_log = log;
49 m_blobWork = new Blob<T>(cuda, log);
50 m_bNeedsBackprop = bNeedsBackprop;
51 m_bCheckForNans = bCheckNans;
52 m_bClipGradients = bClipGradients;
53 m_bAddDebug = bAddDebug;
54 m_nAxis = nAxis;
55 }
56
60 public void Dispose()
61 {
62 if (m_blobWork != null)
63 {
64 m_blobWork.Dispose();
65 m_blobWork = null;
66 }
67 }
68
69 private void add_debug(string str, params Blob<T>[] rg)
70 {
71 string strName = m_rgDebug.Count.ToString() + "_" + str;
72 m_rgDebug.Add(strName, rg);
73 }
74
78 public Dictionary<string, Blob<T>[]> Debug
79 {
80 get { return m_rgDebug; }
81 }
82
86 public string marker
87 {
88 get { return m_strMarker; }
89 set { m_strMarker = value; }
90 }
91
95 public bool needs_backprop
96 {
97 get { return m_bNeedsBackprop; }
98 set { m_bNeedsBackprop = value; }
99 }
100
104 public int axis
105 {
106 get { return m_nAxis; }
107 }
108
109 private Blob<T> work
110 {
111 get { return m_blobWork; }
112 }
113
114 private int input_count(Blob<T> b)
115 {
116 if (b.num_axes <= m_nAxis + 2)
117 return 1;
118
119 return b.count(m_nAxis + 2);
120 }
121
122 private void clip_gradient1(Blob<T> b)
123 {
124 float[] rg = Utility.ConvertVecF<T>(b.mutable_cpu_diff);
125
126 for (int i = 0; i < rg.Length; i++)
127 {
128 if (Math.Abs(rg[i]) < 0.000001)
129 rg[i] = 0;
130 else
131 rg[i] = (float)Math.Round(rg[i], 7);
132 }
133
134 b.mutable_cpu_diff = Utility.ConvertVec<T>(rg);
135 }
136
137 private void clip_gradient(params Blob<T>[] rg)
138 {
139 foreach (Blob<T> b in rg)
140 {
141 clip_gradient1(b);
142 }
143 }
144
145 private T[] round(T[] rgData1, int nDecimals)
146 {
147 float[] rgData = Utility.ConvertVecF<T>(rgData1);
148
149 for (int i = 0; i < rgData.Length; i++)
150 {
151 rgData[i] = (float)Math.Round(rgData[i], nDecimals);
152 }
153
154 return Utility.ConvertVec<T>(rgData);
155 }
156
157 private void check_nan(params Blob<T>[] rg)
158 {
159 for (int i = 0; i < rg.Length; i++)
160 {
161 work.ReshapeLike(rg[i]);
162 Tuple<double, double, double, double> data = rg[i].minmax_data(work, true);
163 Tuple<double, double, double, double> diff = rg[i].minmax_diff(work, true);
164
165 double dfDataNanCount = data.Item3;
166 double dfDataInfCount = data.Item4;
167 double dfDiffNanCount = diff.Item3;
168 double dfDiffInfCount = diff.Item4;
169
170 if (dfDataNanCount > 0 || dfDataInfCount > 0)
171 throw new Exception("NAN or INF detected in " + rg[i].Name + " data!");
172
173 if (dfDataNanCount > 0 || dfDataInfCount > 0)
174 throw new Exception("NAN or INF detected in " + rg[i].Name + " diff!");
175 }
176 }
177
178 private void apply(Blob<T> work, Blob<T> btm)
179 {
180 m_cuda.add(btm.count(), work.gpu_diff, btm.gpu_diff, btm.mutable_gpu_diff);
181 }
182
186 public void DebugOp(params Blob<T>[] rgB)
187 {
188 string strMarker = marker;
189 Action backward = () =>
190 {
191 string str = "";
192 for (int i = 0; i < rgB.Length; i++)
193 {
194 str += rgB[i].Name + ",";
195 }
196 str = str.TrimEnd(',');
197
198 Trace.WriteLine("Debugging at " + strMarker + " blobs: " + str);
199 };
200 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
201 }
202
209 public Blob<T> Round(Blob<T> b, int nDecimals = 6)
210 {
211 b.mutable_cpu_data = round(b.mutable_cpu_data, nDecimals);
212
213 if (m_bNeedsBackprop)
214 {
215 Action backward = () =>
216 {
217 b.mutable_cpu_diff = round(b.mutable_cpu_diff, nDecimals);
218 };
219 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
220 }
221
222 return b;
223 }
224
232 public Blob<T> PeekRow(Blob<T> btm, Blob<T> top, int ix)
233 {
234 string strMarker = marker;
235 List<int> rgShape = new List<int>() { 1, 1 };
236 rgShape[1] = btm.count(m_nAxis + 1);
237 top.Reshape(rgShape);
238
239 int nSpatialDim = btm.count(m_nAxis + 1);
240 m_cuda.copy(nSpatialDim, btm.gpu_data, top.mutable_gpu_data, nSpatialDim * ix, 0);
241
242 if (m_bNeedsBackprop)
243 {
244 Action backward = () =>
245 {
246 m_cuda.copy(nSpatialDim, top.gpu_diff, btm.mutable_gpu_diff, 0, nSpatialDim * ix);
247 m_cuda.copy(nSpatialDim, top.gpu_data, btm.mutable_gpu_data, 0, nSpatialDim * ix);
248
249 if (m_bCheckForNans)
250 check_nan(btm);
251 };
252 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
253 }
254
255 return top;
256 }
257
264 public void PeekItem(Blob<T> btm, Blob<T> top, int ix)
265 {
266 string strMarker = marker;
267 int nSpatialDim = btm.count(m_nAxis);
268
269 m_cuda.copy(nSpatialDim, btm.gpu_data, top.mutable_gpu_data, 0, nSpatialDim * ix);
270
271 if (m_bNeedsBackprop)
272 {
273 Action backward = () =>
274 {
275 btm.Reshape(1, 1, 1, 1);
276 m_cuda.copy(nSpatialDim, top.gpu_diff, btm.mutable_gpu_diff, nSpatialDim * ix, 0);
277 };
278 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
279 }
280 }
281
290 public Blob<T> CopyToRow(Blob<T> btm, Blob<T> top, int ix, bool bCopyDiff = false)
291 {
292 string strMarker = marker;
293 int nSpatialDim = btm.count(m_nAxis);
294
295 if (btm.count() == 0)
296 top.SetData(0, nSpatialDim * ix, nSpatialDim);
297 else
298 m_cuda.copy(nSpatialDim, btm.gpu_data, top.mutable_gpu_data, 0, nSpatialDim * ix);
299
300 if (m_bNeedsBackprop)
301 {
302 Action backward = () =>
303 {
304 if (bCopyDiff)
305 m_cuda.copy(nSpatialDim, top.gpu_diff, btm.mutable_gpu_diff, nSpatialDim * ix, 0);
306 m_cuda.copy(nSpatialDim, top.gpu_data, btm.mutable_gpu_data, nSpatialDim * ix, 0);
307
308 if (m_bCheckForNans)
309 check_nan(btm);
310 };
311 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
312 }
313
314 return top;
315 }
316
322 public void CopyToCache(Blob<T> btm, Cache<T> cache)
323 {
324 string strMarker = marker;
325 cache.CopyToCache(btm, m_nAxis);
326
327 if (m_bNeedsBackprop)
328 {
329 Action backward = () =>
330 {
331 cache.CopyFromCache(btm, m_nAxis);
332 };
333 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
334 }
335 }
336
343 public Blob<T> tanh(Blob<T> btm, Blob<T> top)
344 {
345 string strMarker = marker;
346 top.ReshapeLike(btm);
347
348 m_cuda.tanh_fwd(btm.count(), btm.gpu_data, top.mutable_gpu_data);
349
350 if (m_bNeedsBackprop)
351 {
352 Action backward = () =>
353 {
354 work.ReshapeLike(btm);
355 m_cuda.tanh_bwd(top.count(), top.gpu_diff, top.gpu_data, work.mutable_gpu_diff);
356 apply(work, btm);
357
358 if (m_bClipGradients)
359 clip_gradient(btm);
360 if (m_bCheckForNans)
361 check_nan(btm);
362 if (m_bAddDebug)
363 add_debug(strMarker + " - tanh", btm, top);
364 };
365 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
366 }
367
368 return top;
369 }
370
378 {
379 string strMarker = marker;
380 top.ReshapeLike(btm);
381
382 m_cuda.sigmoid_fwd(btm.count(), btm.gpu_data, top.mutable_gpu_data);
383
384 if (m_bNeedsBackprop)
385 {
386 Action backward = () =>
387 {
388 work.ReshapeLike(btm);
389 m_cuda.sigmoid_bwd(top.count(), top.gpu_diff, top.gpu_data, work.mutable_gpu_diff);
390 apply(work, btm);
391
392 if (m_bClipGradients)
393 clip_gradient(btm);
394 if (m_bCheckForNans)
395 check_nan(btm);
396 if (m_bAddDebug)
397 add_debug(strMarker + " - sigmoid", btm, top);
398 };
399 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
400 }
401
402 return top;
403 }
404
412 public Blob<T> eltmul(Blob<T> btm1, Blob<T> btm2, Blob<T> top)
413 {
414 string strMarker = marker;
415 top.ReshapeLike(btm1);
416
417 m_cuda.mul(top.count(), btm1.gpu_data, btm2.gpu_data, top.mutable_gpu_data);
418
419 if (m_bNeedsBackprop)
420 {
421 Action backward = () =>
422 {
423 work.ReshapeLike(btm1);
424 m_cuda.mul(btm2.count(), btm2.gpu_data, top.gpu_diff, work.mutable_gpu_diff);
425 apply(work, btm1);
426 work.ReshapeLike(btm2);
427 m_cuda.mul(btm1.count(), btm1.gpu_data, top.gpu_diff, work.mutable_gpu_diff);
428 apply(work, btm2);
429
430 if (m_bClipGradients)
431 clip_gradient(btm1, btm2);
432 if (m_bCheckForNans)
433 check_nan(btm1, btm2);
434 if (m_bAddDebug)
435 add_debug(strMarker + " - eltmul", btm1, btm2, top);
436 };
437 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
438 }
439
440 return top;
441 }
442
451 public Blob<T> scalemul(Blob<T> btm1, Blob<T> btm2, Blob<T> top, int nIdx = 0)
452 {
453 string strMarker = marker;
454 top.ReshapeLike(btm1);
455
456 T fScale = btm2.GetData(nIdx);
457 m_cuda.scale(top.count(), fScale, btm1.gpu_data, top.mutable_gpu_data);
458
459 if (m_bNeedsBackprop)
460 {
461 Action backward = () =>
462 {
463 work.ReshapeLike(btm1);
464 m_cuda.scale(top.count(), fScale, top.gpu_diff, work.mutable_gpu_diff);
465 apply(work, btm1);
466
467 work.ReshapeLike(btm2);
468 float fDot = m_cuda.dot_float(btm1.count(), btm1.gpu_data, top.gpu_diff);
469 work.SetDiff(0);
470 work.SetDiff(fDot, nIdx);
471 apply(work, btm2);
472
473 if (m_bClipGradients)
474 clip_gradient(btm1, btm2);
475 if (m_bCheckForNans)
476 check_nan(btm1, btm2);
477 if (m_bAddDebug)
478 add_debug(strMarker + " - scalemul", btm1, btm2, top);
479 };
480 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
481 }
482
483 return top;
484 }
485
494 public Blob<T> mul(Blob<T> btm1, Blob<T> btm2, Blob<T> top, bool bAccumulateGrad = true)
495 {
496 string strMarker = marker;
497 int nM = btm1.shape(m_nAxis);
498 int nN = btm2.count(m_nAxis + 1);
499 int nK = btm1.count(m_nAxis + 1);
500
501 List<int> rgShape = Utility.Create<int>(m_nAxis, 1);
502 rgShape.Add(nM);
503 rgShape.Add(nN);
504
505 top.Reshape(rgShape);
506
507 m_cuda.gemm(false, false, nM, nN, nK, Blob<T>.One, btm1.gpu_data, btm2.gpu_data, Blob<T>.Zero, top.mutable_gpu_data);
508
509 if (m_bNeedsBackprop)
510 {
511 Action backward = () =>
512 {
513 T fBeta = (bAccumulateGrad) ? Blob<T>.One : Blob<T>.Zero;
514 m_cuda.gemm(false, true, nM, nK, nN, Blob<T>.One, top.gpu_diff, btm2.gpu_data, fBeta, btm1.mutable_gpu_diff);
515 m_cuda.gemm(true, false, nK, nN, nM, Blob<T>.One, btm1.gpu_data, top.gpu_diff, fBeta, btm2.mutable_gpu_diff);
516
517 if (m_bClipGradients)
518 clip_gradient(btm1, btm2);
519 if (m_bCheckForNans)
520 check_nan(btm1, btm2);
521 if (m_bAddDebug)
522 add_debug(strMarker + " - mul", btm1, btm2, top);
523 };
524 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
525 }
526
527 return top;
528 }
529
538 public Blob<T> add(Blob<T> btm1, Blob<T> btm2, Blob<T> top, bool bAccumulateGrad = true)
539 {
540 string strMarker = marker;
541 top.ReshapeLike(btm1);
542
543 m_cuda.add(top.count(), btm1.gpu_data, btm2.gpu_data, top.mutable_gpu_data);
544
545 if (m_bNeedsBackprop)
546 {
547 Action backward = () =>
548 {
549 if (!bAccumulateGrad)
550 {
551 btm1.SetDiff(0);
552 btm2.SetDiff(0);
553 }
554
555 m_cuda.add(btm1.count(), btm1.gpu_diff, top.gpu_diff, btm1.mutable_gpu_diff);
556 m_cuda.add(btm2.count(), btm2.gpu_diff, top.gpu_diff, btm2.mutable_gpu_diff);
557
558 if (m_bClipGradients)
559 clip_gradient(btm1, btm2);
560 if (m_bCheckForNans)
561 check_nan(btm1, btm2);
562 if (m_bAddDebug)
563 add_debug(strMarker + " - add", btm1, btm2, top);
564 };
565 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
566 }
567
568 return top;
569 }
570
577 {
578 if (m_bNeedsBackprop)
579 {
580 Action backward = () =>
581 {
582 b.SetDiff(0);
583 };
584 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
585 }
586
587 return b;
588 }
589
595 {
596 foreach (Blob<T> b in rg)
597 {
598 clear_grad(b);
599 }
600 }
601
609 {
610 string strMarker = marker;
611 top.ReshapeLike(btm);
612
613 int nOuterNum = btm.count(0, m_nAxis);
614 int nInnerNum = btm.count(m_nAxis + 1);
615 int nChannels = top.shape(m_nAxis);
616 int nCount = btm.count();
617
618 work.ReshapeLike(top);
619
620 m_cuda.copy(nCount, btm.gpu_data, top.mutable_gpu_data);
621
622 // We need to subtract the max to avoid numerical issues, compute the exp
623 // and then normalize.
624 // compute max.
625 m_cuda.channel_max(nOuterNum * nInnerNum, nOuterNum, nChannels, nInnerNum, top.gpu_data, work.mutable_gpu_data);
626 // subtract
627 m_cuda.channel_sub(nCount, nOuterNum, nChannels, nInnerNum, work.gpu_data, top.mutable_gpu_data);
628 // exponentiate
629 m_cuda.exp(nCount, top.gpu_data, top.mutable_gpu_data);
630 // Sum after exp
631 m_cuda.channel_sum(nOuterNum * nInnerNum, nOuterNum, nChannels, nInnerNum, top.gpu_data, work.mutable_gpu_data);
632 // divide
633 m_cuda.channel_div(nCount, nOuterNum, nChannels, nInnerNum, work.gpu_data, top.mutable_gpu_data);
634
635 if (m_bNeedsBackprop)
636 {
637 Action backward = () =>
638 {
639 work.ReshapeLike(top);
640 m_cuda.copy(nCount, top.gpu_diff, work.mutable_gpu_diff);
641
642 // Compute inner1d(top_diff, top_data) and subtract them from the bottom diff.
643 m_cuda.channel_dot(nOuterNum * nInnerNum, nOuterNum, nChannels, nInnerNum, top.gpu_diff, top.gpu_data, work.mutable_gpu_data);
644 m_cuda.channel_sub(nCount, nOuterNum, nChannels, nInnerNum, work.gpu_data, work.mutable_gpu_diff);
645
646 // elementwise multiplication
647 m_cuda.mul(nCount, work.gpu_diff, top.gpu_data, work.mutable_gpu_diff);
648 apply(work, btm);
649
650 if (m_bClipGradients)
651 clip_gradient(btm);
652 if (m_bCheckForNans)
653 check_nan(btm);
654 if (m_bAddDebug)
655 add_debug(strMarker + " - softmax", btm, top);
656 };
657 m_rgBackprop.Add(new Tuple<string, Action>(m_strMarker, backward));
658 }
659
660 return top;
661 }
662
666 public int BackwardCount
667 {
668 get { return m_rgBackprop.Count; }
669 }
670
675 public void BackwardOne(int nIdx)
676 {
677 m_rgBackprop[nIdx].Item2();
678 }
679
684 public void Backward(bool bClear = false)
685 {
686 for (int i = m_rgBackprop.Count - 1; i >= 0; i--)
687 {
688 m_rgBackprop[i].Item2();
689 }
690
691 if (bClear)
692 m_rgBackprop.Clear();
693 }
694
698 public void Clear()
699 {
700 m_rgBackprop.Clear();
701 }
702 }
703
708 public class Cache<T> : IDisposable
709 {
710 CudaDnn<T> m_cuda;
711 Blob<T> m_blobCache;
712 int m_nCacheIdx = 0;
713
719 public Cache(CudaDnn<T> cuda, Log log)
720 {
721 m_cuda = cuda;
722 m_blobCache = new Blob<T>(cuda, log, false);
723 m_blobCache.Name = "cache";
724 }
725
729 public void Dispose()
730 {
731 if (m_blobCache != null)
732 {
733 m_blobCache.Dispose();
734 m_blobCache = null;
735 }
736 }
737
743 public void Create(int nCount, List<int> rgItemShape)
744 {
745 List<int> rgShape = new List<int>(rgItemShape);
746 rgShape.Insert(0, nCount);
747 m_blobCache.Reshape(rgShape);
748 }
749
753 public void Reset()
754 {
755 m_nCacheIdx = 0;
756 m_blobCache.SetData(0);
757 }
758
764 public void CopyToCache(Blob<T> b, int nAxis)
765 {
766 int nSpatialDim = b.count(nAxis);
767
768 if (m_nCacheIdx >= m_blobCache.num)
769 throw new Exception("The cache is full!");
770
771 m_cuda.copy(nSpatialDim, b.gpu_data, m_blobCache.mutable_gpu_data, 0, nSpatialDim * m_nCacheIdx);
772 m_nCacheIdx++;
773 }
774
780 public void CopyFromCache(Blob<T> b, int nAxis)
781 {
782 int nSpatialDim = b.count(nAxis);
783
784 m_nCacheIdx--;
785 if (m_nCacheIdx < 0)
786 throw new Exception("The cache is empty!");
787
788 m_cuda.copy(nSpatialDim, m_blobCache.gpu_data, b.mutable_gpu_data, nSpatialDim * m_nCacheIdx, 0);
789 }
790 }
791}
The Log class provides general output in text form.
Definition: Log.cs:13
The Utility class provides general utility funtions.
Definition: Utility.cs:35
static List< int > Create(int nCount, int nStart, int nInc)
Create a new List and fill it with values starting with start and incrementing by inc.
Definition: Utility.cs:721
static double[] ConvertVec(float[] rgf)
Convert an array of float to an array of generics.
Definition: Utility.cs:550
The BlobCollection contains a list of Blobs.
The Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
void SetData(T[] rgData, int nCount=-1, bool bSetCount=true)
Sets a number of items within the Blob's data.
Definition: Blob.cs:1922
long mutable_gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1555
T[] mutable_cpu_diff
Get diff from the GPU and bring it over to the host, or Set diff from the Host and send it over to th...
Definition: Blob.cs:1511
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1487
T[] mutable_cpu_data
Get data from the GPU and bring it over to the host, or Set data from the Host and send it over to th...
Definition: Blob.cs:1461
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
Definition: Blob.cs:442
List< int > shape()
Returns an array where each element contains the shape of an axis of the Blob.
Definition: Blob.cs:684
static T Zero
Returns Zero (0) in type T.
Definition: Blob.cs:260
T GetData(int nIdx)
Returns the data at a given flat index within the Blob.
Definition: Blob.cs:1893
int count()
Returns the total number of items in the Blob.
Definition: Blob.cs:739
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
long gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1541
virtual void Dispose(bool bDisposing)
Releases all resources used by the Blob (including both GPU and Host).
Definition: Blob.cs:402
void SetDiff(double dfVal, int nIdx=-1)
Either sets all of the diff items in the Blob to a given value, or alternatively only sets a single i...
Definition: Blob.cs:1981
int num
DEPRECIATED; legacy shape accessor num: use shape(0) instead.
Definition: Blob.cs:792
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1479
The Cache class is used to cache blobs over time.
void CopyFromCache(Blob< T > b, int nAxis)
Copies a value from the current location in the cache to the blob.
Cache(CudaDnn< T > cuda, Log log)
The constructor.
void Dispose()
Release any resources used.
void Reset()
Resets the cache.
void CopyToCache(Blob< T > b, int nAxis)
Copies a blob to the current location in the cache.
void Create(int nCount, List< int > rgItemShape)
Create the cache memory.
The ComputeGraph class provides a simple computation graph of operations used in a forward pass that ...
Definition: ComputeGraph.cs:22
Blob< T > mul(Blob< T > btm1, Blob< T > btm2, Blob< T > top, bool bAccumulateGrad=true)
'mul' operation performs a blas gemm operation on the 'btm1' matrix with the 'btm2' matrix and places...
void DebugOp(params Blob< T >[] rgB)
DebugOp operation places a debug stub in the backpropagation chain for debugging only.
void CopyToCache(Blob< T > btm, Cache< T > cache)
CopyToCache operation copies the blob into the cache.
Blob< T > clear_grad(Blob< T > b)
'clear_grad' operation only runs on the backward pass and zeros out the gradients on an input.
void PeekItem(Blob< T > btm, Blob< T > top, int ix)
PeekItem operation copies a single item from the bottom to the top.
Blob< T > add(Blob< T > btm1, Blob< T > btm2, Blob< T > top, bool bAccumulateGrad=true)
'elthmul' operation adds each element of the 'btm1' with the 'btm2' and places the results in 'top'.
Dictionary< string, Blob< T >[]> Debug
Returns a dictionary of Blobs used during each operation, only filled when 'bAddDebug' = true in the ...
Definition: ComputeGraph.cs:79
void Clear()
Clears all backward operations from the list.
int BackwardCount
Returns the backward operation count.
Blob< T > sigmoid(Blob< T > btm, Blob< T > top)
'sigmoid' operation runs the sigmoid on each item in the btm and places the results in the top.
Blob< T > CopyToRow(Blob< T > btm, Blob< T > top, int ix, bool bCopyDiff=false)
CopyToRow operation copies the bottom vector into the top matrix.
void clear_grad(BlobCollection< T > rg)
'clear_grad' operation only runs on the backward pass and zeros out the gradients of the inputs.
void Backward(bool bClear=false)
Runs a backward operation on all items starting from the last and running through the first.
string marker
Get/set a string marker added to the debug information and used to indicate where in the code a given...
Definition: ComputeGraph.cs:87
Blob< T > Round(Blob< T > b, int nDecimals=6)
Round operation, rounds the values to the nearest specified decimal.
Blob< T > tanh(Blob< T > btm, Blob< T > top)
'tanh' operation runs the tanh on each item in the btm and places the results in the top.
Blob< T > eltmul(Blob< T > btm1, Blob< T > btm2, Blob< T > top)
'elthmul' operation mutliplies each element of the 'btm1' with the 'btm2' and places the results in '...
Blob< T > softmax(Blob< T > btm, Blob< T > top)
'softmax' operation runs the softmax on each item in the btm and places the results in the top.
ComputeGraph(CudaDnn< T > cuda, Log log, int nAxis, bool bNeedsBackprop=true, bool bClipGradients=false, bool bCheckNans=false, bool bAddDebug=false)
The constructor.
Definition: ComputeGraph.cs:45
void BackwardOne(int nIdx)
Runs a backward operation at a given index.
Blob< T > scalemul(Blob< T > btm1, Blob< T > btm2, Blob< T > top, int nIdx=0)
'scalemul' operation mutliplies each element of the 'btm1' with the first item within 'btm2' and plac...
void Dispose()
Release all resources used.
Definition: ComputeGraph.cs:60
bool needs_backprop
Get/set whether or not to back propagate.
Definition: ComputeGraph.cs:96
int axis
Returns the axis on which all operations are performed.
Blob< T > PeekRow(Blob< T > btm, Blob< T > top, int ix)
PeeKRow operation copies data and diffs from one row from within the bottom matrix and places it in t...
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
void copy(int nCount, long hSrc, long hDst, int nSrcOffset=0, int nDstOffset=0, long hStream=-1, bool? bSrcHalfSizeOverride=null, bool? bDstHalfSizeOverride=null)
Copy data from one block of GPU memory to another.
Definition: CudaDnn.cs:6007
void channel_sub(int nCount, int nOuterNum, int nChannels, int nInnerNum, long hA, long hX, long hY)
Subtracts the values across the channels of X from A and places the result in Y.
Definition: CudaDnn.cs:8197
void tanh_fwd(int nCount, long hBottomData, long hTopData)
Performs a TanH forward pass in Cuda.
Definition: CudaDnn.cs:9286
void channel_dot(int nCount, int nOuterNum, int nChannels, int nInnerNum, long hX, long hA, long hY)
Calculates the dot product the the values within each channel of X and places the result in Y.
Definition: CudaDnn.cs:8326
void add(int n, long hA, long hB, long hC, long hY)
Adds A, B and C and places the result in Y.
Definition: CudaDnn.cs:7209
void channel_max(int nCount, int nOuterNum, int nChannels, int nInnerNum, long hX, long hY, bool bReturnIdx=false)
Calculates the maximum value within each channel of X and places the result in Y.
Definition: CudaDnn.cs:8099
void sigmoid_fwd(int nCount, long hBottomData, long hTopData)
Performs a Sigmoid forward pass in Cuda.
Definition: CudaDnn.cs:9323
void gemm(bool bTransA, bool bTransB, int m, int n, int k, double fAlpha, long hA, long hB, double fBeta, long hC)
Perform a matrix-matrix multiplication operation: C = alpha transB (B) transA (A) + beta C
Definition: CudaDnn.cs:6236
void scale(int n, double fAlpha, long hX, long hY)
Scales the values in X and places them in Y.
Definition: CudaDnn.cs:6925
void exp(int n, long hA, long hY)
Calculates the exponent value of A and places the result in Y.
Definition: CudaDnn.cs:7454
void channel_sum(int nCount, int nOuterNum, int nChannels, int nInnerNum, long hX, long hY, bool bSumAcrossChannels=true, DIR dir=DIR.FWD, int nChannelsY=-1)
Calculates the sum the the values either across or within each channel (depending on bSumAcrossChanne...
Definition: CudaDnn.cs:8236
float dot_float(int n, long hX, long hY)
Computes the dot product of X and Y.
Definition: CudaDnn.cs:6830
void mul(int n, long hA, long hB, long hY, int nAOff=0, int nBOff=0, int nYOff=0)
Multiplies each element of A with each element of B and places the result in Y.
Definition: CudaDnn.cs:7334
void channel_div(int nCount, int nOuterNum, int nChannels, int nInnerNum, long hX, long hY, int nMethod=1)
Divides the values of the channels from X and places the result in Y.
Definition: CudaDnn.cs:8254
void sigmoid_bwd(int nCount, long hTopDiff, long hTopData, long hBottomDiff)
Performs a Sigmoid backward pass in Cuda.
Definition: CudaDnn.cs:9341
void tanh_bwd(int nCount, long hTopDiff, long hTopData, long hBottomDiff)
Performs a TanH backward pass in Cuda.
Definition: CudaDnn.cs:9304
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 namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12