MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
GradientScaleLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using System.Text;
6using MyCaffe.basecode;
7using MyCaffe.common;
8using MyCaffe.param;
9
10namespace MyCaffe.layers
11{
33 public class GradientScaleLayer<T> : NeuronLayer<T>
34 {
35 double m_dfLowerBound;
36 double m_dfUpperBound;
37 double m_dfAlpha;
38 double m_dfMaxIter;
39 double m_dfCoeff;
40 Stopwatch m_swOutput = new Stopwatch();
41
58 : base(cuda, log, p)
59 {
60 m_type = LayerParameter.LayerType.GRADIENTSCALER;
61 }
62
68 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
69 {
70 base.LayerSetUp(colBottom, colTop);
71
73 if (args != null && args.CurrentPhase != Phase.TRAIN)
74 return;
75
76 m_log.CHECK(args != null, "WARNING: The OnGetIteration event is not connected!");
77
82 m_dfCoeff = 1.0; // default adaptation coefficient.
83
84 m_log.CHECK_LE(m_dfLowerBound, m_dfUpperBound, "The lower bound must be <= the upper bound.");
85 m_log.CHECK_GE(m_dfAlpha, 0, "The alpha value must be >= 0.0");
86 m_log.CHECK_GE(m_dfCoeff, 1, "The max_iter must be >= 1.0");
87
88 int nIteration = (args == null) ? 1 : args.Iteration;
89 double dfProgress = Math.Min(1.0, (double)nIteration / m_dfMaxIter);
90 double dfHeight = m_dfUpperBound - m_dfLowerBound;
91
92 m_dfCoeff = 2.0 * dfHeight / (1.0 + Math.Exp(-m_dfAlpha * dfProgress)) - dfHeight + m_dfLowerBound;
93 m_log.WriteLine("iter = " + nIteration.ToString() + " progress = " + dfProgress.ToString() + " coeff = " + m_dfCoeff.ToString());
94 m_swOutput.Start();
95 }
96
104 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
105 {
106 colTop[0].ShareData(colBottom[0]);
107 }
108
118 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
119 {
120 if (!rgbPropagateDown[0])
121 return;
122
124 if (args != null && args.CurrentPhase != Phase.TRAIN)
125 return;
126
127 int nCount = colTop[0].count();
128 long hTopDiff = colTop[0].gpu_diff;
129 long hBottomDiff = colBottom[0].mutable_gpu_diff;
130
131 int nIteration = (args == null) ? 1 : args.Iteration;
132 double dfProgress = Math.Min(1.0, (double)nIteration / m_dfMaxIter);
133 double dfHeight = m_dfUpperBound - m_dfLowerBound;
134
135 m_dfCoeff = 2.0 * dfHeight / (1.0 + Math.Exp(-m_dfAlpha * dfProgress)) - dfHeight + m_dfLowerBound;
136
137 if (m_swOutput.Elapsed.TotalMilliseconds > 1000)
138 {
139 m_log.WriteLine("iter = " + nIteration.ToString() + " progress = " + dfProgress.ToString() + " coeff = " + m_dfCoeff.ToString());
140 m_swOutput.Restart();
141 }
142
143 m_cuda.scale(nCount, -m_dfCoeff, hTopDiff, hBottomDiff);
144 }
145 }
146}
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 WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
Definition: Log.cs:80
void CHECK_LE(double df1, double df2, string str)
Test whether one number is less than or equal to another.
Definition: Log.cs:263
void CHECK_GE(double df1, double df2, string str)
Test whether one number is greater than or equal to another.
Definition: Log.cs:287
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 GetIterationArgs is sent bubbled up to the solver when a layer needs to know the curret training ...
Definition: EventArgs.cs:748
int Iteration
Returns the iteration.
Definition: EventArgs.cs:774
Phase CurrentPhase
Returns the phase.
Definition: EventArgs.cs:782
The GradientScaleLayer which scales the deltas during the backpropagation. This layer is initialized ...
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
GradientScaleLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The GradientScaleLayer constructor.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Scales the error gradient w.r.t. the GRADIENTSCALER value inputs.
Log m_log
Specifies the Log for output.
Definition: Layer.cs:43
GetIterationArgs getCurrentIteration()
Fires the OnGetIteration event to query the current iteration.
Definition: Layer.cs:398
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
The NeuronLayer is an interface for layers that take one blob as input (x) and produce only equally-s...
Definition: NeuronLayer.cs:22
double lower_bound
Specifies the lower bound of the height used for scaling.
double alpha
Specifies the alpha value applied to the current iter/max_iter, used when scaling.
double max_iter
Specifies the maximum iteration used when scaling.
double upper_bound
Specifies the upper bound of the height used for scaling.
Specifies the base parameter for all layers.
GradientScaleParameter gradient_scale_param
Returns the parameter set when initialized with LayerType.GSL
LayerType
Specifies the layer type.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
Phase
Defines the Phase under which to run a Net.
Definition: Interfaces.cs:61
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