コード例 #1
0
PropertySet MF::getProperties() const {
    PropertySet opts;
    opts.Set( "tol", props.tol );
    opts.Set( "maxiter", props.maxiter );
    opts.Set( "verbose", props.verbose );
    opts.Set( "damping", props.damping );
    return opts;
}
コード例 #2
0
ファイル: CBP.cpp プロジェクト: fhadiji/liftedgibbs
PropertySet CBP::getProperties() const {
    PropertySet opts;
    opts.Set( "tol", props.tol );
    opts.Set( "maxiter", props.maxiter );
    opts.Set( "verbose", props.verbose );
    opts.Set( "logdomain", props.logdomain );
    opts.Set( "updates", props.updates );
    opts.Set( "damping", props.damping );
    opts.Set( "inference", props.inference );
    return opts;
}
コード例 #3
0
ファイル: gi_libDAI.cpp プロジェクト: cjaques/ilastik-ssvm
void GI_libDAI::getMarginals(float* marginals,
                             const int label)
{
  // Set some constants
  Real   tol = 1e-7;
  //Real   tol = 1e-9;
  size_t verb = 0;
  //size_t verb = 3;
  size_t maxiter = 100;

  // Store the constants in a PropertySet object
  PropertySet opts;
#if LIBDAI_24
  opts.Set("maxiter",maxiter);  // Maximum number of iterations
  opts.Set("tol",tol);          // Tolerance for convergence
  opts.Set("verbose",verb);     // Verbosity (amount of output generated)
  opts.Set("logdomain",false);
  opts.Set("inference",string("MAXPROD"));
  opts.Set("updates",string("SEQFIX"));
#else
  opts.set("maxiter",maxiter);  // Maximum number of iterations
  opts.set("tol",tol);          // Tolerance for convergence
  opts.set("verbose",verb);     // Verbosity (amount of output generated)
  opts.set("logdomain",false);
  opts.set("inference",string("MAXPROD"));
  opts.set("updates",string("SEQFIX"));
#endif

  FactorGraph fg( factors.begin(), factors.end(), vars.begin(), vars.end(), factors.size(), vars.size() );
  BP bp(fg, opts);
  bp.init();
  bp.run();

  for(long sid = 0; sid < slice->getNbSupernodes(); sid++) {
    Factor f = bp.belief(fg.var(sid));
    marginals[sid] = f[label];
  }
}
コード例 #4
0
PropertySet ExactInf::getProperties() const {
    PropertySet opts;
    opts.Set( "verbose", props.verbose );
    return opts;
}
コード例 #5
0
/// Main function
int main( int argc, char *argv[] ) {
    try {
        // Variables for storing command line arguments
        size_t seed;
        size_t states = 2;
        string type;
        size_t d, N, K, k, j, n1, n2, n3, prime;
        bool periodic = false;
        FactorType ft;
        LDPCType ldpc;
        Real beta, sigma_w, sigma_th, mean_w, mean_th, noise;

        // Declare the supported options.
        po::options_description opts("General command line options");
        opts.add_options()
            ("help",     "produce help message")
            ("seed",     po::value<size_t>(&seed),   "random number seed (tries to read from /dev/urandom if not specified)")
            ("states",   po::value<size_t>(&states), "number of states of each variable (default=2 for binary variables)")
        ;

        // Graph structure options
        po::options_description opts_graph("Options for specifying graph structure");
        opts_graph.add_options()
            ("type",     po::value<string>(&type),   "factor graph type (one of 'FULL', 'DREG', 'LOOP', 'TREE', 'GRID', 'GRID3D', 'HOI', 'LDPC')")
            ("d",        po::value<size_t>(&d),      "variable connectivity (only for type=='DREG');\n\t<d><N> should be even")
            ("N",        po::value<size_t>(&N),      "number of variables (not for type=='GRID','GRID3D')")
            ("n1",       po::value<size_t>(&n1),     "width of grid (only for type=='GRID','GRID3D')")
            ("n2",       po::value<size_t>(&n2),     "height of grid (only for type=='GRID','GRID3D')")
            ("n3",       po::value<size_t>(&n3),     "length of grid (only for type=='GRID3D')")
            ("periodic", po::value<bool>(&periodic), "periodic grid? (only for type=='GRID','GRID3D'; default=0)")
            ("K",        po::value<size_t>(&K),      "number of factors (only for type=='HOI','LDPC')")
            ("k",        po::value<size_t>(&k),      "number of variables per factor (only for type=='HOI','LDPC')")
        ;

        // Factor options
        po::options_description opts_factors("Options for specifying factors");
        opts_factors.add_options()
            ("factors",  po::value<FactorType>(&ft), "factor type (one of 'EXPGAUSS','POTTS','ISING')")
            ("beta",     po::value<Real>(&beta),     "inverse temperature (ignored for factors=='ISING')")
            ("mean_w",   po::value<Real>(&mean_w),   "mean of pairwise interactions w_{ij} (only for factors=='ISING')")
            ("mean_th",  po::value<Real>(&mean_th),  "mean of unary interactions th_i (only for factors=='ISING')")
            ("sigma_w",  po::value<Real>(&sigma_w),  "stddev of pairwise interactions w_{ij} (only for factors=='ISING')")
            ("sigma_th", po::value<Real>(&sigma_th), "stddev of unary interactions th_i (only for factors=='ISING'")
        ;

        // LDPC options
        po::options_description opts_ldpc("Options for specifying LDPC code factor graphs");
        opts_ldpc.add_options()
            ("ldpc",     po::value<LDPCType>(&ldpc), "type of LDPC code (one of 'SMALL','GROUP','RANDOM')")
            ("j",        po::value<size_t>(&j),      "number of parity checks per bit (only for type=='LDPC')")
            ("noise",    po::value<Real>(&noise),    "bitflip probability for binary symmetric channel (only for type=='LDPC')")
            ("prime",    po::value<size_t>(&prime),  "prime number for construction of LDPC code (only for type=='LDPC' with ldpc='GROUP'))")
        ;

        // All options
        opts.add(opts_graph).add(opts_factors).add(opts_ldpc);

        // Parse command line arguments
        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, opts), vm);
        po::notify(vm);

        // Display help message if necessary
        if( vm.count("help") || !vm.count("type") ) {
            cout << "This program is part of libDAI - http://www.libdai.org/" << endl << endl;
            cout << "Usage: ./createfg [options]" << endl << endl;
            cout << "Creates a factor graph according to the specified options." << endl << endl;
            
            cout << endl << opts << endl;
            
            cout << "The following factor graph types with pairwise interactions can be created:" << endl;
            cout << "\t'FULL':   fully connected graph of <N> variables" << endl;
            cout << "\t'DREG':   random regular graph of <N> variables where each variable is connected with <d> others" << endl;
            cout << "\t'LOOP':   a single loop of <N> variables" << endl;
            cout << "\t'TREE':   random tree-structured (acyclic, connected) graph of <N> variables" << endl;
            cout << "\t'GRID':   2D grid of <n1>x<n2> variables" << endl;
            cout << "\t'GRID3D': 3D grid of <n1>x<n2>x<n3> variables" << endl;
            cout << "The following higher-order interactions factor graphs can be created:" << endl;
            cout << "\t'HOI':    random factor graph consisting of <N> variables and <K> factors," << endl;
            cout << "\t          each factor being an interaction of <k> variables." << endl;
            cout << "The following LDPC code factor graphs can be created:" << endl;
            cout << "\t'LDPC':   simulates LDPC decoding problem, using an LDPC code of <N> bits and <K>" << endl;
            cout << "\t          parity checks, with <k> bits per check and <j> checks per bit, transmitted" << endl;
            cout << "\t          on a binary symmetric channel with probability <noise> of flipping a bit." << endl;
            cout << "\t          The transmitted codeword has all bits set to zero. The argument 'ldpc'" << endl;
            cout << "\t          determines how the LDPC code is constructed: either using a group structure," << endl;
            cout << "\t          or randomly, or a fixed small code with (N,K,k,j) = (4,4,3,3)." << endl << endl;

            cout << "For all types except type=='LDPC', the factors have to be specified as well." << endl << endl;

            cout << "EXPGAUSS factors (the default) are created by drawing all log-factor entries" << endl;
            cout << "independently from a Gaussian with mean 0 and standard deviation <beta>." << endl << endl;
            
            cout << "In case of pairwise interactions, one can also choose POTTS factors, for which" << endl;
            cout << "the log-factors are simply delta functions multiplied by the strength <beta>." << endl << endl;

            cout << "For pairwise interactions and binary variables, one can also use ISING factors." << endl;
            cout << "Here variables x1...xN are assumed to be +1/-1--valued, and unary interactions" << endl;
            cout << "are of the form exp(th*xi) with th drawn from a Gaussian distribution with mean" << endl;
            cout << "<mean_th> and standard deviation <sigma_th>, and pairwise interactions are of the" << endl;
            cout << "form exp(w*xi*xj) with w drawn from a Gaussian distribution with mean <mean_w>" << endl;
            cout << "and standard deviation <sigma_w>." << endl;
            return 1;
        }

        // Set default number of states
        if( !vm.count("states") )
            states = 2;

        // Set default factor type
        if( !vm.count("factors") )
            ft = FactorType::EXPGAUSS;
        // Check validness of factor type
        if( ft == FactorType::POTTS )
            if( type == HOI_TYPE )
                throw "For factors=='POTTS', interactions should be pairwise (type!='HOI')";
        if( ft == FactorType::ISING )
            if( ((states != 2) || (type == HOI_TYPE)) )
                throw "For factors=='ISING', variables should be binary (states==2) and interactions should be pairwise (type!='HOI')";

        // Read random seed
        if( !vm.count("seed") ) {
            ifstream infile;
            bool success;
            infile.open( "/dev/urandom" );
            success = infile.is_open();
            if( success ) {
                infile.read( (char *)&seed, sizeof(size_t) / sizeof(char) );
                success = infile.good();
                infile.close();
            }
            if( !success )
                throw "Please specify random number seed.";
        }
        rnd_seed( seed );

        // Set default periodicity
        if( !vm.count("periodic") )
            periodic = false;

        // Store some options in a PropertySet object
        PropertySet options;
        if( vm.count("mean_th") )
            options.Set("mean_th", mean_th);
        if( vm.count("sigma_th") )
            options.Set("sigma_th", sigma_th);
        if( vm.count("mean_w") )
            options.Set("mean_w", mean_w);
        if( vm.count("sigma_w") )
            options.Set("sigma_w", sigma_w);
        if( vm.count("beta") )
            options.Set("beta", beta);

        // Output some comments
        cout << "# Factor graph made by " << argv[0] << endl;
        cout << "# type = " << type << endl;
        cout << "# states = " << states << endl;

        // The factor graph to be constructed
        FactorGraph fg;

