MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ExpLayer.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{
21 public class ExpLayer<T> : NeuronLayer<T>
22 {
23 double m_dfInnerScale;
24 double m_dfOuterScale;
25
40 : base(cuda, log, p)
41 {
43 }
44
50 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
51 {
52 base.LayerSetUp(colBottom, colTop);
53
54 double dfBase = m_param.exp_param.base_val;
55
56 if (dfBase != -1)
57 m_log.CHECK_GT(dfBase, 0, "base_val must be strictly positive.");
58
59 // If base == -1, interpret the base as e and set log_base = 1 exactly.
60 // Otehrwise, calculate its log explicitly.
61 double dfLogBase = (dfBase == -1) ? 1 : Math.Log(dfBase);
62
63 m_log.CHECK(!double.IsNaN(dfLogBase), "NaN result: log(base) == log(" + dfBase.ToString() + ") = " + dfLogBase.ToString());
64 m_log.CHECK(!double.IsInfinity(dfLogBase), "Inf result: log(base) == log(" + dfBase.ToString() + ") = " + dfLogBase.ToString());
65
66 double dfInputScale = m_param.exp_param.scale;
67 double dfInputShift = m_param.exp_param.shift;
68
69 m_dfInnerScale = dfLogBase * dfInputScale;
70 m_dfOuterScale = (dfInputShift == 0) ? 1 : ((dfBase != -1) ? Math.Pow(dfBase, dfInputShift) : Math.Exp(dfInputShift));
71 }
72
82 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
83 {
84 int nCount = colBottom[0].count();
85 long hBottomData = colBottom[0].gpu_data;
86 long hTopData = colTop[0].mutable_gpu_data;
87
88 if (m_dfInnerScale == 1)
89 {
90 m_cuda.exp(nCount, hBottomData, hTopData);
91 }
92 else
93 {
94 m_cuda.scale(nCount, convert(m_dfInnerScale), hBottomData, hTopData);
95 m_cuda.exp(nCount, hTopData, hTopData);
96 }
97
98 if (m_dfOuterScale != 1)
99 m_cuda.scal(nCount, convert(m_dfOuterScale), hTopData);
100 }
101
113 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
114 {
115 if (!rgbPropagateDown[0])
116 return;
117
118 int nCount = colBottom[0].count();
119 long hTopData = colTop[0].gpu_data;
120 long hTopDiff = colTop[0].gpu_diff;
121 long hBottomDiff = colBottom[0].mutable_gpu_diff;
122
123 m_cuda.mul(nCount, hTopData, hTopDiff, hBottomDiff);
124
125 if (m_dfInnerScale != 1)
126 m_cuda.scal(nCount, convert(m_dfInnerScale), hBottomDiff);
127 }
128 }
129}
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_GT(double df1, double df2, string str)
Test whether one number is greater than another.
Definition: Log.cs:299
The BlobCollection contains a list of Blobs.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
The ExpLayer which computes the exponential of the input. This layer is initialized with the MyCaffe....
Definition: ExpLayer.cs:22
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: ExpLayer.cs:50
ExpLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The ExpLayer constructor.
Definition: ExpLayer.cs:39
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the EXP value inputs.
Definition: ExpLayer.cs:113
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation
Definition: ExpLayer.cs:82
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
void convert(BlobCollection< T > col)
Convert a collection of blobs from / to half size.
Definition: Layer.cs:535
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
The NeuronLayer is an interface for layers that take one blob as input (x) and produce only equally-s...
Definition: NeuronLayer.cs:22
double base_val
Specifies the base to use for the exponent, where , for base > 0.
Definition: ExpParameter.cs:41
double shift
Specifies the shift to use for the exponent, where , for base > 0.
Definition: ExpParameter.cs:61
double scale
Specifies the scale to use for the exponent, where , for base > 0.
Definition: ExpParameter.cs:51
Specifies the base parameter for all layers.
ExpParameter exp_param
Returns the parameter set when initialized with LayerType.EXP
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