CTinRay / md-hw2

0 stars 0 forks source link

Construct Factor Graph #1

Open CTinRay opened 7 years ago

CTinRay commented 7 years ago

You have two function class (for h(y), not defined yet)


class CCFactorFunction: FactorFunction{
public:
    Real beta;
    CCFactorFunction(Real beta);
    Real eval(const std::vector<Label>&args) const{
        return expl(beta * args[0] * args[1]);
    }
};

class PriorFactorFunction: FactorFunction{
    static std::vector<Real>alpha;
    std::vector<Real>&features;
    PriorFactorFunction(const std::vector<Real>&features);
    Real eval(const std::vector<Label>&args) const{
        return expl(args[0] * (alpha[0] * features[0] +
                               alpha[1] * features[1] +
                               alpha[2] * features[2] ) );
    }
};

And one function to add factor

    void addFactor(const std::vector<unsigned int>&scope,
                   const FactorFunction&factorFunction);
CTinRay commented 7 years ago

You may want to implement function

void ConstructGraph(FactorGraph& graph);
CTinRay commented 7 years ago

Update class defs. I will push it to github latter.

class GFactorFunction: FactorFunction{
public:
    Real beta;
    GFactorFunction(Real beta);
    Real eval(const std::vector<Label>&args) const{
        return expl(beta * args[0] * args[1]);
    }
};

class FFactorFunction: FactorFunction{
    static std::vector<Real>alpha;
    std::vector<Real>&features;
    FFactorFunction(const std::vector<Real>&features);
    Real eval(const std::vector<Label>&args) const{
        return expl(args[0] * (alpha[0] * features[0] +
                               alpha[1] * features[1] +
                               alpha[2] * features[2] ) );
    }
};

class HFactorFunction: FactorFunction{
    static Real gamma;
    unsigned int ti;
    std::vector<Real>&features;
    HFactorFunction(unsigned int ti);
    Real eval(const std::vector<Label>&args) const{
        unsigned int count = 0;
        for (unsigned int i = 0; i < args.size(); ++i ){
            count += args[i] == 1 ? 1 : 0;
        }
        return expl(gamma * (ti - count) * (ti - count));
    }
};
CTinRay commented 7 years ago

If you have done, tell me how to use your function(s) to construct graph here.

And make pull request as well, I will merge you into master.