MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
ScalarLayer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using MyCaffe.basecode;
6using MyCaffe.common;
7using MyCaffe.layers;
8using MyCaffe.param;
9using MyCaffe.param.nt;
10
11namespace MyCaffe.layers.nt
12{
20 public class ScalarLayer<T> : NeuronLayer<T>
21 {
29 : base(cuda, log, p)
30 {
32 }
33
39 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
40 {
41 base.LayerSetUp(colBottom, colTop);
42
43 // Setup the convert to half flags used by the Layer just before calling forward and backward.
45 }
46
56 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
57 {
58 int nCount = colTop[0].count();
59 long hBottomData = colBottom[0].gpu_data;
60 long hTopData = colTop[0].mutable_gpu_data;
61
62 m_cuda.copy(nCount, hBottomData, hTopData);
63
65 {
67 if (m_param.scalar_param.value != 1.0)
68 m_cuda.mul_scalar(nCount, m_param.scalar_param.value, hTopData);
69 break;
70
72 if (m_param.scalar_param.value != 0.0)
73 m_cuda.add_scalar(nCount, m_param.scalar_param.value, hTopData);
74 break;
75
76 default:
77 throw new Exception("Unknown scalar operation '" + m_param.scalar_param.operation.ToString());
78 }
79 }
80
96 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
97 {
98 if (!rgbPropagateDown[0])
99 return;
100
101 int nCount = colTop[0].count();
102 long hTopDiff = colTop[0].gpu_diff;
103 long hBottomDiff = colBottom[0].mutable_gpu_diff;
104
105 m_cuda.copy(nCount, hTopDiff, hBottomDiff);
106
108 {
110 {
111 case ScalarParameter.ScalarOp.MUL:
112 m_cuda.mul_scalar(nCount, 1.0 / m_param.scalar_param.value, hBottomDiff);
113 break;
114
115 case ScalarParameter.ScalarOp.ADD:
116 m_cuda.add_scalar(nCount, -m_param.scalar_param.value, hBottomDiff);
117 break;
118
119 default:
120 throw new Exception("Unknown scalar operation '" + m_param.scalar_param.operation.ToString());
121 }
122 }
123 }
124 }
125}
The Log class provides general output in text form.
Definition: Log.cs:13
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
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
Definition: Layer.cs:47
bool m_bUseHalfSize
Specifies that the half size of the top (if any) should be converted to the base size.
Definition: Layer.cs:84
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 ScalarLayer computes the operation with the value on the input.
Definition: ScalarLayer.cs:21
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Reverses the previous scalar operation.
Definition: ScalarLayer.cs:96
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Computes
Definition: ScalarLayer.cs:56
ScalarLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The ScalarLayer constructor.
Definition: ScalarLayer.cs:28
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
Definition: ScalarLayer.cs:39
Specifies the base parameter for all layers.
bool use_halfsize
Specifies whether or not to use half sized memory or not.
ScalarParameter scalar_param
Returns the parameter set when initialized with LayerType.SCALAR
LayerType
Specifies the layer type.
Specifies the parameters for the ScalarLayer
double value
Specifies the scalar value to apply.
ScalarOp operation
Specifies the scalar operation to apply (mul, add, etc).
bool passthrough_gradient
Specifies whether or not to pass-through the gradient without performing the back-prop calculation (d...
ScalarOp
Defines the scalar operations that may be performed.
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.nt namespace contains all Neural Transfer related layers.
Definition: LayerFactory.cs:19
The MyCaffe.layers namespace contains all layers that have a solidified code base,...
Definition: LayerFactory.cs:15
The MyCaffe.param.nt namespace defines the parameters used by the Nerual Style Transfer layers.
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