PetaVision  Alpha
InitUniformRandomWeights.cpp
1 /*
2  * InitUniformRandomWeights.cpp
3  *
4  * Created on: Aug 9, 2011
5  * Author: kpeterson
6  */
7 
8 #include "InitUniformRandomWeights.hpp"
9 
10 namespace PV {
11 
12 InitUniformRandomWeights::InitUniformRandomWeights(char const *name, HyPerCol *hc) {
13  initialize(name, hc);
14 }
15 
16 InitUniformRandomWeights::InitUniformRandomWeights() {}
17 
18 InitUniformRandomWeights::~InitUniformRandomWeights() {}
19 
20 int InitUniformRandomWeights::initialize(char const *name, HyPerCol *hc) {
21  int status = InitRandomWeights::initialize(name, hc);
22  return status;
23 }
24 
25 int InitUniformRandomWeights::ioParamsFillGroup(enum ParamsIOFlag ioFlag) {
26  int status = InitRandomWeights::ioParamsFillGroup(ioFlag);
27  ioParam_wMinInit(ioFlag);
28  ioParam_wMaxInit(ioFlag);
29  ioParam_sparseFraction(ioFlag);
30  ioParam_minNNZ(ioFlag);
31  return status;
32 }
33 
34 void InitUniformRandomWeights::ioParam_wMinInit(enum ParamsIOFlag ioFlag) {
35  parent->parameters()->ioParamValue(ioFlag, name, "wMinInit", &mWMin, mWMin);
36 }
37 
38 void InitUniformRandomWeights::ioParam_wMaxInit(enum ParamsIOFlag ioFlag) {
39  parent->parameters()->ioParamValue(ioFlag, name, "wMaxInit", &mWMax, mWMax);
40 }
41 
42 void InitUniformRandomWeights::ioParam_sparseFraction(enum ParamsIOFlag ioFlag) {
43  parent->parameters()->ioParamValue(
44  ioFlag, name, "sparseFraction", &mSparseFraction, mSparseFraction);
45 }
46 
47 void InitUniformRandomWeights::ioParam_minNNZ(enum ParamsIOFlag ioFlag) {
48  parent->parameters()->ioParamValue(ioFlag, name, "minNNZ", &mMinNNZ, mMinNNZ);
49 }
50 
55 void InitUniformRandomWeights::randomWeights(float *patchDataStart, int patchIndex) {
56  double p;
57  if (mWMax <= mWMin) {
58  if (mWMax < mWMin) {
59  WarnLog().printf(
60  "uniformWeights maximum less than minimum. Changing max = %f to min value of %f\n",
61  (double)mWMax,
62  (double)mWMin);
63  mWMax = mWMin;
64  }
65  p = 0.0;
66  }
67  else {
68  p = (double)(mWMax - mWMin) / (1.0 + (double)CL_RANDOM_MAX);
69  }
70  float sparseFraction = mSparseFraction * (float)(1.0 + (double)CL_RANDOM_MAX);
71 
72  // loop over all post-synaptic cells in patch
73 
74  const int nxp = mWeights->getPatchSizeX();
75  const int nyp = mWeights->getPatchSizeY();
76  const int nfp = mWeights->getPatchSizeF();
77  const int patchSize = nxp * nyp * nfp;
78 
79  // Force a minimum number of nonzero weights
80  int zeroesLeft = patchSize - mMinNNZ;
81 
82  // Start from a random index so that we don't always run out of zeros in the same place
83  int startIndex = 0;
84 
85  // This line ensures we create the same weight patches for minNNZ = 0 as we did before
86  if (mMinNNZ != 0) {
87  startIndex = mRandState->randomUInt(patchIndex) % patchSize;
88  }
89 
90  for (int n = 0; n < patchSize; n++) {
91  float data = (mWMin + (float)(p * (double)mRandState->randomUInt(patchIndex)));
92  if (zeroesLeft > 0 && (double)mRandState->randomUInt(patchIndex) < (double)sparseFraction) {
93  data = 0.0f;
94  --zeroesLeft;
95  }
96  patchDataStart[(n + startIndex) % patchSize] = data;
97  }
98 }
99 
100 } /* namespace PV */
int getPatchSizeX() const
Definition: Weights.hpp:219
void randomWeights(float *patchDataStart, int patchIndex) override
int getPatchSizeY() const
Definition: Weights.hpp:222
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
Definition: InitWeights.cpp:39
int getPatchSizeF() const
Definition: Weights.hpp:225