def test_random(self): test_f = 0 size = 20 domain = ["0", "1", "2", "3"] properties = ["P0", "P1", "P2", "P3"] binary_rels = ["R0", "R1", "R2", "R3"] form = fol.OpenFormula(domain, "P0(x)", ["x"]) feature = FeatureFormIndicatorType(form) label_fn = lambda d : feature.compute(d)[test_f] d = gram.fol.data.DataSet.make_random(size, domain, properties, binary_rels, label_fn) for i in range(size): f = d.get_data()[i].get_model().evaluate(form.get_form(), feature.get_token(test_f).get_closed_form().get_g()) l = d.get_data()[i].get_label() self.assertEqual(f, l) self.assertEqual(len(d.get_data()), size) form1 = fol.OpenFormula(domain, "P1(x)", ["x"]) feature1 = FeatureFormIndicatorType(form1) F = feat.FeatureSet() F.add_feature_type(feature1) fmat = feat.DataFeatureMatrix(d, F) fmat.extend([feature]) mat = fmat.get_matrix() for i in range(size): self.assertEqual(mat[i][feature1.get_size() + test_f], d.get_data()[i].get_label())
def test_ops(self): np.random.seed(2) domain = ["0", "1", "2", "3"] properties = ["P0", "P1"] binary_rels = [] form1 = fol.OpenFormula(domain, "P0(x)", ["x"]) form2 = fol.OpenFormula(domain, "P1(y)", ["y"]) c12 = form1.conjoin(form2) print form1.orthogonize(form2).get_exp() print form1.conjoin(form2, orthogonize=True).get_exp() print form1.conjoin(form2, orthogonize=False).get_exp() model = fol.RelationalModel.make_random(domain, properties, binary_rels) s1 = form1.satisfiers(model) s2 = form2.satisfiers(model) s12 = c12.satisfiers(model) print "Model:" print model._model print "P0(x)" print s1 print "P1(y)" print s2 print "P0(x) & P1(y)" print s12
def p_2_fn(cf): ofs = [] for var in cf.get_g(): ofs.append( fol.OpenFormula(domain, "P2(" + var + ")", [var], init_g=nltk.Assignment( domain, [(var, cf.get_g()[var])]))) return ofs
def disj_fn(cf1, cf2): cf2_o = cf2.orthogonize(cf1) new_g = cf2_o.get_g().copy() new_g.update(cf1.get_g()) return [ fol.OpenFormula(domain, str(cf1.get_exp() | cf2_o.get_exp()), new_g.keys(), init_g=new_g) ]
def exist_fn(cf): ofs = [] for var in cf.get_g(): new_g = cf.get_g().copy() del new_g[var] ofs.append( fol.OpenFormula(domain, "exists " + var + "." + cf.get_form(), new_g.keys(), init_g=new_g)) return ofs
def _make_atom_conjuncts(self, molecule_domain, includeHs): props = chem.ATOMIC_PROPERTIES_NOH if includeHs: props = chem.ATOMIC_PROPERTIES atom_conjs = [ fol.OpenFormula(molecule_domain, prop + "(x)", ["x"]) for prop in props ] return [atom_conjs]
def _make_bond_conjuncts(self, molecule_domain, atomic_relations, includeHs): props = chem.ATOMIC_PROPERTIES_NOH if includeHs: props = chem.ATOMIC_PROPERTIES atom_x_conjs = [ fol.OpenFormula(molecule_domain, prop + "(x)", ["x"]) for prop in props ] atom_y_conjs = [ fol.OpenFormula(molecule_domain, prop + "(y)", ["y"]) for prop in props ] bond_xy_conjs = [ fol.OpenFormula(molecule_domain, bond + "(x,y)", ["x", "y"]) for bond in atomic_relations ] return [atom_x_conjs, bond_xy_conjs, atom_y_conjs]
def _test_model(self, model_type, eta_0, evaluate): print "Starting test..." data_size = 3000 properties_n = 5 domain = ["0", "1", "2"] properties = [] F_full = feat.FeatureSet() F_relevant = feat.FeatureSet() F_0 = feat.FeatureSet() w = [] R = rule.RuleSet() feature = FeatureTopType() F_full.add_feature_type(feature) F_relevant.add_feature_type(feature) F_0.add_feature_type(feature) w.append(-1.0) for i in range(properties_n): prop = "P" + str(i) properties.append(prop) form = fol.OpenFormula(domain, prop + "(x)", ["x"]) feature = FeatureFormIndicatorType(form) F_full.add_feature_type(feature) token_relevant = feature.get_token(0) form_relevant = fol.OpenFormula( domain, prop + "(x)", ["x"], init_g=token_relevant.get_closed_form().get_g()) feature_relevant = FeatureFormIndicatorType(form_relevant) F_relevant.add_feature_type(feature_relevant) if i == 0: F_0.add_feature_type(feature) else: def p_fn(cf, i=i): ofs = [] for var in cf.get_g(): ofs.append( fol.OpenFormula(domain, "P" + str(i) + "(" + var + ")", [var], init_g=nltk.Assignment( domain, [(var, cf.get_g()[var])]))) return ofs cf = fol.ClosedFormula("P" + str(i - 1) + "(x)", nltk.Assignment(domain, [])) rP = UnaryRule(cf, p_fn) R.add_unary_rule(rP) w.append(.1 * 2.0**i) model_true = model_linear.PredictionModel.make(model_type, F=F_relevant, w=np.array(w)) label_fn = lambda d: model_true.predict(d) D = gram.fol.data.DataSet.make_random(data_size, domain, properties, [], label_fn) D_parts = D.split([0.8, 0.1, 0.1]) D_train = D_parts[0] D_dev = D_parts[1] eval_dev = evaluate.for_data(D_dev) modell1 = model_linear.PredictionModel.make(model_type) l1_hist = modell1.train_l1(D, F_full, iterations=140001, C=16.0, eta_0=eta_0, alpha=0.8, evaluation_fn=eval_dev) modell1_g = model_linear.PredictionModel.make(model_type) l1_g_hist = modell1_g.train_l1_g(D, F_0, R, t=0.04, iterations=140001, C=8.0, eta_0=eta_0, alpha=0.8, evaluation_fn=eval_dev) print "True model" print str(model_true) print "l1 model" print str(modell1) print "l1-g model" print str(modell1_g) print "l1 histories" self._print_table(l1_hist, "losses") self._print_table(l1_hist, "l1s") self._print_table(l1_hist, "nzs") self._print_table(l1_hist, "vals") print "l1-g histories" self._print_table(l1_g_hist, "losses") self._print_table(l1_g_hist, "l1s") self._print_table(l1_g_hist, "nzs") self._print_table(l1_g_hist, "vals")
def test_rules(self): domain = ["0", "1", "2"] properties = ["P0", "P1", "P2", "P3"] binary_rels = ["R0", "R1", "R2", "R3"] F = feat.FeatureSet() form0 = fol.OpenFormula(domain, "P0(x)", ["x"]) feature0 = FeatureFormIndicatorType(form0) F.add_feature_type(feature0) form1 = fol.OpenFormula(domain, "P1(x)", ["x"]) feature1 = FeatureFormIndicatorType(form1) F.add_feature_type(feature1) form2 = fol.OpenFormula(domain, "P2(x)", ["x"]) feature2 = FeatureFormIndicatorType(form2) #F.add_feature_type(feature2) rs = rule.RuleSet() # Binary rule conjunction def conj_fn(cf1, cf2): cf2_o = cf2.orthogonize(cf1) new_g = cf2_o.get_g().copy() new_g.update(cf1.get_g()) return [ fol.OpenFormula(domain, str(cf1.get_exp() & cf2_o.get_exp()), new_g.keys(), init_g=new_g) ] rConj = BinaryRule(None, None, conj_fn) rs.add_binary_rule(rConj) # Test conjunction F_tokens = [F.get_feature_token(i) for i in range(F.get_size())] for i in range(len(F_tokens)): self.assertTrue(F_tokens[i] is not None) output_forms = rs.apply(F_tokens) self.assertEquals(len(output_forms), len(F_tokens) * (len(F_tokens) - 1)) # Binary rule specific disjunction def disj_fn(cf1, cf2): cf2_o = cf2.orthogonize(cf1) new_g = cf2_o.get_g().copy() new_g.update(cf1.get_g()) return [ fol.OpenFormula(domain, str(cf1.get_exp() | cf2_o.get_exp()), new_g.keys(), init_g=new_g) ] cf0 = fol.ClosedFormula(form0.get_form(), nltk.Assignment(domain, [])) rDisj0 = BinaryRule(cf0, cf0, disj_fn) rs.add_binary_rule(rDisj0) output_forms = rs.apply(F_tokens) self.assertEquals( len(output_forms), len(F_tokens) * (len(F_tokens) - 1) + len(domain) * (len(domain) - 1)) # Unary rule introduce existential # Introducing exists through a string is probably the wrong way to do this def exist_fn(cf): ofs = [] for var in cf.get_g(): new_g = cf.get_g().copy() del new_g[var] ofs.append( fol.OpenFormula(domain, "exists " + var + "." + cf.get_form(), new_g.keys(), init_g=new_g)) return ofs rExistI = UnaryRule(None, exist_fn) rs.add_unary_rule(rExistI) # FIXME Maybe do later: Unary rule remove existential # Unary rule 0 => 2 def p_2_fn(cf): ofs = [] for var in cf.get_g(): ofs.append( fol.OpenFormula(domain, "P2(" + var + ")", [var], init_g=nltk.Assignment( domain, [(var, cf.get_g()[var])]))) return ofs rP0_2 = UnaryRule(cf0, p_2_fn) rs.add_unary_rule(rP0_2) output_forms = rs.apply(F_tokens) for i in range(len(output_forms)): self.assertTrue(output_forms[i] is not None)