MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
SwishLayer.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 SwishLayer<T> : NeuronLayer<T>
22 {
23 SigmoidLayer<T> m_sigmoidLayer;
24 Blob<T> m_blobSigmoidInput;
25 Blob<T> m_blobSigmoidOutput;
26 BlobCollection<T> m_colSigmoidBottom = new BlobCollection<T>();
27 BlobCollection<T> m_colSigmoidTop = new BlobCollection<T>();
28
39 : base(cuda, log, p)
40 {
42
43 m_blobSigmoidInput = new Blob<T>(cuda, log);
44 m_blobSigmoidInput.Name = m_param.name + " sigmoid in";
45 m_blobSigmoidOutput = new Blob<T>(cuda, log);
46 m_blobSigmoidOutput.Name = m_param.name + " sigmoid out";
47
48 LayerParameter sigmoidParam = new LayerParameter(LayerParameter.LayerType.SIGMOID);
49 sigmoidParam.sigmoid_param.engine = p.swish_param.engine;
50
51 m_sigmoidLayer = new SigmoidLayer<T>(cuda, log, sigmoidParam);
52 }
53
57 protected override void dispose()
58 {
59 m_blobSigmoidOutput.Dispose();
60 m_blobSigmoidInput.Dispose();
61
62 base.dispose();
63 }
64
70 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
71 {
72 base.LayerSetUp(colBottom, colTop);
73
74 m_colSigmoidBottom.Clear();
75 m_colSigmoidBottom.Add(m_blobSigmoidInput);
76 m_colSigmoidTop.Clear();
77 m_colSigmoidTop.Add(m_blobSigmoidOutput);
78 m_sigmoidLayer.LayerSetUp(m_colSigmoidBottom, m_colSigmoidTop);
79 }
80
86 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
87 {
88 base.Reshape(colBottom, colTop);
89
90 m_blobSigmoidInput.ReshapeLike(colBottom[0]);
91 m_sigmoidLayer.Reshape(m_colSigmoidBottom, m_colSigmoidTop);
92 }
93
107 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
108 {
109 long hBottomData = colBottom[0].gpu_data;
110 long hSigmoidInputData = m_blobSigmoidInput.mutable_gpu_data;
111 long hTopData = colTop[0].mutable_gpu_data;
112 int nCount = colBottom[0].count();
113 double dfBeta = m_param.swish_param.beta;
114
115 m_cuda.copy(nCount, hBottomData, hSigmoidInputData);
116 m_cuda.scal(nCount, dfBeta, hSigmoidInputData);
117 m_sigmoidLayer.Forward(m_colSigmoidBottom, m_colSigmoidTop);
118 m_cuda.mul(nCount, hBottomData, m_blobSigmoidOutput.gpu_data, hTopData);
119 }
120
140 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
141 {
142 if (rgbPropagateDown[0])
143 {
144 long hTopData = colTop[0].gpu_data;
145 long hTopDiff = colTop[0].gpu_diff;
146 long hSigmoidOutputData = m_blobSigmoidOutput.gpu_data;
147 long hBottomDiff = colBottom[0].mutable_gpu_diff;
148 int nCount = colBottom[0].count();
149 double dfBeta = m_param.swish_param.beta;
150
151 m_cuda.swish_bwd(nCount, hTopDiff, hTopData, hSigmoidOutputData, hBottomDiff, dfBeta);
152 }
153 }
154 }
155}
The Log class provides general output in text form.
Definition: Log.cs:13
The BlobCollection contains a list of Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
void Clear(bool bDispose=false)
Remove all items from the collection.
The Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
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
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
The SigmoidLayer is a neuron layer that calculates the sigmoid function, a classc choice for neural n...
Definition: SigmoidLayer.cs:28
The SwishLayer provides a novel activation function that tends to work better than ReLU....
Definition: SwishLayer.cs:22
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation
Definition: SwishLayer.cs:107
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the Swish value inputs.
Definition: SwishLayer.cs:140
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the layer.
Definition: SwishLayer.cs:86
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: SwishLayer.cs:70
SwishLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The SwishLayer constructor.
Definition: SwishLayer.cs:38
override void dispose()
Release all resources used.
Definition: SwishLayer.cs:57
Engine engine
Specifies the Engine in use.
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
SigmoidParameter sigmoid_param
Returns the parameter set when initialized with LayerType.SIGMOID
LayerType
Specifies the layer type.
SwishParameter swish_param
Returns the parameter set when initialized with LayerType.SWISH
double beta
Specifies the beta value for the Swish activation function.
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