Exemplo n.º 1
0
 def load_inference(self, method):
     if method in self.method2inference:
         self.method = method
         self.inference = self.method2inference[method]
     else:
         mthdname2props = dai.parseNameProperties(method,
                                                  self.method_aliases)
         alg = dai.newInfAlg(mthdname2props.first, self.dai_factor_graph,
                             mthdname2props.second)
         self.method2props[method] = mthdname2props
         self.method2inference[method] = alg
         self.method = method
         self.inference = alg
Exemplo n.º 2
0
    def get_inf(self, name, **kwds):
        """
        cls, config = network_toolkit.dai_conf.config_map[name]
        prop = dai.PropertySet()
        for c in config:
            prop[c] = config[c]
        if verbose:
            prop["verbose"] = "1"
        sn = dai.FactorGraph(self.vecfac)
        inf = cls(sn, prop)
        return inf
        """

        sn = self.get_factor_graph()
        prop = dai.PropertySet()
        for k in kwds:
            prop[k] = str(kwds[k])
        inf_alg = dai.newInfAlg(name, sn, prop)
        return inf_alg
Exemplo n.º 3
0
    def get_inf(self, name, **kwds):
        """
        cls, config = network_toolkit.dai_conf.config_map[name]
        prop = dai.PropertySet()
        for c in config:
            prop[c] = config[c]
        if verbose:
            prop["verbose"] = "1"
        sn = dai.FactorGraph(self.vecfac)
        inf = cls(sn, prop)
        return inf
        """

        sn = self.get_factor_graph()
        prop = dai.PropertySet()
        for k in kwds:
            prop[k] = str(kwds[k])
        inf_alg = dai.newInfAlg(name, sn, prop )
        return inf_alg
Exemplo n.º 4
0
    def run(self, method_name, pseudo_count=0.1, **kwds):
        self.dai_factor_graph.setup_factor_graph()
        obsvec = dai.ObservationVec()
        for sample in self.evidence_map:
            obs = dai.Observation()
            for var in self.evidence_map[sample]:
                #obs[ self.factor_graph.var_map[var].dai_variable ] = self.evidence_map[sample][var]
                #obs[ var.get_dai_value() ] = self.evidence_map[sample][var]
                obs[self.dai_factor_graph.
                    dai_variables[var]] = self.evidence_map[sample][var]
            obsvec.append(obs)

        sp_vec = dai.VecSharedParameters()
        for sp in self.shared_param_list:
            fo = dai.FactorOrientations()
            for variable_list, factor in sp.shared_list:
                vec = dai.VecVar()
                for v in variable_list:
                    vec.append(self.dai_factor_graph.dai_variables[v])
                fo[factor.factor_id] = vec

            total_dim = 1
            for d in sp.variable_dims:
                total_dim *= d
            #props = dai.PropertySet()
            #props["total_dim"] = str(total_dim)
            #props["target_dim"] = str(sp.variable_dims[0])
            #pe = dai.ParameterEstimation.construct("CondProbEstimation", props)
            pseudo_counts = dai.Prob(total_dim, pseudo_count)
            #for i in range(total_dim):
            #    pseudo_count.append( 0.01 )
            pe = dai.CondProbEstimation(sp.variable_dims[0], pseudo_counts)
            dai_sp = dai.SharedParameters(fo, pe)
            sp_vec.append(dai_sp)

        max_step = dai.MaximizationStep(sp_vec)
        vec_max_step = dai.VecMaximizationStep()
        vec_max_step.append(max_step)
        evidence = dai.Evidence(obsvec)
        #inf_alg = self.factor_graph.get_inf_bp(verbose=True)
        prop = dai.PropertySet()
        for k in kwds:
            prop[k] = str(kwds[k])

        inf_alg = dai.newInfAlg(method_name,
                                self.dai_factor_graph.get_factor_graph(), prop)
        em_props = dai.PropertySet()
        dai_em_alg = dai.EMAlg(evidence, inf_alg, vec_max_step, em_props)

        while not dai_em_alg.hasSatisfiedTermConditions():
            print "Cycle 1"
            l = dai_em_alg.iterate()
            if kwds.get("verbose", False):
                print "Iteration ", dai_em_alg.Iterations(), " likelihood: ", l
                for max_step in dai_em_alg:
                    print max_step, len(max_step)
                    for shared_param in max_step:
                        print "\t", shared_param.currentExpectations()
                        print "\t", shared_param.getPEst().estimate(
                            shared_param.currentExpectations())

        for i in range(len(dai_em_alg[0])):
            shared_param = dai_em_alg[0][i]
            param_vec = shared_param.getPEst().estimate(
                shared_param.currentExpectations())
            var_list = self.shared_param_list[i].variable_set_labels
            var_dims = self.shared_param_list[i].variable_dims
            self.shared_param_list[i].result = param_vec
