8 #include "L2NormProbe.hpp" 9 #include "columns/HyPerCol.hpp" 10 #include "layers/HyPerLayer.hpp" 14 L2NormProbe::L2NormProbe() : AbstractNormProbe() { initialize_base(); }
16 L2NormProbe::L2NormProbe(
const char *name, HyPerCol *hc) : AbstractNormProbe() {
21 L2NormProbe::~L2NormProbe() {}
23 int L2NormProbe::initialize_base() {
28 int L2NormProbe::initialize(
const char *name, HyPerCol *hc) {
29 return AbstractNormProbe::initialize(name, hc);
34 ioParam_exponent(ioFlag);
39 parent->parameters()->ioParamValue(
40 ioFlag, name,
"exponent", &exponent, 1.0 ,
true );
44 assert(!parent->parameters()->presentAndNotBeenRead(name,
"exponent"));
45 int status = PV_SUCCESS;
46 if (exponent == 1.0) {
47 status = setNormDescriptionToString(
"L2-norm");
49 else if (exponent == 2.0) {
50 status = setNormDescriptionToString(
"L2-norm squared");
53 std::stringstream desc(
"(L2-norm)^");
55 status = setNormDescriptionToString(desc.str().c_str());
61 if (index < 0 || index >= parent->getNBatch()) {
64 PVLayerLoc const *loc = getTargetLayer()->getLayerLoc();
65 int const nx = loc->nx;
66 int const ny = loc->ny;
67 int const nf = loc->nf;
68 PVHalo const *halo = &loc->halo;
69 int const lt = halo->lt;
70 int const rt = halo->rt;
71 int const dn = halo->dn;
72 int const up = halo->up;
73 double l2normsq = 0.0;
74 float const *aBuffer =
75 getTargetLayer()->getLayerData() + index * getTargetLayer()->getNumExtended();
78 PVLayerLoc const *maskLoc = getMaskLayer()->getLayerLoc();
79 PVHalo const *maskHalo = &maskLoc->halo;
80 float const *maskLayerData =
81 getMaskLayer()->getLayerData()
82 + index * getMaskLayer()->getNumExtended();
85 int const maskLt = maskHalo->lt;
86 int const maskRt = maskHalo->rt;
87 int const maskDn = maskHalo->dn;
88 int const maskUp = maskHalo->up;
89 if (maskHasSingleFeature()) {
90 assert(getTargetLayer()->getNumNeurons() == nx * ny * nf);
92 #ifdef PV_USE_OPENMP_THREADS 93 #pragma omp parallel for reduction(+ : l2normsq) 94 #endif // PV_USE_OPENMP_THREADS 95 for (
int kxy = 0; kxy < nxy; kxy++) {
96 int kexMask = kIndexExtended(kxy, nx, ny, 1, maskLt, maskRt, maskDn, maskUp);
97 if (maskLayerData[kexMask]) {
98 int featureBase = kxy * nf;
99 for (
int f = 0; f < nf; f++) {
100 int kex = kIndexExtended(featureBase++, nx, ny, nf, lt, rt, dn, up);
101 l2normsq += pow((
double)aBuffer[kex], 2.0);
107 #ifdef PV_USE_OPENMP_THREADS 108 #pragma omp parallel for reduction(+ : l2normsq) 109 #endif // PV_USE_OPENMP_THREADS 110 for (
int k = 0; k < getTargetLayer()->getNumNeurons(); k++) {
111 int kex = kIndexExtended(k, nx, ny, nf, lt, rt, dn, up);
112 int kexMask = kIndexExtended(k, nx, ny, nf, maskLt, maskRt, maskDn, maskUp);
113 double val = aBuffer[kex];
114 l2normsq += (double)maskLayerData[kexMask] * val * val;
119 if (getTargetLayer()->getSparseFlag()) {
120 PVLayerCube cube = getTargetLayer()->getPublisher()->createCube();
121 long int numActive = cube.numActive[index];
122 int numItems = cube.numItems / cube.loc.nbatch;
125 #ifdef PV_USE_OPENMP_THREADS 126 #pragma omp parallel for reduction(+ : l2normsq) 127 #endif // PV_USE_OPENMP_THREADS 128 for (
int k = 0; k < numActive; k++) {
129 int extIndex = activeList[k].index;
130 int inRestricted = !extendedIndexInBorderRegion(
131 extIndex, nx, ny, nf, halo->lt, halo->rt, halo->dn, halo->up);
132 double val = inRestricted * fabs((
double)aBuffer[extIndex]);
133 l2normsq += pow(val, 2.0);
137 #ifdef PV_USE_OPENMP_THREADS 138 #pragma omp parallel for reduction(+ : l2normsq) 139 #endif // PV_USE_OPENMP_THREADS 140 for (
int k = 0; k < getTargetLayer()->getNumNeurons(); k++) {
141 int kex = kIndexExtended(k, nx, ny, nf, lt, rt, dn, up);
142 l2normsq += pow((
double)aBuffer[kex], 2.0);
152 if (exponent != 2.0) {
155 for (
int b = 0; b < numVals; b++) {
156 double v = valBuf[b];
157 valBuf[b] = pow(v, exponent / 2.0);
virtual void calcValues(double timeValue) override
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
virtual double getValueInternal(double timevalue, int index) override
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
virtual int setNormDescription() override
double * getValuesBuffer()
virtual void ioParam_exponent(enum ParamsIOFlag ioFlag)
exponent: The exponent on the L2-norm. getValue(t, index) returns (L2-Norm)^exponent.
virtual void calcValues(double timevalue) override