#define NEED_ARG(name, desc) do { if(!vm.count(name)) throw "Please specify " desc " with --" name; } while(0);

        if( type == FULL_TYPE || type == DREG_TYPE || type == LOOP_TYPE || type == TREE_TYPE || type == GRID_TYPE || type == GRID3D_TYPE ) {
            // Pairwise interactions

            // Check command line options
            if( type == GRID_TYPE ) {
                NEED_ARG("n1", "width of grid");
                NEED_ARG("n2", "height of grid");
                N = n1 * n2;
            } else if( type == GRID3D_TYPE ) {
                NEED_ARG("n1", "width of grid");
                NEED_ARG("n2", "height of grid");
                NEED_ARG("n3", "depth of grid");
                N = n1 * n2 * n3;
            } else
                NEED_ARG("N", "number of variables");

            if( states > 2 || ft == FactorType::POTTS ) {
                NEED_ARG("beta", "stddev of log-factor entries");
            } else {
                NEED_ARG("mean_w", "mean of pairwise interactions");
                NEED_ARG("mean_th", "mean of unary interactions");
                NEED_ARG("sigma_w", "stddev of pairwise interactions");
                NEED_ARG("sigma_th", "stddev of unary interactions");
            }

            if( type == DREG_TYPE )
                NEED_ARG("d", "connectivity (number of neighboring variables of each variable)");

            // Build pairwise interaction graph
            GraphAL G;
            if( type == FULL_TYPE )
                G = createGraphFull( N );
            else if( type == DREG_TYPE )
                G = createGraphRegular( N, d );
            else if( type == LOOP_TYPE )
                G = createGraphLoop( N );
            else if( type == TREE_TYPE )
                G = createGraphTree( N );
            else if( type == GRID_TYPE )
                G = createGraphGrid( n1, n2, periodic );
            else if( type == GRID3D_TYPE )
                G = createGraphGrid3D( n1, n2, n3, periodic );

            // Construct factor graph from pairwise interaction graph
            fg = createFG( G, ft, states, options );

            // Output some additional comments
            if( type == GRID_TYPE || type == GRID3D_TYPE ) {
                cout << "# n1 = " << n1 << endl;
                cout << "# n2 = " << n2 << endl;
                if( type == GRID3D_TYPE )
                    cout << "# n3 = " << n3 << endl;
            }
            if( type == DREG_TYPE )
                cout << "# d = " << d << endl;
            cout << "# options = " << options << endl;
        } else if( type == HOI_TYPE ) {
            // Higher order interactions

            // Check command line arguments
            NEED_ARG("N", "number of variables");
            NEED_ARG("K", "number of factors");
            NEED_ARG("k", "number of variables per factor");
            NEED_ARG("beta", "stddev of log-factor entries");

            // Create higher-order interactions factor graph
            do {
                fg = createHOIFG( N, K, k, beta );
            } while( !fg.isConnected() );

            // Output some additional comments
            cout << "# K = " << K << endl;
            cout << "# k = " << k << endl;
            cout << "# beta = " << beta << endl;
        } else if( type == LDPC_TYPE ) {
            // LDPC codes

            // Check command line arguments
            NEED_ARG("ldpc", "type of LDPC code");
            NEED_ARG("noise", "bitflip probability for binary symmetric channel");

            // Check more command line arguments (seperately for each LDPC type)
            if( ldpc == LDPCType::RANDOM ) {
                NEED_ARG("N", "number of variables");
                NEED_ARG("K", "number of factors");
                NEED_ARG("k", "number of variables per factor");
                NEED_ARG("j", "number of parity checks per bit");
                if( N * j != K * k )
                    throw "Parameters should satisfy N * j == K * k";
            } else if( ldpc == LDPCType::GROUP ) {
                NEED_ARG("prime", "prime number");
                NEED_ARG("k", "number of variables per factor");
                NEED_ARG("j", "number of parity checks per bit");

                if( !isPrime(prime) )
                    throw "Parameter <prime> should be prime";
                if( !((prime-1) % j == 0 ) )
                    throw "Parameters should satisfy (prime-1) % j == 0";
                if( !((prime-1) % k == 0 ) )
                    throw "Parameters should satisfy (prime-1) % k == 0";

                N = prime * k;
                K = prime * j;
            } else if( ldpc == LDPCType::SMALL ) {
                N = 4;
                K = 4;
                j = 3;
                k = 3;
            }

            // Output some additional comments
            cout << "# N = " << N << endl;
            cout << "# K = " << K << endl;
            cout << "# j = " << j << endl;
            cout << "# k = " << k << endl;
            if( ldpc == LDPCType::GROUP )
                cout << "# prime = " << prime << endl;
            cout << "# noise = " << noise << endl;

            // Construct likelihood and paritycheck factors
            Real likelihood[4] = {1.0 - noise, noise, noise, 1.0 - noise};
            Real *paritycheck = new Real[1 << k];
            createParityCheck(paritycheck, k, 0.0);

            // Create LDPC structure
            BipartiteGraph ldpcG;
            bool regular;
            do {
                if( ldpc == LDPCType::GROUP )
                    ldpcG = createGroupStructuredLDPCGraph( prime, j, k );
                else if( ldpc == LDPCType::RANDOM )
                    ldpcG = createRandomBipartiteGraph( N, K, j, k );
                else if( ldpc == LDPCType::SMALL )
                    ldpcG = createSmallLDPCGraph();

                regular = true;
                for( size_t i = 0; i < N; i++ )
                    if( ldpcG.nb1(i).size() != j )
                        regular = false;
                for( size_t I = 0; I < K; I++ )
                    if( ldpcG.nb2(I).size() != k )
                        regular = false;
            } while( !regular && !ldpcG.isConnected() );

            // Convert to FactorGraph
            vector<Factor> factors;
            for( size_t I = 0; I < K; I++ ) {
                VarSet vs;
                for( size_t _i = 0; _i < k; _i++ ) {
                    size_t i = ldpcG.nb2(I)[_i];
                    vs |= Var( i, 2 );
                }
                factors.push_back( Factor( vs, paritycheck ) );
            }
            delete paritycheck;

            // Generate noise vector
            vector<char> noisebits(N,0);
            size_t bitflips = 0;
            for( size_t i = 0; i < N; i++ ) {
                if( rnd_uniform() < noise ) {
                    noisebits[i] = 1;
                    bitflips++;
                }
            }
            cout << "# bitflips = " << bitflips << endl;

            // Simulate transmission of all-zero codeword
            vector<char> input(N,0);
            vector<char> output(N,0);
            for( size_t i = 0; i < N; i++ )
                output[i] = (input[i] + noisebits[i]) & 1;

            // Add likelihoods
            for( size_t i = 0; i < N; i++ )
               factors.push_back( Factor(Var(i,2), likelihood + output[i]*2) );

            // Construct Factor Graph
            fg = FactorGraph( factors );
        } else
            throw "Invalid type";

        // Output additional comments
        cout << "# N = " << fg.nrVars() << endl;
        cout << "# seed = " << seed << endl;

        // Output factor graph
        cout << fg;
    } catch( const char *e ) {
        /// Display error message
        cerr << "Error: " << e << endl;
        return 1;
    }

    return 0;
}
コード例 #6
0
ファイル: gi_libDAI.cpp プロジェクト: cjaques/ilastik-ssvm
/**
 * Run BP on a given factor graph
 * @param nodeLabelsBP node inferred by BP
 */