Exemplo n.º 5
0
    def run(self, method_name, pseudo_count=0.1, **kwds):
        self.dai_factor_graph.setup_factor_graph()
        obsvec = dai.ObservationVec()
        for sample in self.evidence_map:
            obs = dai.Observation()
            for var in self.evidence_map[sample]:
                #obs[ self.factor_graph.var_map[var].dai_variable ] = self.evidence_map[sample][var]
                #obs[ var.get_dai_value() ] = self.evidence_map[sample][var]
                obs[ self.dai_factor_graph.dai_variables[var] ] = self.evidence_map[sample][var]
            obsvec.append(obs)

        sp_vec = dai.VecSharedParameters()
        for sp in self.shared_param_list:
            fo = dai.FactorOrientations()
            for variable_list, factor in sp.shared_list:
                vec = dai.VecVar()
                for v in variable_list:
                    vec.append(self.dai_factor_graph.dai_variables[v])
                fo[factor.factor_id] = vec

            total_dim = 1
            for d in sp.variable_dims:
                total_dim *= d
            #props = dai.PropertySet()
            #props["total_dim"] = str(total_dim)
            #props["target_dim"] = str(sp.variable_dims[0])
            #pe = dai.ParameterEstimation.construct("CondProbEstimation", props)
            pseudo_counts = dai.Prob(total_dim, pseudo_count)
            #for i in range(total_dim):
            #    pseudo_count.append( 0.01 )
            pe = dai.CondProbEstimation(sp.variable_dims[0], pseudo_counts)
            dai_sp = dai.SharedParameters(fo, pe)
            sp_vec.append(dai_sp)

        max_step = dai.MaximizationStep(sp_vec)
        vec_max_step = dai.VecMaximizationStep()
        vec_max_step.append(max_step)
        evidence = dai.Evidence(obsvec)
        #inf_alg = self.factor_graph.get_inf_bp(verbose=True)
        prop = dai.PropertySet()
        for k in kwds:
            prop[k] = str(kwds[k])

        inf_alg = dai.newInfAlg(method_name, self.dai_factor_graph.get_factor_graph(), prop )
        em_props= dai.PropertySet()
        dai_em_alg = dai.EMAlg(evidence, inf_alg, vec_max_step, em_props)

        while not dai_em_alg.hasSatisfiedTermConditions():
            print "Cycle 1"
            l = dai_em_alg.iterate()
            if kwds.get("verbose", False):
                print "Iteration ", dai_em_alg.Iterations(), " likelihood: ", l
                for max_step in dai_em_alg:
                    print max_step, len(max_step)
                    for shared_param in max_step:
                        print "\t", shared_param.currentExpectations()
                        print "\t", shared_param.getPEst().estimate(shared_param.currentExpectations())
        
        for i in range(len(dai_em_alg[0])):
            shared_param = dai_em_alg[0][i]
            param_vec = shared_param.getPEst().estimate(shared_param.currentExpectations())
            var_list = self.shared_param_list[i].variable_set_labels
            var_dims = self.shared_param_list[i].variable_dims
            self.shared_param_list[i].result = param_vec