本文整理汇总了C++中LayerDimensions类的典型用法代码示例。如果您正苦于以下问题:C++ LayerDimensions类的具体用法?C++ LayerDimensions怎么用?C++ LayerDimensions使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LayerDimensions类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TEST
TEST( testforward, compare_1_n_biased_pad ) {
EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu();
int maxWorkgroupSize = cl->getMaxWorkgroupSize();
delete cl;
LayerDimensions dim;
int batchSize = 4;
int N = 4;
string activationName = "tanh";
dim.setInputPlanes( 8 ).setInputSize(19).setNumFilters( 8 )
.setFilterSize( 5 )
.setPadZeros( true ).setBiased( true );
for( int instance = 2; instance <= 7; instance++ ) {
if( instance == 5 ) {
continue; // forwardfc, cant use for inputimagesize != filtersize
}
dim.setInputSize(19);
if(instance == 2 && maxWorkgroupSize < 19 * 19) {
dim.setInputSize(15);
}
if(instance == 3 && maxWorkgroupSize < 19 * 19) {
dim.setInputSize(15);
}
cout << "instance: " << instance << endl;
compareSpecific( false, N, batchSize, dim, 1, instance );
}
}
示例2: TEST
TEST( SLOW_testpropagate, perf_kgsgo_fc500 ) {
int batchSize = 128;
LayerDimensions dim;
dim.setInputPlanes( 32 ).setInputImageSize(19).setNumFilters( 500 ).setFilterSize( 19 )
.setPadZeros( false ).setBiased( true );
testPerf( -1, 128, batchSize, dim, new TanhActivation() );
}
示例3: TEST
TEST(SLOW_testbackward, perf_kgsgo_32c5) {
int batchSize = 128;
LayerDimensions dim;
dim.setInputPlanes(32).setInputSize(19).setNumFilters(32).setFilterSize(5)
.setPadZeros(true).setBiased(true);
cout << dim.buildOptionsString() << endl;
// ActivationFunction *fn = new ReluActivation();
measurePerf(2, batchSize, dim);
}
示例4: BackpropWeights
BackpropWeightsScratchBias::BackpropWeightsScratchBias( OpenCLHelper *cl, LayerDimensions dim, ActivationFunction const *fn ) :
BackpropWeights( cl, dim, fn )
{
// [[[cog
// import stringify
// # stringify.write_kernel( "kernelSource", "ClConvolve.cl")
// ]]]
// [[[end]]]
std::string options = dim.buildOptionsString();
options += " -D " + fn->getDefineName();
kernel = cl->buildKernel( "backpropweights.cl", "backprop_floats_withscratch_dobias", options );
// kernel = cl->buildKernelFromString( kernelSource, "calcErrorsForUpstream", options );
}
示例5: testPerf
void testPerf( int instance, int N, int batchSize, LayerDimensions dim ) {
cout << dim.buildOptionsString() << endl;
int inputsSize = batchSize * dim.inputCubeSize;
int filtersSize = dim.filtersSize;
int biasSize = dim.numFilters;
int inputsAllocated = std::max( inputsSize, 10000 );
int filtersAllocated = std::max( filtersSize, 10000 );
int biasFiltersAllocated = std::max( biasSize, 10000 );
float *inputs = new float[ inputsAllocated ];
float *filters = new float[ filtersAllocated ];
float *biasFilters = new float[ biasFiltersAllocated ];
memset( inputs, 0, sizeof(float) * inputsAllocated );
memset( filters, 0, sizeof(float) * filtersAllocated );
memset( biasFilters, 0, sizeof(float) * biasFiltersAllocated );
WeightRandomizer::randomize( inputs, inputsAllocated, -0.1f, 0.1f );
WeightRandomizer::randomize( filters, filtersAllocated, -0.1f, 0.1f );
WeightRandomizer::randomize( biasFilters, biasFiltersAllocated, -0.1f, 0.1f );
EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu();
Forward *p1 = Forward::instanceSpecific( instance, cl, dim );
for( int it = 0; it < (N + batchSize - 1 ) / batchSize; it++ ) {
int thisBatchSize = it < N - 1 ? batchSize : N - batchSize * it;
float *output1 = new float[p1->getOutputTotalSize(thisBatchSize)];
p1->forward( thisBatchSize, inputs, filters, biasFilters, output1 );
delete[] output1;
}
StatefulTimer::dump(true);
delete p1;
delete cl;
delete[] inputs;
delete[] filters;
delete[] biasFilters;
}
示例6: testPerf
void testPerf( int instance, int N, int batchSize, LayerDimensions dim, ActivationFunction *fn ) {
cout << dim.buildOptionsString() << endl;
int inputsSize = batchSize * dim.inputCubeSize;
int filtersSize = dim.filtersSize;
int biasSize = dim.numFilters;
int inputsAllocated = std::max( inputsSize, 10000 );
int filtersAllocated = std::max( filtersSize, 10000 );
int biasFiltersAllocated = std::max( biasSize, 10000 );
float *inputs = new float[ inputsAllocated ];
float *filters = new float[ filtersAllocated ];
float *biasFilters = new float[ biasFiltersAllocated ];
memset( inputs, 0, sizeof(float) * inputsAllocated );
memset( filters, 0, sizeof(float) * filtersAllocated );
memset( biasFilters, 0, sizeof(float) * biasFiltersAllocated );
WeightRandomizer::randomize( inputs, inputsAllocated, -0.1f, 0.1f );
WeightRandomizer::randomize( filters, filtersAllocated, -0.1f, 0.1f );
WeightRandomizer::randomize( biasFilters, biasFiltersAllocated, -0.1f, 0.1f );
OpenCLHelper *cl = OpenCLHelper::createForFirstGpuOtherwiseCpu();
Propagate *p1 = Propagate::instanceSpecific( instance, cl, dim, fn );
for( int it = 0; it < (N + batchSize - 1 ) / batchSize; it++ ) {
int thisBatchSize = it < N - 1 ? batchSize : N - batchSize * it;
float *results1 = p1->propagate( thisBatchSize, inputs, filters, biasFilters );
delete[] results1;
}
StatefulTimer::dump(true);
delete p1;
delete cl;
delete[] inputs;
delete[] filters;
delete[] biasFilters;
}
示例7: Backward
BackwardGpuNaive::BackwardGpuNaive( EasyCL *cl, LayerDimensions dim ) :
Backward( cl, dim )
{
std::string options = dim.buildOptionsString();
options += ""; // " -D " + upstreamFn->getDefineName();
// [[[cog
// import stringify
// stringify.write_kernel2( "kernel", "cl/backward.cl", "calcGradInput", 'options' )
// # stringify.write_kernel2( "broadcastMultiply", "cl/backproperrorsv2.cl", "broadcast_multiply", 'options' )
// # stringify.write_kernel2( "applyActivationDeriv", "cl/applyActivationDeriv.cl", "applyActivationDeriv", 'options' )
// # stringify.write_kernel( "kernelSource", "ClConvolve.cl")
// ]]]
// generated using cog, from cl/backward.cl:
const char * kernelSource =
"// Copyright Hugh Perkins 2014 hughperkins at gmail\n"
"//\n"
"// This Source Code Form is subject to the terms of the Mozilla Public License,\n"
"// v. 2.0. If a copy of the MPL was not distributed with this file, You can\n"
"// obtain one at http://mozilla.org/MPL/2.0/.\n"
"\n"
"// expected defines:\n"
"// - none\n"
"\n"
"// globalid as: [n][upstreamPlane][upstreamrow][upstreamcol]\n"
"// inputdata: [n][upstreamPlane][upstreamrow][upstreamcol] 128 * 32 * 19 * 19 * 4 = 6MB\n"
"// gradOutput: [n][outPlane][outRow][outCol] 128 * 32 * 19 * 19 * 4 = 6MB\n"
"// weights: [filterId][inputPlane][filterRow][filterCol] 32 * 32 * 5 * 5 * 4 = 409KB\n"
"void kernel calcGradInput(\n"
" const int batchSize,\n"
" global const float *gradOutput, global float *weights, global float *gradInput ) {\n"
" int globalId = get_global_id(0);\n"
"\n"
" const int upstreamImage2dId = globalId / gInputImageSizeSquared;\n"
"\n"
" const int intraImageOffset = globalId % gInputImageSizeSquared;\n"
" const int upstreamRow = intraImageOffset / gInputImageSize;\n"
" const int upstreamCol = intraImageOffset % gInputImageSize;\n"
"\n"
" const int upstreamPlane = upstreamImage2dId % gInputPlanes;\n"
" const int n = upstreamImage2dId / gInputPlanes;\n"
"\n"
" if( n >= batchSize ) {\n"
" return;\n"
" }\n"
"\n"
" const int minFilterRow = max( 0, upstreamRow + gMargin - (gOutputImageSize - 1) );\n"
" const int maxFilterRow = min( gFilterSize - 1, upstreamRow + gMargin );\n"
" const int minFilterCol = max( 0, upstreamCol + gMargin - (gOutputImageSize -1) );\n"
" const int maxFilterCol = min( gFilterSize - 1, upstreamCol + gMargin );\n"
"\n"
" float sumWeightTimesOutError = 0;\n"
" // aggregate over [outPlane][outRow][outCol]\n"
" for( int outPlane = 0; outPlane < gNumFilters; outPlane++ ) {\n"
" for( int filterRow = minFilterRow; filterRow <= maxFilterRow; filterRow++ ) {\n"
" int outRow = upstreamRow + gMargin - filterRow;\n"
" for( int filterCol = minFilterCol; filterCol <= maxFilterCol; filterCol++ ) {\n"
" int outCol = upstreamCol + gMargin - filterCol;\n"
" int resultIndex = ( ( n * gNumFilters\n"
" + outPlane ) * gOutputImageSize\n"
" + outRow ) * gOutputImageSize\n"
" + outCol;\n"
" float thisError = gradOutput[resultIndex];\n"
" int thisWeightIndex = ( ( outPlane * gInputPlanes\n"
" + upstreamPlane ) * gFilterSize\n"
" + filterRow ) * gFilterSize\n"
" + filterCol;\n"
" float thisWeight = weights[thisWeightIndex];\n"
" float thisWeightTimesError = thisWeight * thisError;\n"
" sumWeightTimesOutError += thisWeightTimesError;\n"
" }\n"
" }\n"
" }\n"
" gradInput[globalId] = sumWeightTimesOutError;\n"
"}\n"
"\n"
"";
kernel = cl->buildKernelFromString( kernelSource, "calcGradInput", options, "cl/backward.cl" );
// [[[end]]]
// kernel = cl->buildKernel( "backproperrorsv2.cl", "calcGradInput", options );
// kernel = cl->buildKernelFromString( kernelSource, "calcGradInput", options );
}
示例8: Forward
Forward1::Forward1( EasyCL *cl, LayerDimensions dim ) :
Forward( cl, dim )
{
addBias = new AddBias( cl );
std::string options = "";
options += dim.buildOptionsString();
// [[[cog
// import stringify
// stringify.write_kernel2( "kernel", "cl/forward1.cl", "convolve_imagecubes_float2", 'options' )
// ]]]
// generated using cog, from cl/forward1.cl:
const char * kernelSource =
"// Copyright Hugh Perkins 2014, 2015 hughperkins at gmail\n"
"//\n"
"// This Source Code Form is subject to the terms of the Mozilla Public License,\n"
"// v. 2.0. If a copy of the MPL was not distributed with this file, You can\n"
"// obtain one at http://mozilla.org/MPL/2.0/.\n"
"\n"
"// notes on non-odd filtersizes:\n"
"// for odd, imagesize and filtersize 3, padZeros = 0:\n"
"// output is a single square\n"
"// m and n should vary between -1,0,1\n"
"// for even, imagesize and filtersize 2, padzeros = 0\n"
"// output is a single square, which we can position at topleft or bottomrigth\n"
"// lets position it in bottomright\n"
"// then m and n should vary as -1,0\n"
"//\n"
"// for even, imagesize and filtersize 2, padzeros = 1\n"
"// output is 2 by 2\n"
"// well... if it is even:\n"
"// - if we are not padding zeros, then we simply move our filter around the image somehow\n"
"// - if we are padding zeros, then we conceptually pad the bottom and right edge of the image with zeros by 1\n"
"// filtersize remains the same\n"
"// m will vary as -1,0,1\n"
"// outputrow is fixed by globalid\n"
"// inputrow should be unchanged...\n"
"// padzeros = 0:\n"
"// x x . . . .\n"
"// x x . . x x\n"
"// . . . . x x\n"
"// when filtersize even:\n"
"// new imagesize = oldimagesize - filtersize + 1\n"
"// when filtersize odd:\n"
"// x x x .\n"
"// x x x .\n"
"// x x x .\n"
"// . . . .\n"
"// new imagesize = oldimagesize - filtersize + 1\n"
"// padzeros = 1:\n"
"// x x\n"
"// x x . . x x . . . . . . .\n"
"// . . . x x . . x x . . .\n"
"// . . . . . . . x x . . x x\n"
"// outrow=0 outrow=1 outrow=2 x x\n"
"// outcol=0 outcol=1 outcol=2 outrow=3\n"
"// outcol=3\n"
"// when filtersize is even, and padzeros, imagesize grows by 1 each time...\n"
"// imagesize = oldimagesize + 1\n"
"// when filtersize is odd\n"
"// x x x\n"
"// x x x . x x x . . .\n"
"// x x x . x x x . x x x\n"
"// . . . x x x . x x x\n"
"// x x x\n"
"\n"
"// images are organized like [imageId][plane][row][col]\n"
"// filters are organized like [filterid][inplane][filterrow][filtercol]\n"
"// output are organized like [imageid][filterid][row][col]\n"
"// global id is organized like output, ie: [imageid][outplane][outrow][outcol]\n"
"// - no local memory used currently\n"
"// - each thread:\n"
"// - loads a whole upstream cube\n"
"// - loads a whole filter cube\n"
"// - writes one output...\n"
"void kernel convolve_imagecubes_float2(\n"
" const int numExamples,\n"
" global const float *inputs, global const float *filters,\n"
" global float *output ) {\n"
" int globalId = get_global_id(0);\n"
"\n"
" int outputImage2Id = globalId / gOutputImageSizeSquared;\n"
" int exampleId = outputImage2Id / gNumFilters;\n"
" int filterId = outputImage2Id % gNumFilters;\n"
"\n"
" // intraimage coords\n"
" int localid = globalId % gOutputImageSizeSquared;\n"
" int outputRow = localid / gOutputImageSize;\n"
" int outputCol = localid % gOutputImageSize;\n"
"\n"
" global float const*inputCube = inputs + exampleId * gNumInputPlanes * gInputImageSizeSquared;\n"
" global float const*filterCube = filters + filterId * gNumInputPlanes * gFilterSizeSquared;\n"
"\n"
" float sum = 0;\n"
" if( exampleId < numExamples ) {\n"
" for( int inputPlaneIdx = 0; inputPlaneIdx < gNumInputPlanes; inputPlaneIdx++ ) {\n"
" global float const*inputPlane = inputCube + inputPlaneIdx * gInputImageSizeSquared;\n"
" global float const*filterPlane = filterCube + inputPlaneIdx * gFilterSizeSquared;\n"
" for( int u = -gHalfFilterSize; u <= gHalfFilterSize - gEven; u++ ) {\n"
//.........这里部分代码省略.........
示例9: runtime_error
BackpropWeightsScratchLarge::BackpropWeightsScratchLarge(EasyCL *cl, LayerDimensions dim) :
BackpropWeights(cl, dim)
{
if(square(dim.filterSize) > cl->getMaxWorkgroupSize()) {
throw runtime_error("cannot use BackpropWeightsScratchLarge, since filterSize * filterSize > maxworkgroupsize");
}
// [[[cog
// import stringify
// # stringify.write_kernel("kernelSource", "ClConvolve.cl")
// ]]]
// [[[end]]]
// cout << "dim: " << dim << endl;
std::string options = dim.buildOptionsString();
int localMemoryRequirementsFullImage = dim.inputSize * dim.inputSize * 4 + dim.outputSize * dim.outputSize * 4;
int availableLocal = cl->getLocalMemorySize();
// cout << "localmemoryrequirementsfullimage: " << localMemoryRequirementsFullImage << endl;
// cout << "availablelocal: " << availableLocal << endl;
// make the local memory used about one quarter of what is available? half of what is available?
// let's try one quarter :-)
int localWeCanUse = availableLocal / 4;
numStripes = (localMemoryRequirementsFullImage + localWeCanUse - 1) / localWeCanUse;
// cout << "numStripes: " << numStripes << endl;
// make it a power of 2
numStripes = EasyCL::getNextPower2(numStripes);
// cout << "numStripes: " << numStripes << endl;
int inputStripeMarginRows = dim.filterSize - 1;
int inputStripeInnerNumRows = dim.inputSize / numStripes;
int inputStripeOuterNumRows = inputStripeInnerNumRows + 2 * inputStripeMarginRows;
int inputStripeInnerSize = inputStripeInnerNumRows * dim.inputSize;
inputStripeOuterSize = inputStripeOuterNumRows * dim.inputSize;
int inputStripeMarginSize = inputStripeMarginRows * dim.inputSize;
int outputStripeNumRows = (dim.outputSize + numStripes - 1) / numStripes;
outputStripeSize = outputStripeNumRows * dim.outputSize;
// [[[cog
// import cog_optionswriter
// cog_optionswriter.write_options(['numStripes','inputStripeMarginRows','inputStripeInnerNumRows',
// 'inputStripeOuterNumRows', 'inputStripeInnerSize', 'inputStripeOuterSize', 'inputStripeMarginSize',
// 'outputStripeNumRows', 'outputStripeSize' ])
// ]]]
// generated, using cog:
options += " -DgNumStripes=" + toString(numStripes);
options += " -DgInputStripeMarginRows=" + toString(inputStripeMarginRows);
options += " -DgInputStripeInnerNumRows=" + toString(inputStripeInnerNumRows);
options += " -DgInputStripeOuterNumRows=" + toString(inputStripeOuterNumRows);
options += " -DgInputStripeInnerSize=" + toString(inputStripeInnerSize);
options += " -DgInputStripeOuterSize=" + toString(inputStripeOuterSize);
options += " -DgInputStripeMarginSize=" + toString(inputStripeMarginSize);
options += " -DgOutputStripeNumRows=" + toString(outputStripeNumRows);
options += " -DgOutputStripeSize=" + toString(outputStripeSize);
// [[[end]]]
cout << "options: " << options << endl;
// [[[cog
// import stringify
// stringify.write_kernel2("kernel", "cl/BackpropWeightsScratchLarge.cl", "backprop_floats_withscratch_dobias_striped", 'options')
// ]]]
// generated using cog, from cl/BackpropWeightsScratchLarge.cl:
const char * kernelSource =
"// Copyright Hugh Perkins 2014,2015 hughperkins at gmail\n"
"//\n"
"// This Source Code Form is subject to the terms of the Mozilla Public License,\n"
"// v. 2.0. If a copy of the MPL was not distributed with this file, You can\n"
"// obtain one at http://mozilla.org/MPL/2.0/.\n"
"\n"
"// expected defines:\n"
"// BIASED (or not)\n"
"\n"
"// workgroupId: [outputPlane][inputPlane]\n"
"// localId: [filterRow][filterCol]\n"
"// per-thread iteration: [n][outputRow][outputCol]\n"
"// local: errorimage: outputSize * outputSize\n"
"// imageimage: inputSize * inputSize\n"
"// specific characteristic: load one stripe of each image at a time,\n"
"// so we dont run out of memory\n"
"// number of stripes set in: gNumStripes\n"
"// note that whilst we can stripe the gradOutput simply,\n"
"// we actually need to add a half-filter widthed additional few rows\n"
"// onto the images stripe, otherwise we will be missing data\n"
"// we will call the size of the non-overlapping image stripes: gInputStripeInnerSize\n"
"// the outersize, including the two margins is: gInputStripeOuterSize\n"
"// of course, the first and last stripes will be missing a bit off the top/bottom, where the\n"
"// corresponding outer margin would be\n"
"void kernel backprop_floats_withscratch_dobias_striped(\n"
" const float learningRateMultiplier, const int batchSize,\n"
" global const float *gradOutput, global const float *images,\n"
" global float *gradWeights,\n"
" #ifdef BIASED\n"
" global float *gradBiasWeights,\n"
" #endif\n"
" local float *_errorStripe, local float *_imageStripe\n"
" ) {\n"
" // gHalfFilterSize\n"
" // gInputSize\n"
" //\n"
//.........这里部分代码省略.........