double GI_libDAI::run(labelType* inferredLabels,
                      int id,
                      size_t maxiter,
                      labelType* nodeLabelsGroundTruth,
                      bool computeEnergyAtEachIteration,
                      double* _loss)
{
  double energy = 0;
  double loss = 0;

  string paramMSRC;
  Config::Instance()->getParameter("msrc", paramMSRC);
  bool useMSRC = paramMSRC.c_str()[0] == '1';
  bool replaceVoidMSRC = false;
  if(useMSRC) {
    Config::Instance()->getParameter("msrc_replace_void", paramMSRC);
    replaceVoidMSRC = paramMSRC.c_str()[0] == '1';
  }

  // Set some constants
  Real   tol = 1e-7;
  //Real   tol = 1e-9;
  size_t verb = 0;
  //size_t verb = 3;

  // Store the constants in a PropertySet object
  PropertySet opts;
#if LIBDAI_24
  opts.Set("maxiter",maxiter);  // Maximum number of iterations
  opts.Set("tol",tol);          // Tolerance for convergence
  opts.Set("verbose",verb);     // Verbosity (amount of output generated)
  opts.Set("logdomain",false);
  opts.Set("inference",string("MAXPROD"));
  opts.Set("updates",string("SEQFIX"));
#else
  opts.set("maxiter",maxiter);  // Maximum number of iterations
  opts.set("tol",tol);          // Tolerance for convergence
  opts.set("verbose",verb);     // Verbosity (amount of output generated)
  opts.set("logdomain",false);
  opts.set("inference",string("MAXPROD"));
  opts.set("updates",string("SEQFIX"));
#endif

  FactorGraph fg( factors.begin(), factors.end(), vars.begin(), vars.end(), factors.size(), vars.size() );

  // Construct a BP (belief propagation) object from the FactorGraph fg
  // using the parameters specified by opts and two additional properties,
  // specifying the type of updates the BP algorithm should perform and
  // whether they should be done in the real or in the logdomain
  //BP bp(fg, opts("updates",string("SEQFIX"))("logdomain",true));
  //BP bp(fg, opts("updates",string("SEQFIX"))("logdomain",false));
  //BP bp(fg, opts("updates",string("SEQFIX"))("logdomain",false)("inference",string("MAXPROD")));
  BP bp(fg, opts);
  // Initialize belief propagation algorithm
  bp.init();

  vector<std::size_t> labels;

  // Run belief propagation algorithm
  if(computeEnergyAtEachIteration) {

#if OUTPUT_ENERGY
      // one file per example
      stringstream sEnergyMaxFile;
      sEnergyMaxFile << "x_" << id;
      sEnergyMaxFile << "_energyBPMax.txt";

      ofstream ofsEnergyMax(sEnergyMaxFile.str().c_str(),ios::app);
#endif

      int i = maxiter;
      //for(int i = 1; i <= (int)maxiter; i+=10) // loop to see how energy evolves
        {
          //INFERENCE_PRINT("[gi_libDAI] BP Iteration %d\n", i);
#if LIBDAI_24
          opts.Set("maxiter",(size_t)i);  // Maximum number of iterations
#else
          opts.set("maxiter",(size_t)i);  // Maximum number of iterations
#endif
          bp.setProperties(opts);
          //Real maxDiff = bp.run();
          bp.run();

          labels = bp.findMaximum();

          if(replaceVoidMSRC) {
            if(lossPerLabel == 0) {
              copyLabels_MSRC(labels, inferredLabels, bp, fg);                
            } else {
              copy(labels.begin(),labels.end(),inferredLabels);
            }
          } else {
            copy(labels.begin(),labels.end(),inferredLabels);
          }

          energy = GraphInference::computeEnergy(inferredLabels);

#if OUTPUT_ENERGY
          ofsEnergyMax << energy << " " << loss << endl;
#endif

          //if( maxDiff < tol )
          //  break;
        }

#if OUTPUT_ENERGY
        ofsEnergyMax.close();
#endif

  } else {
    bp.run();
    labels = bp.findMaximum();

    if(replaceVoidMSRC) {
      if(lossPerLabel == 0) {
	copyLabels_MSRC(labels,
			inferredLabels,
			bp,
			fg);
      } else {
	copy(labels.begin(),labels.end(),inferredLabels);
      }
    } else {
      copy(labels.begin(),labels.end(),inferredLabels);
    }

    energy = GraphInference::computeEnergy(inferredLabels);
  }

  if(_loss) {
    *_loss = loss;
  }

  return energy;
}