-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConvolutionalTriangularLayer.cu
76 lines (75 loc) · 3.45 KB
/
ConvolutionalTriangularLayer.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "ConvolutionalTriangularLayer.h"
#include "ConvolutionalLayer.h"
#include <iostream>
#include <cassert>
#include "utilities.h"
#include "Regions.h"
ConvolutionalTriangularLayer::ConvolutionalTriangularLayer(
cudaMemStream &memStream, int filterSize, int filterStride, int dimension,
int nFeaturesIn, int minActiveInputs)
: SpatiallySparseLayer(memStream), filterSize(filterSize),
filterStride(filterStride), dimension(dimension),
nFeaturesIn(nFeaturesIn), minActiveInputs(minActiveInputs) {
fs = triangleSize(filterSize, dimension);
nFeaturesOut = fs * nFeaturesIn;
std::cout << dimension
<< "D ConvolutionalTriangularLayer side-length=" << filterSize
<< " " << nFeaturesIn << "x" << fs << "->" << nFeaturesOut;
if (filterStride > 1)
std::cout << ", stride " << filterStride;
std::cout << std::endl;
}
void ConvolutionalTriangularLayer::preprocess(
SpatiallySparseBatch &batch, SpatiallySparseBatchInterface &input,
SpatiallySparseBatchInterface &output) {
assert(input.nFeatures == nFeaturesIn);
assert(input.spatialSize >= filterSize);
assert((input.spatialSize - filterSize) % filterStride == 0);
output.nFeatures = nFeaturesOut;
output.spatialSize = (input.spatialSize - filterSize) / filterStride + 1;
output.nSpatialSites = 0;
output.grids.resize(batch.batchSize);
output.backpropErrors = input.backpropErrors;
RegularTriangularRegions regions(inSpatialSize, outSpatialSize, dimension,
filterSize, filterStride);
for (int item = 0; item < batch.batchSize; item++)
gridRules(input.grids[item], output.grids[item], regions,
output.nSpatialSites, output.rules.hVector(), minActiveInputs);
output.featuresPresent.resize(input.featuresPresent.size() * fs);
convolutionFeaturesPresent(input.featuresPresent.hVector(),
output.featuresPresent.hVector(), input.nFeatures,
input.featuresPresent.size(), fs);
}
void ConvolutionalTriangularLayer::forwards(
SpatiallySparseBatch &batch, SpatiallySparseBatchInterface &input,
SpatiallySparseBatchInterface &output) {
output.sub->features.resize(output.nSpatialSites *
output.featuresPresent.size());
propForwardToMatrixMultiply(input.sub->features.dPtr(),
output.sub->features.dPtr(), output.rules.dPtr(),
output.nSpatialSites * fs,
input.featuresPresent.size(), memStream);
}
void ConvolutionalTriangularLayer::backwards(
SpatiallySparseBatch &batch, SpatiallySparseBatchInterface &input,
SpatiallySparseBatchInterface &output, float learningRate, float momentum) {
if (input.backpropErrors) {
input.sub->dfeatures.resize(input.nSpatialSites *
input.featuresPresent.size());
input.sub->dfeatures.setZero(memStream);
propBackwardFromMatrixMultiply(
input.sub->dfeatures.dPtr(), output.sub->dfeatures.dPtr(),
output.rules.dPtr(), output.nSpatialSites * fs,
input.featuresPresent.size(), memStream);
}
}
int ConvolutionalTriangularLayer::calculateInputSpatialSize(
int outputSpatialSize) {
outSpatialSize = outputSpatialSize;
inSpatialSize = filterSize + (outputSpatialSize - 1) * filterStride;
std::cout << "-(C" << filterSize;
if (filterStride != 1)
std::cout << "/" << filterStride;
std::cout << ")-" << inSpatialSize;
return inSpatialSize;
}