int MLPOutputLayer::updateState(double timef, double dt) { int status = MLPSigmoidLayer::updateState(timef, dt); //Collect stats if needed if(statProgressPeriod > 0){ //TODO add more if statements for different cases if(!localTarget){ if(getLayerLoc()->nf == 1){ binaryNonlocalStats(); } else{ multiclassNonlocalStats(); } } else{ if(getLayerLoc()->nf == 1){ binaryLocalStats(); } else{ std::cout << "Not implemented\n"; exit(EXIT_FAILURE); //TODO //multiclassNonlocalStats(); } } } //For testing purposes pvdata_t * A = getCLayer()->activity->data; // for(int ni = 0; ni < getNumNeurons(); ni++){ //int nExt = kIndexExtended(ni, loc->nx, loc->ny, loc->nf, loc->nb); // std::cout << timef <<": ni: " << ni << " A: " << A[ni] << "\n"; // } return PV_SUCCESS; }
int SoundStream::allocateDataStructures() { int status = PV::HyPerLayer::allocateDataStructures(); free(clayer->V); clayer->V = NULL; // Point to clayer data struct soundData = clayer->activity->data; assert(soundData!=NULL); //Layer must be 1 by 1 by 1 if(getLayerLoc()->nx != 1 || getLayerLoc()->ny != 1){ fprintf(stderr, "SoundStream::SoundStream layer must be 1 by 1 in the x and y direction\n"); exit(EXIT_FAILURE); } if(getLayerLoc()->nf > fileHeader->channels){ fprintf(stderr, "SoundStream::Audio file has %d channels, while the number of features is %d\n", fileHeader->channels, getLayerLoc()->nf); exit(EXIT_FAILURE); } //Allocate read buffer based on number of channels soundBuf = (float*) malloc(sizeof(float) * fileHeader->channels); if(frameStart <= 1){ frameStart = 1; } //Set to frameStart, which is 1 indexed sf_seek(fileStream, frameStart-1, SEEK_SET); status = updateState(0,parent->getDeltaTime()); //Reset filepointer to reread the same frame on the 0th timestep sf_seek(fileStream, frameStart-1, SEEK_SET); return status; }
int PursuitLayer::constrainMinima() { int nxy = getLayerLoc()->nx * getLayerLoc()->ny; for (int kxy=0; kxy<nxy; kxy++) { if (foundFeatures[kxy]>=0 && minLocationsBestFeature[kxy]<-gSynSparse[kxy]) { pvdata_t b = (gSynSparse[kxy]+minLocationsBestFeature[kxy])/minLocationsBestFeature[kxy]; energyDropsBestFeature[kxy] -= energyDropsBestFeature[kxy]*b*b; minLocationsBestFeature[kxy] = -gSynSparse[kxy]; } } return PV_SUCCESS; }
int LCALIFLayer::checkpointWrite(const char * cpDir) { int status = LIFGap::checkpointWrite(cpDir); InterColComm * icComm = parent->icCommunicator(); char basepath[PV_PATH_MAX]; char filename[PV_PATH_MAX]; int lenbase = snprintf(basepath, PV_PATH_MAX, "%s/%s", cpDir, name); if (lenbase+strlen("_integratedSpikeCount.pvp") >= PV_PATH_MAX) { // currently _integratedSpikeCount.pvp is the longest suffix needed if (icComm->commRank()==0) { fprintf(stderr, "LCALIFLayer::checkpointWrite error in layer \"%s\". Base pathname \"%s/%s_\" too long.\n", name, cpDir, name); } abort(); } double timed = (double) parent->simulationTime(); int chars_needed = snprintf(filename, PV_PATH_MAX, "%s_integratedSpikeCount.pvp", basepath); assert(chars_needed < PV_PATH_MAX); writeBufferFile(filename, icComm, timed, &integratedSpikeCount, 1, /*extended*/false, getLayerLoc()); chars_needed = snprintf(filename, PV_PATH_MAX, "%s_Vadpt.pvp", basepath); assert(chars_needed < PV_PATH_MAX); writeBufferFile(filename, icComm, timed, &Vadpt, 1, /*extended*/false, getLayerLoc()); chars_needed = snprintf(filename, PV_PATH_MAX, "%s_Vattained.pvp", basepath); assert(chars_needed < PV_PATH_MAX); writeBufferFile(filename, icComm, timed, &Vattained, 1, /*extended*/false, getLayerLoc()); chars_needed = snprintf(filename, PV_PATH_MAX, "%s_Vmeminf.pvp", basepath); assert(chars_needed < PV_PATH_MAX); writeBufferFile(filename, icComm, timed, &Vmeminf, 1, /*extended*/false, getLayerLoc()); chars_needed = snprintf(filename, PV_PATH_MAX, "%s_G_Norm.pvp", basepath); assert(chars_needed < PV_PATH_MAX); writeBufferFile(filename, icComm, timed, &G_Norm, 1, /*extended*/false, getLayerLoc()); chars_needed = snprintf(filename, PV_PATH_MAX, "%s_GSynExcEffective.pvp", basepath); assert(chars_needed < PV_PATH_MAX); writeBufferFile(filename, icComm, timed, &GSynExcEffective, 1, /*extended*/false, getLayerLoc()); chars_needed = snprintf(filename, PV_PATH_MAX, "%s_GSynInhEffective.pvp", basepath); assert(chars_needed < PV_PATH_MAX); writeBufferFile(filename, icComm, timed, &GSynInhEffective, 1, /*extended*/false, getLayerLoc()); chars_needed = snprintf(filename, PV_PATH_MAX, "%s_excitatoryNoise.pvp", basepath); assert(chars_needed < PV_PATH_MAX); writeBufferFile(filename, icComm, timed, &excitatoryNoise, 1, /*extended*/false, getLayerLoc()); chars_needed = snprintf(filename, PV_PATH_MAX, "%s_inhibitoryNoise.pvp", basepath); assert(chars_needed < PV_PATH_MAX); writeBufferFile(filename, icComm, timed, &inhibitoryNoise, 1, /*extended*/false, getLayerLoc()); chars_needed = snprintf(filename, PV_PATH_MAX, "%s_inhibNoiseB.pvp", basepath); assert(chars_needed < PV_PATH_MAX); writeBufferFile(filename, icComm, timed, &inhibNoiseB, 1, /*extended*/false, getLayerLoc()); return status; }
/* * Segment values are such: * 1 1 1 2 2 2 3 3 * 1 1 1 2 2 2 3 3 * 1 1 1 2 2 2 3 3 * 4 4 4 5 5 5 6 6 * 4 4 4 5 5 5 6 6 * 4 4 4 5 5 5 6 6 * 7 7 7 8 8 8 9 9 * 7 7 7 8 8 8 9 9 */ float SegmentifyTest::getTargetVal(int yi, int xi, int fi){ const PVLayerLoc * loc = getLayerLoc(); //We can convert yi and xi to an index between 0 and 2 int newYi = yi / 3; int newXi = xi / 3; int segmentLabel = newYi * 3 + newXi + 1; int returnLabel = -1; if(strcmp(inputMethod, "sum") == 0){ //Account for edge cases if(segmentLabel == 3 || segmentLabel == 6 || segmentLabel == 7 || segmentLabel == 8){ returnLabel = segmentLabel * 6; } else if(segmentLabel == 9){ returnLabel = segmentLabel * 4; } else{ returnLabel = segmentLabel * 9; } } else if(strcmp(inputMethod, "average") == 0 || strcmp(inputMethod, "max") == 0){ returnLabel = segmentLabel; } else{ //Should never get here assert(0); } return returnLabel; }
int BinaryThresh::updateState(double time, double dt) { const PVLayerLoc * loc = getLayerLoc(); int nx = loc->nx; int ny = loc->ny; int nf = loc->nf; int num_neurons = nx*ny*nf; //Reset pointer of gSynHead to point to the inhib channel pvdata_t * GSynExt = getChannel(CHANNEL_EXC); pvdata_t * GSynInh = getChannel(CHANNEL_INH); pvdata_t * A = getCLayer()->activity->data; pvdata_t * V = getV(); #ifdef PV_USE_OPENMP_THREADS #pragma omp parallel for #endif for(int ni = 0; ni < num_neurons; ni++){ int next = kIndexExtended(ni, nx, ny, nf, loc->halo.lt, loc->halo.rt, loc->halo.dn, loc->halo.up); //Activity is either 0 or 1 based on if it's active A[next] = GSynExt[ni] == 0 ? 0 : 1; } return PV_SUCCESS; }
int SegmentifyTest::updateState(double timef, double dt){ //Do update state first Segmentify::updateState(timef, dt); const PVLayerLoc * loc = getLayerLoc(); pvdata_t * A = getActivity(); assert(A); for(int bi = 0; bi < loc->nbatch; bi++){ pvdata_t * batchA = A + bi * getNumExtended(); for(int yi = 0; yi < loc->ny; yi++){ for(int xi = 0; xi < loc->nx; xi++){ for(int fi = 0; fi < loc->nf; fi++){ int extIdx = (yi + loc->halo.up) * (loc->nx + loc->halo.lt + loc->halo.rt) * loc->nf + (xi + loc->halo.lt) * loc->nf + fi; float actualVal = batchA[extIdx]; float targetVal = getTargetVal(yi+loc->ky0, xi+loc->kx0, fi); checkOutputVals(yi+loc->ky0, xi+loc->kx0, fi, targetVal, actualVal); //std::cout << "Idx: (" << bi << "," << yi << "," << xi << "," << fi << ") Val: " << actualVal << " Target: " << targetVal << "\n"; } } } } return PV_SUCCESS; }
int ImageTestLayer::updateStateWrapper(double time, double dt) { Image::updateStateWrapper(time, dt); const PVLayerLoc * loc = getLayerLoc(); int nx = loc->nx; int ny = loc->ny; int nf = loc->nf; int nbatch = loc->nbatch; for(int b = 0; b < nbatch; b++){ pvdata_t * dataBatch = data + b * getNumExtended(); for(int nkRes = 0; nkRes < getNumNeurons(); nkRes++){ //Calculate extended index int nkExt = kIndexExtended(nkRes, nx, ny, nf, loc->halo.lt, loc->halo.rt, loc->halo.dn, loc->halo.up); //checkVal is the value from batch index 0 pvdata_t checkVal = dataBatch[nkExt] * 255; int kxGlobal = kxPos(nkRes, nx, ny, nf) + loc->kx0; int kyGlobal = kyPos(nkRes, nx, ny, nf) + loc->ky0; int kf = featureIndex(nkRes, nx, ny, nf); pvdata_t expectedVal = kIndex(kxGlobal, kyGlobal, kf, loc->nxGlobal, loc->nyGlobal, nf); if(fabs(checkVal - expectedVal) >= 1e-5){ std::cout << "ImageFileIO test Expected: " << expectedVal << " Actual: " << checkVal << "\n"; exit(-1); } } } return PV_SUCCESS; }
int ComparisonLayer::updateState(double timef, double dt){ //Grab layer size const PVLayerLoc* loc = getLayerLoc(); int nx = loc->nx; int ny = loc->ny; int nf = loc->nf; int kx0 = loc->kx0; int ky0 = loc->ky0; pvdata_t * GSynExt = getChannel(CHANNEL_EXC); //gated pvdata_t * GSynInh = getChannel(CHANNEL_INH); //gt bool isCorrect = true; //Grab the activity layer of current layer //We only care about restricted space for (int k = 0; k < getNumNeurons(); k++){ if(GSynExt[k] != GSynInh[k]){ std::cout << "Connection " << name << " Mismatch at " << k << ": actual value: " << GSynExt[k] << " Expected value: " << GSynInh[k] << ".\n"; isCorrect = false; } } if(!isCorrect){ exit(-1); } return PV_SUCCESS; }
int SoundStream::updateState(double time, double dt){ int status = PV_SUCCESS; assert(fileStream); // if (time >= nextSampleTime) { // nextSampleTime += (1.0 / sampleRate); //Read 1 frame int numRead = sf_readf_float(fileStream, soundBuf, 1); //EOF if(numRead == 0){ sf_seek(fileStream, 0, SEEK_SET); numRead = sf_readf_float(fileStream, soundBuf, 1); if(numRead == 0){ fprintf(stderr, "SoundStream:: Fatal error, is the file empty?\n"); exit(EXIT_FAILURE); } std::cout << "Rewinding sound file\n"; } else if(numRead > 1){ fprintf(stderr, "SoundStream:: Fatal error, numRead is bigger than 1\n"); exit(EXIT_FAILURE); } for(int fi = 0; fi < getLayerLoc()->nf; fi++){ soundData[fi] = soundBuf[fi]; } // } return status; }
int MaskError::updateState(double time, double dt) { const PVLayerLoc * loc = getLayerLoc(); int nx = loc->nx; int ny = loc->ny; int nf = loc->nf; int num_neurons = nx*ny*nf; //Reset pointer of gSynHead to point to the inhib channel pvdata_t * GSynExt = getChannel(CHANNEL_EXC); pvdata_t * GSynInh = getChannel(CHANNEL_INH); pvdata_t * A = getCLayer()->activity->data; pvdata_t * V = getV(); #ifdef PV_USE_OPENMP_THREADS #pragma omp parallel for #endif for(int ni = 0; ni < num_neurons; ni++){ int next = kIndexExtended(ni, nx, ny, nf, loc->halo.lt, loc->halo.rt, loc->halo.dn, loc->halo.up); //expected - actual only if expected isn't 0 if(GSynExt[ni] == 0){ A[next] = 0; } else{ A[next] = GSynExt[ni] - GSynInh[ni]; } } return PV_SUCCESS; }
int MaskFromMemoryBuffer::updateState(double time, double dt) { if (imageLayer->getDataLeft() == dataLeft && imageLayer->getDataTop() == dataTop && imageLayer->getDataWidth() == dataRight-dataLeft && imageLayer->getDataHeight() && dataBottom-dataTop) { return PV_SUCCESS; // mask only needs to change if the imageLayer changes its active region } dataLeft = imageLayer->getDataLeft(); dataRight = dataLeft+imageLayer->getDataWidth(); dataTop = imageLayer->getDataTop(); dataBottom = dataTop + imageLayer->getDataHeight(); PVLayerLoc const * loc = getLayerLoc(); for(int b = 0; b < loc->nbatch; b++) { pvdata_t * ABatch = getActivity() + b * getNumExtended(); int const num_neurons = getNumNeurons(); #ifdef PV_USE_OPENMP_THREADS #pragma omp parallel for #endif for(int ni = 0; ni < num_neurons; ni++) { PVHalo const * halo = &loc->halo; int const nx = loc->nx; int const ny = loc->ny; int const nf = loc->nf; int x = kxPos(ni, nx, ny, nf); int y = kyPos(ni, nx, ny, nf); pvadata_t a = (pvadata_t) (x>=dataLeft && x < dataRight && y >= dataTop && y < dataBottom); int nExt = kIndexExtended(ni, nx, ny, nf, halo->lt, halo->rt, halo->dn, halo->up); ABatch[nExt] = a; } } return PV_SUCCESS; }
int MLPForwardLayer::updateState(double time, double dt) { //update_timer->start(); const PVLayerLoc * loc = getLayerLoc(); int nx = loc->nx; int ny = loc->ny; int nf = loc->nf; int num_neurons = nx*ny*nf; //Reset pointer of gSynHead to point to the excitatory channel pvdata_t * GSynExt = getChannel(CHANNEL_EXC); pvdata_t * V = getV(); #ifdef PV_USE_OPENMP_THREADS #pragma omp parallel for #endif for(int ni = 0; ni < num_neurons*loc->nbatch; ni++){ //int next = kIndexExtended(ni, nx, ny, nf, loc->nb); double p = randState->uniformRandom(); if(p <= dropoutChance){ //Set dropout flag dropout[ni] = true; } else{ dropout[ni] = false; } V[ni] = GSynExt[ni] * potentialScale; //if(strcmp(name, "ForwardLayerFinal") == 0){ // std::cout << "Neuron: " << ni << " V: " << V[ni] << "\n"; //} } //A never updated, TODO see if you can remove A buffer //update_timer->stop(); return PV_SUCCESS; }
int PursuitLayer::checkpointWrite(const char * cpDir) { int status = HyPerLayer::checkpointWrite(cpDir); InterColComm * icComm = parent->icCommunicator(); double timed = (double) parent->simulationTime(); int chars_needed; int filenamesize = strlen(cpDir)+1+strlen(name)+29; // The +1 is for the slash between cpDir and name; the +29 needs to be large enough to hold the suffix (e.g. _minLocationsBestFeature.pvp) plus the null terminator char * filename = (char *) malloc( filenamesize*sizeof(char) ); assert(filename != NULL); PVLayerLoc flat_loc; memcpy(&flat_loc, getLayerLoc(), sizeof(PVLayerLoc)); flat_loc.nf = 1; chars_needed = snprintf(filename, filenamesize, "%s/%s_gSynSparse.pvp", cpDir, name); assert(chars_needed < filenamesize); writeBufferFile(filename, icComm, timed, &gSynSparse, 1/*numbands*/, false/*extended*/, &flat_loc); pvdata_t buffer1feature[flat_loc.nx*flat_loc.ny]; chars_needed = snprintf(filename, filenamesize, "%s/%s_foundFeatures.pvp", cpDir, name); assert(chars_needed < filenamesize); for (int k=0; k<flat_loc.nx*flat_loc.ny; k++) { buffer1feature[k] = (pvdata_t) foundFeatures[k]; } pvdata_t * buffer1ptr = buffer1feature; writeBufferFile(filename, icComm, timed, &buffer1ptr, 1/*numbands*/, false/*extended*/, &flat_loc); status = parent->writeScalarToFile(cpDir, getName(), "nextUpdate", nextUpdate); assert(status==PV_SUCCESS); free(filename); return status; }
int MLPOutputLayer::communicateInitInfo(){ int status = MLPSigmoidLayer::communicateInitInfo(); if(statProgressPeriod > 0){ gtLayer = parent->getLayerFromName(gtLayername); if (gtLayer==NULL) { if (parent->columnId()==0) { fprintf(stderr, "%s \"%s\" error: gtLayername \"%s\" is not a layer in the HyPerCol.\n", getKeyword(), name, gtLayername); } #ifdef PV_USE_MPI MPI_Barrier(parent->icCommunicator()->communicator()); #endif exit(EXIT_FAILURE); } const PVLayerLoc * srcLoc = gtLayer->getLayerLoc(); const PVLayerLoc * loc = getLayerLoc(); assert(srcLoc != NULL && loc != NULL); if (srcLoc->nxGlobal != loc->nxGlobal || srcLoc->nyGlobal != loc->nyGlobal || srcLoc->nf != loc->nf) { if (parent->columnId()==0) { fprintf(stderr, "%s \"%s\" error: gtLayerName \"%s\" does not have the same dimensions.\n", getKeyword(), name, gtLayername); fprintf(stderr, " original (nx=%d, ny=%d, nf=%d) versus (nx=%d, ny=%d, nf=%d)\n", srcLoc->nxGlobal, srcLoc->nyGlobal, srcLoc->nf, loc->nxGlobal, loc->nyGlobal, loc->nf); } #ifdef PV_USE_MPI MPI_Barrier(parent->icCommunicator()->communicator()); #endif exit(EXIT_FAILURE); } } return(status); }
int CIFARGTLayer::updateState(double timef, double dt) { //getline (inputfile,inputString); inputString = std::string(imageLayer->getFilename()); unsigned found = inputString.find_last_of("/\\"); //CIFAR is 0 indexed char cVal = inputString.at(found-1); iVal = cVal - '0'; pvdata_t * A = getCLayer()->activity->data; const PVLayerLoc * loc = getLayerLoc(); //std::cout << "time: " << parent->simulationTime() << " inputString:" << inputString << " iVal:" << iVal << "\n"; assert(iVal >= 0 && iVal < 10); //NF must be 10, one for each class assert(loc->nf == 10); for(int ni = 0; ni < getNumNeurons(); ni++){ int nExt = kIndexExtended(ni, loc->nx, loc->ny, loc->nf, loc->halo.lt, loc->halo.rt, loc->halo.dn, loc->halo.up); int fi = featureIndex(nExt, loc->nx+loc->halo.rt+loc->halo.lt, loc->ny+loc->halo.dn+loc->halo.up, loc->nf); if(fi == iVal){ A[nExt] = 1; } else{ if(negativeGt){ A[nExt] = -1; } else{ A[nExt] = 0; } } } return PV_SUCCESS; }
int BinningLayer::updateState(double timef, double dt) { int status; assert(GSyn==NULL); pvdata_t * gSynHead = NULL; status = doUpdateState(timef, dt, originalLayer->getLayerLoc(), getLayerLoc(), originalLayer->getLayerData(delay), getActivity(), binMax, binMin); return status; }
int SegmentLayer::communicateInitInfo() { int status = HyPerLayer::communicateInitInfo(); //Get original layer originalLayer = parent->getLayerFromName(originalLayerName); if (originalLayer==NULL) { if (parent->columnId()==0) { fprintf(stderr, "%s \"%s\" error: originalLayerName \"%s\" is not a layer in the HyPerCol.\n", getKeyword(), name, originalLayerName); } MPI_Barrier(parent->icCommunicator()->communicator()); exit(EXIT_FAILURE); } if (originalLayer->getInitInfoCommunicatedFlag()==false) { return PV_POSTPONE; } //Sync margins originalLayer->synchronizeMarginWidth(this); this->synchronizeMarginWidth(originalLayer); //Check size const PVLayerLoc * srcLoc = originalLayer->getLayerLoc(); const PVLayerLoc * thisLoc = getLayerLoc(); //Original layer must be the same x/y size as this layer if (srcLoc->nxGlobal != thisLoc->nxGlobal || srcLoc->nyGlobal != thisLoc->nyGlobal) { if (parent->columnId()==0) { fprintf(stderr, "%s \"%s\" error: originalLayer \"%s\" does not have the same x and y dimensions as this layer.\n", getKeyword(), name, originalLayerName); fprintf(stderr, " original (nx=%d, ny=%d) versus (nx=%d, ny=%d)\n", srcLoc->nxGlobal, srcLoc->nyGlobal, thisLoc->nxGlobal, thisLoc->nyGlobal); } MPI_Barrier(parent->icCommunicator()->communicator()); exit(EXIT_FAILURE); } //This layer must have only 1 feature if(thisLoc->nf != 1){ if (parent->columnId()==0) { fprintf(stderr, "%s \"%s\" error: SegmentLayer must have 1 feature.\n", getKeyword(), name); } MPI_Barrier(parent->icCommunicator()->communicator()); exit(EXIT_FAILURE); } //If segmentMethod is none, we also need to make sure the srcLayer also has nf == 1 if(strcmp(segmentMethod, "none") == 0 && srcLoc->nf != 1){ if (parent->columnId()==0) { fprintf(stderr, "%s \"%s\" error: Source layer must have 1 feature with segmentation method \"none\".\n", getKeyword(), name); } MPI_Barrier(parent->icCommunicator()->communicator()); exit(EXIT_FAILURE); } return status; }
/** * - Update the image buffers * - If the time is a multiple of biasChangetime then the position of the bias (biasX, biasY) changes. * - With probability persistenceProb the offset position (offsetX, offsetY) remains unchanged. * - Otherwise, with probability (1-persistenceProb) the offset position performs a random walk * around the bias position (biasX, biasY). * * - If the time is a multiple of displayPeriod then load the next image. * - If nf=1 then the image is converted to grayscale during the call to read(filename, offsetX, offsetY). * If nf>1 then the image is loaded with color information preserved. * - Return true if buffers have changed */ bool Movie::updateImage(double time, double dt) { if( jitterFlag ) { jitter(); } // jitterFlag InterColComm * icComm = getParent()->icCommunicator(); //TODO: Fix movie layer to take with batches. This is commented out for compile //if(!flipOnTimescaleError && (parent->getTimeScale() > 0 && parent->getTimeScale() < parent->getTimeScaleMin())){ // if (parent->icCommunicator()->commRank()==0) { // std::cout << "timeScale of " << parent->getTimeScale() << " is less than timeScaleMin of " << parent->getTimeScaleMin() << ", Movie is keeping the same frame\n"; // } //} //else{ //Only do this if it's not the first update timestep //The timestep number is (time - startTime)/(width of timestep), with allowance for roundoff. //But if we're using adaptive timesteps, the dt passed as a function argument is not the correct (width of timestep). if(fabs(time - (parent->getStartTime() + parent->getDeltaTime())) > (parent->getDeltaTime()/2)){ int status = getFrame(time, dt); assert(status == PV_SUCCESS); } //nextDisplayTime removed, now using nextUpdateTime in HyPerLayer //while (time >= nextDisplayTime) { // nextDisplayTime += displayPeriod; //} //Set frame number (member variable in Image) //Write to timestamp file here when updated if( icComm->commRank()==0 ) { //Only write if the parameter is set if(timestampFile){ std::ostringstream outStrStream; outStrStream.precision(15); int kb0 = getLayerLoc()->kb0; for(int b = 0; b < parent->getNBatch(); b++){ outStrStream << time << "," << b+kb0 << "," << frameNumbers[b] << "," << framePath[b] << "\n"; } size_t len = outStrStream.str().length(); int status = PV_fwrite(outStrStream.str().c_str(), sizeof(char), len, timestampFile)==len ? PV_SUCCESS : PV_FAILURE; if (status != PV_SUCCESS) { fprintf(stderr, "%s \"%s\" error: Movie::updateState failed to write to timestamp file.\n", getKeyword(), name); exit(EXIT_FAILURE); } //Flush buffer fflush(timestampFile->fp); } } //} // randomMovie return true; }
int LeakyIntegrator::updateState(double timed, double dt) { pvdata_t * V = getV(); pvdata_t * gSyn = GSyn[0]; pvdata_t decayfactor = (pvdata_t) exp(-dt/integrationTime); for (int k=0; k<getNumNeuronsAllBatches(); k++) { V[k] *= decayfactor; V[k] += gSyn[k]; } int nx = getLayerLoc()->nx; int ny = getLayerLoc()->ny; int nf = getLayerLoc()->nf; int nbatch = getLayerLoc()->nbatch; PVHalo const * halo = &getLayerLoc()->halo; pvdata_t * A = getActivity(); int status = setActivity_PtwiseLinearTransferLayer(nbatch, getNumNeurons(), A, V, nx, ny, nf, halo->lt, halo->rt, halo->dn, halo->up, numVertices, verticesV, verticesA, slopes); return status; }
int MLPForwardLayer::allocateDataStructures() { int status = ANNLayer::allocateDataStructures(); // // a random state variable is needed for every neuron/clthread randState = new PV::Random(parent, getLayerLoc(), false/*isExtended*/); if (randState == NULL) { fprintf(stderr, "MLPForwardLayer::initialize error. Layer \"%s\" unable to create object of Random class.\n", getName()); exit(EXIT_FAILURE); } return status; }
int PursuitLayer::readStateFromCheckpoint(const char * cpDir, double * timeptr) { int status = ANNLayer::readStateFromCheckpoint(cpDir, timeptr); PVLayerLoc flat_loc; memcpy(&flat_loc, getLayerLoc(), sizeof(PVLayerLoc)); flat_loc.nf = 1; pvdata_t buffer1feature[flat_loc.nx*flat_loc.ny]; status = read_gSynSparseFromCheckpoint(cpDir, timeptr, &flat_loc); status = read_foundFeaturesFromCheckpoint(cpDir, timeptr, &flat_loc); return status; }
int StreamReconLayer::updateState(double timef, double dt) { update_timer->start(); pvdata_t * V = getV(); int nx = getLayerLoc()->nx; int ny = getLayerLoc()->ny; int nf = getLayerLoc()->nf; for (int i = 0; i < nx; i++) { int vx = i; int gx = i; for (int j = 0; j < nf; j ++) { int vf = j; int gf; if (vf + bufferLevel < nf) { gf = bufferLevel + j; } else { gf = bufferLevel + j - nf; } int vindex = kIndex(vx, 0, vf, nx, ny, nf); int gindex = kIndex(gx, 0, gf, nx, ny, nf); V[vindex] = GSyn[0][gindex]; } } //Copy V to A buffer PV::HyPerLayer::setActivity(); if (bufferLevel < nf - 1) { bufferLevel++; } else { bufferLevel = 0; } update_timer->stop(); return PV_SUCCESS; } // end update state
// set V to global x/y/f position int PlasticConnTestLayer::copyAtoV(){ const PVLayerLoc * loc = getLayerLoc(); pvdata_t * V = getV(); pvdata_t * A = clayer->activity->data; for (int kLocal = 0; kLocal < getNumNeurons(); kLocal++){ int kExtended = kIndexExtended(kLocal, loc->nx, loc->ny, loc->nf, loc->halo.lt, loc->halo.rt, loc->halo.dn, loc->halo.up); V[kLocal] = A[kExtended]; } return PV_SUCCESS; }
int BackwardsBatchNorm::clearDelta() { int nf = getLayerLoc()->nf; for(int i = 0; i < nf; i++) { deltaVar[i] = 0; deltaMean[i] = 0; deltaVarShift[i] = 0; deltaMeanShift[i] = 0; } return PV_SUCCESS; }
int InputLayer::updateState(double timef, double dt) { if(!constantValue || firstRun){ char cVal = inputString.at(int(parent->simulationTime()-1)%numExamples); iVal = cVal - '0'; } pvdata_t * A = getCLayer()->activity->data; const PVLayerLoc * loc = getLayerLoc(); assert(loc->nf == 2); //Set binary values of xor values std::cout << timef << ": input val:" << iVal << "\n"; int negVal; negVal = -1; for(int ni = 0; ni < getNumNeurons(); ni++){ int nExt = kIndexExtended(ni, loc->nx, loc->ny, loc->nf, loc->halo.lt, loc->halo.rt, loc->halo.dn, loc->halo.up); int fi = featureIndex(nExt, loc->nx+loc->halo.lt+loc->halo.rt, loc->ny+loc->halo.dn+loc->halo.up, loc->nf); switch(iVal){ case 0: if(fi == 0){ A[nExt] = negVal; } if(fi == 1){ A[nExt] = negVal; } break; case 1: if(fi == 0){ A[nExt] = negVal; } if(fi == 1){ A[nExt] = 1; } break; case 2: if(fi == 0){ A[nExt] = 1; } if(fi == 1){ A[nExt] = negVal; } break; case 3: if(fi == 0){ A[nExt] = 1; } if(fi == 1){ A[nExt] = 1; } break; } } firstRun = false; return PV_SUCCESS; }
int MLPOutputLayer::allocateDataStructures() { int status = MLPSigmoidLayer::allocateDataStructures(); //Allocate buffer size of this layer's nf (number of output classes) int nf = getLayerLoc()->nf; if(!localTarget){ classBuffer = (pvdata_t*) malloc(nf * sizeof(pvdata_t)); } //Grab gtLayername if it exists return status; }
int ANNWeightedErrorLayer::allocateDataStructures() { int status = HyPerLayer::allocateDataStructures(); int nf = getLayerLoc()->nf; errWeights = (float *) calloc(nf, sizeof(float *)); for(int i_weight = 0; i_weight < nf; i_weight++){ errWeights[i_weight] = 1.0f; } PV_Stream * pvstream = NULL; InterColComm *icComm = getParent()->icCommunicator(); char errWeight_string[PV_PATH_MAX]; if (getParent()->icCommunicator()->commRank()==0) { PV_Stream * errWeights_stream = pvp_open_read_file(errWeightsFileName, icComm); for(int i_weight = 0; i_weight < nf; i_weight++){ char * fgetsstatus = fgets(errWeight_string, PV_PATH_MAX, errWeights_stream->fp); if( fgetsstatus == NULL ) { bool endoffile = feof(errWeights_stream->fp)!=0; if( endoffile ) { fprintf(stderr, "File of errWeights \"%s\" reached end of file before all %d errWeights were read. Exiting.\n", errWeightsFileName, nf); exit(EXIT_FAILURE); } else { int error = ferror(errWeights_stream->fp); assert(error); fprintf(stderr, "File of errWeights: error %d while reading. Exiting.\n", error); exit(error); } } else { // Remove linefeed from end of string errWeight_string[PV_PATH_MAX-1] = '\0'; int len = strlen(errWeight_string); if (len > 1) { if (errWeight_string[len-1] == '\n') { errWeight_string[len-1] = '\0'; } } } // fgetstatus // set errWeight = chance / relative fraction float errWeight_tmp = atof(errWeight_string); fprintf(stderr, "errWeight %i = %f\n", i_weight, errWeight_tmp); errWeights[i_weight] = (1.0/nf) / errWeight_tmp; } // i_weight } // commRank() == rootproc #ifdef PV_USE_MPI //broadcast errWeights MPI_Bcast(errWeights, nf, MPI_FLOAT, 0, icComm->communicator()); #endif // PV_USE_MPI }
int AccumulateLayer::setActivity() { const PVLayerLoc * loc = getLayerLoc(); int nx = loc->nx; int ny = loc->ny; int nf = loc->nf; int num_neurons = nx*ny*nf; int status = PV_SUCCESS; memset(clayer->activity->data, 0, sizeof(pvdata_t)*getNumExtended()); if( status == PV_SUCCESS ) status = applyVThresh_ANNLayer(num_neurons, getV(), AMin, VThresh, AShift, VWidth, getCLayer()->activity->data, nx, ny, nf, loc->halo.lt, loc->halo.rt, loc->halo.dn, loc->halo.up); if( status == PV_SUCCESS ) status = applyVMax_ANNLayer(num_neurons, getV(), AMax, getCLayer()->activity->data, nx, ny, nf, loc->halo.lt, loc->halo.rt, loc->halo.dn, loc->halo.up); return status; }
int RunningAverageLayer::updateState(double timef, double dt) { numUpdateTimes++; int status = PV_SUCCESS; double deltaT = parent->getDeltaTime(); //Check if an update is needed //Done in cloneVLayer //if(checkIfUpdateNeeded()){ int numNeurons = originalLayer->getNumNeurons(); pvdata_t * A = clayer->activity->data; const pvdata_t * originalA = originalLayer->getCLayer()->activity->data; const PVLayerLoc * loc = getLayerLoc(); const PVLayerLoc * locOriginal = originalLayer->getLayerLoc(); int nbatch = loc->nbatch; //Make sure all sizes match //assert(locOriginal->nb == loc->nb); assert(locOriginal->nx == loc->nx); assert(locOriginal->ny == loc->ny); assert(locOriginal->nf == loc->nf); for(int b = 0; b < nbatch; b++){ const pvdata_t * originalABatch = originalA + b * originalLayer->getNumExtended(); pvdata_t * ABatch = A + b * getNumExtended(); if (numUpdateTimes < numImagesToAverage*deltaT){ #ifdef PV_USE_OPENMP_THREADS #pragma omp parallel for #endif // PV_USE_OPENMP_THREADS for(int k=0; k<numNeurons; k++) { int kExt = kIndexExtended(k, loc->nx, loc->ny, loc->nf, loc->halo.lt, loc->halo.rt, loc->halo.dn, loc->halo.up); int kExtOriginal = kIndexExtended(k, locOriginal->nx, locOriginal->ny, locOriginal->nf, locOriginal->halo.lt, loc->halo.rt, loc->halo.dn, loc->halo.up); ABatch[kExt] = ((numUpdateTimes/deltaT-1) * ABatch[kExt] + originalABatch[kExtOriginal]) * deltaT / numUpdateTimes; } } else{ #ifdef PV_USE_OPENMP_THREADS #pragma omp parallel for #endif // PV_USE_OPENMP_THREADS for(int k=0; k<numNeurons; k++) { int kExt = kIndexExtended(k, loc->nx, loc->ny, loc->nf, loc->halo.lt, loc->halo.rt, loc->halo.dn, loc->halo.up); int kExtOriginal = kIndexExtended(k, locOriginal->nx, locOriginal->ny, locOriginal->nf, locOriginal->halo.lt, loc->halo.rt, loc->halo.dn, loc->halo.up); ABatch[kExt] = ((numImagesToAverage-1) * ABatch[kExt] + originalABatch[kExtOriginal]) / numImagesToAverage; } } } //Update lastUpdateTime lastUpdateTime = parent->simulationTime(); //} return status; }