Exemplo n.º 1
0
void Util::sampleGaussianMat(MatrixXf& mat){
  for (int i = 0; i < mat.rows(); ++i){
    int j = 0;
    for ( ; j+1 < mat.cols(); j += 2){
      float f1, f2;
      sampleTwoGaussian(f1, f2);
      mat(i,j  ) = f1;
      mat(i,j+1) = f2;
    }
    for (; j < mat.cols(); j ++){
      float f1, f2;
      sampleTwoGaussian(f1, f2);
      mat(i, j)  = f1;
    }
  }
}
Exemplo n.º 2
0
    void
    GRBM<VIS_DIM, HID_DIM>::computeVisibleProb()
    {
      // before we actually compute anything here we need to ensure
      // that the hiddenProbs are binary
      for (int i = 0; i < HID_DIM; ++i)
        {
          hiddenProb(i) = hiddenProb(i) > RAND_0_1() ? 1. : 0.;
        }

      // we have several cases here for which the visible probabilities need to be computed:
      // 1) visible gaussian with learned stdev
      // 2) visible gaussian with added gaussian noise and fixed stdev
      // 3) visible gaussian withoud gaussian noise and fixed stdev
      // 4) visible binary
      if (!isVisibleBinary && isVisStdevLearned)
        {
          visibleProb = weights * hiddenProb;

          if (addGaussianNoise)
            {
              // add some gaussian noise to the visible units here
              g_float gn1, gn2;
              int i = 0;
              for (; i + 1 < visibleProb.size(); i += 2)
                {
                  sampleTwoGaussian(gn1, gn2);
                  visibleProb(i) += gn1;
                  visibleProb(i+1) += gn2;
                }
              for (; i < visibleProb.size(); ++i)
                {
                  sampleTwoGaussian(gn1, gn2);
                  visibleProb(i) += gn1;
                }
            }
          visibleProb.array() = visibleProb.array() * visStdevs.array();
        }
      else
        {
          if (!isVisibleBinary && addGaussianNoise)
            {
              sampleGaussianMat<VisVType>( visibleProb);
              // scale sampled matrix
              //visibleProb *= (1. / visStdev); // << done in next row
              visibleProb.array() += (visibleProb.array() / visStdev)
                  + (weights * hiddenProb).array() / visStdev;
            }
          else
            {
              visibleProb = weights * hiddenProb;
              if (!isVisibleBinary)
                visibleProb *= (1. / visStdev);
            }
        }

      // add bias / prior
      visibleProb += visiblebias;

      //std::cout << "vProb: " << std::endl <<  visibleProb.transpose() << std::endl << std::endl;
      // if visible should be binary threshold via logistic activation
      if (isVisibleBinary)
        {
          for (int i = 0; i < visibleProb.size(); ++i)
            visibleProb(i) = 1. / (1 + exp(-visibleProb(i)));
        }
    }