MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
CategoricalTransformationLayer.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.tft
10{
23 {
24 List<Layer<T>> m_rgEmbLayers = new List<Layer<T>>();
26 BlobCollection<T> m_rgEmbBtm = new BlobCollection<T>();
27 BlobCollection<T> m_rgEmbTop = new BlobCollection<T>();
28
36 : base(cuda, log, p)
37 {
38 m_type = LayerParameter.LayerType.CATEGORICAL_TRANS;
39 }
40
42 protected override void dispose()
43 {
44 }
45
47 protected override void setup_internal_blobs(BlobCollection<T> col)
48 {
49 if (col.Count > 0)
50 return;
51 }
52
56 public override int ExactNumBottomBlobs
57 {
58 get { return 1; }
59 }
60
64 public override int ExactNumTopBlobs
65 {
66 get { return (int)m_param.categorical_trans_param.num_input; }
67 }
68
74 public override void LayerSetUp(BlobCollection<T> colBottom, BlobCollection<T> colTop)
75 {
76 int nOffset = (colBottom[0].num_axes == 2) ? 1 : 2;
77 int nDim = colBottom[0].count(0, nOffset);
78 int nNumInput = m_param.categorical_trans_param.cardinalities.Count;
79 int nCount = colBottom[0].count(nOffset);
80 int nSpatialDim = nCount / nNumInput;
81 List<int> rgShape = new List<int>() { nDim, nSpatialDim };
82 Blob<T> blobBtm = null;
83
84 m_log.CHECK_EQ(m_param.categorical_trans_param.num_input, m_param.categorical_trans_param.cardinalities.Count, "The num_input must match the number of cardinalities!");
85
86 m_rgEmbBtm.Clear();
87 m_rgEmbBtm.Add(blobBtm);
88 m_rgEmbTop.Clear();
89 m_rgEmbTop.Add(colTop[0]);
90
91 for (int i = 0; i < nNumInput; i++)
92 {
93 blobBtm = new Blob<T>(m_cuda, m_log);
94 blobBtm.Reshape(rgShape);
95 m_rgBtm.Add(blobBtm);
96
97 m_rgEmbBtm[0] = m_rgBtm[i];
98 m_rgEmbTop[0] = colTop[i];
99
100 int nCardinality = m_param.categorical_trans_param.cardinalities[i];
101 LayerParameter p = new LayerParameter(LayerParameter.LayerType.EMBED, m_param.name + ".emb" + i.ToString());
103 p.embed_param.input_dim = (uint)nCardinality;
104 p.embed_param.bias_term = false;
105
107 m_rgEmbLayers.Add(emb_layer);
108
109 emb_layer.LayerSetUp(m_rgEmbBtm, m_rgEmbTop);
110 blobs.Add(emb_layer.blobs);
111 }
112 }
113
119 public override void Reshape(BlobCollection<T> colBottom, BlobCollection<T> colTop)
120 {
121 int nNumInput = m_param.categorical_trans_param.cardinalities.Count;
122 for (int i = 0; i < nNumInput; i++)
123 {
124 m_rgEmbBtm[0] = m_rgBtm[i];
125 m_rgEmbTop[0] = colTop[i];
126 m_rgEmbLayers[i].Reshape(m_rgEmbBtm, m_rgEmbTop);
127 }
128 }
129
141 protected override void forward(BlobCollection<T> colBottom, BlobCollection<T> colTop)
142 {
143 int nNumInput = m_param.categorical_trans_param.cardinalities.Count;
144 for (int i = 0; i < nNumInput; i++)
145 {
146 int nCount = m_rgBtm[i].count();
147 m_cuda.channel_copy(nCount, nCount, 1, nNumInput, 1, i, colBottom[0].gpu_data, m_rgBtm[i].mutable_gpu_data, DIR.FWD);
148
149 m_rgEmbBtm[0] = m_rgBtm[i];
150 m_rgEmbTop[0] = colTop[i];
151 m_rgEmbLayers[i].Forward(m_rgEmbBtm, m_rgEmbTop);
152 }
153 }
154
170 protected override void backward(BlobCollection<T> colTop, List<bool> rgbPropagateDown, BlobCollection<T> colBottom)
171 {
172 int nNumInput = m_param.categorical_trans_param.cardinalities.Count;
173 for (int i = 0; i < nNumInput; i++)
174 {
175 m_rgEmbBtm[0] = m_rgBtm[i];
176 m_rgEmbTop[0] = colTop[i];
177 m_rgEmbLayers[i].Backward(m_rgEmbTop, rgbPropagateDown, m_rgEmbBtm);
178
179 // data fields do not have gradients so no gradients are output.
180 //int nCount = m_rgEmbBtm[0].count();
181 //m_cuda.channel_copy(nCount, nCount, 1, nNumInput, 1, i, colBottom[0].mutable_gpu_diff, m_rgEmbBtm[0].gpu_diff, DIR.BWD);
182 }
183 }
184 }
185}
The Log class provides general output in text form.
Definition: Log.cs:13
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Definition: Log.cs:239
The BlobCollection contains a list of Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
int Count
Returns the number of items in the collection.
void Clear(bool bDispose=false)
Remove all items from the collection.
void Reshape(int[] rgShape)
Reshapes all blobs in the collection to the given shape.
The Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
Definition: Blob.cs:442
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
An interface for the units of computation which can be composed into a Net.
Definition: Layer.cs:31
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
abstract void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Performs Layer specific setup. Derived layers should override this function as well as the Reshape fu...
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
Definition: Layer.cs:39
static Layer< T > Create(CudaDnn< T > cuda, Log log, LayerParameter p, CancelEvent evtCancel, IXDatabaseBase db=null, TransferInput trxinput=null)
Create a new Layer based on the LayerParameter.
Definition: Layer.cs:1468
LayerParameter.LayerType m_type
Specifies the Layer type.
Definition: Layer.cs:35
BlobCollection< T > blobs
Returns the collection of learnable parameter Blobs for the Layer.
Definition: Layer.cs:875
LayerParameter convertLayerParam(LayerParameter pChild, LayerParameter pParent)
Called to convert a parent LayerParameterEx, used in blob sharing, with a child layer parameter.
Definition: Layer.cs:1134
The CategoricalTransformationLayer implements the transforming/embeddings for the set of categorical ...
override int ExactNumBottomBlobs
Returns the exact number of required bottom (input) Blobs: data
CategoricalTransformationLayer(CudaDnn< T > cuda, Log log, LayerParameter p)
The constructor.
override void setup_internal_blobs(BlobCollection< T > col)
Derivative layers should add all internal blobws to the 'col' provided.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the bottom (input) and top (output) blobs.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Computes the error gradient w.r.t. the cardinality value inputs.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Forward computation
override int ExactNumTopBlobs
Returns the exact number of required top (output) Blobs: norm
override void dispose()
Releases all GPU and host resources used by the Layer.
uint num_output
Specifies the number of outputs for the layer.
uint input_dim
Specifies the input given as integers to be interpreted as one-hot vector indices with dimension num_...
bool bias_term
Specifies whether to use a bias term or not.
Specifies the base parameter for all layers.
string name
Specifies the name of this LayerParameter.
CategoricalTransformationParameter categorical_trans_param
Returns the parameter set when initialized with LayerType.CATEGORICAL_TRANS
EmbedParameter embed_param
Returns the parameter set when initialized with LayerType.EMBED
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
DIR
Defines the direction of data flow.
Definition: CudaDnn.cs:22
The MyCaffe.layers.tft namespace contains all TFT related layers.
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