You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

kb.py 3.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import openml
  2. from z3 import If, Implies, Int, Not, Solver, Sum, sat # noqa: F401
  3. from abl.reasoning import KBBase
  4. class ZooKB(KBBase):
  5. def __init__(self):
  6. super().__init__(pseudo_label_list=list(range(7)), use_cache=False)
  7. self.solver = Solver()
  8. # Load information of Zoo dataset
  9. dataset = openml.datasets.get_dataset(dataset_id = 62, download_data=False, download_qualities=False, download_features_meta_data=False)
  10. X, y, categorical_indicator, attribute_names = dataset.get_data(target=dataset.default_target_attribute)
  11. self.attribute_names = attribute_names
  12. self.target_names = y.cat.categories.tolist()
  13. # print("Attribute names are: ", self.attribute_names)
  14. # print("Target names are: ", self.target_names)
  15. # self.attribute_names = ["hair", "feathers", "eggs", "milk", "airborne", "aquatic", "predator", "toothed", "backbone", "breathes", "venomous", "fins", "legs", "tail", "domestic", "catsize"]
  16. # self.target_names = ["mammal", "bird", "reptile", "fish", "amphibian", "insect", "invertebrate"]
  17. # Define variables
  18. for name in self.attribute_names+self.target_names:
  19. exec(f"globals()['{name}'] = Int('{name}')") ## or use dict to create var and modify rules
  20. # Define rules
  21. rules = [
  22. Implies(milk == 1, mammal == 1),
  23. Implies(mammal == 1, milk == 1),
  24. Implies(mammal == 1, backbone == 1),
  25. Implies(mammal == 1, breathes == 1),
  26. Implies(feathers == 1, bird == 1),
  27. Implies(bird == 1, feathers == 1),
  28. Implies(bird == 1, eggs == 1),
  29. Implies(bird == 1, backbone == 1),
  30. Implies(bird == 1, breathes == 1),
  31. Implies(bird == 1, legs == 2),
  32. Implies(bird == 1, tail == 1),
  33. Implies(reptile == 1, backbone == 1),
  34. Implies(reptile == 1, breathes == 1),
  35. Implies(reptile == 1, tail == 1),
  36. Implies(fish == 1, aquatic == 1),
  37. Implies(fish == 1, toothed == 1),
  38. Implies(fish == 1, backbone == 1),
  39. Implies(fish == 1, Not(breathes == 1)),
  40. Implies(fish == 1, fins == 1),
  41. Implies(fish == 1, legs == 0),
  42. Implies(fish == 1, tail == 1),
  43. Implies(amphibian == 1, eggs == 1),
  44. Implies(amphibian == 1, aquatic == 1),
  45. Implies(amphibian == 1, backbone == 1),
  46. Implies(amphibian == 1, breathes == 1),
  47. Implies(amphibian == 1, legs == 4),
  48. Implies(insect == 1, eggs == 1),
  49. Implies(insect == 1, Not(backbone == 1)),
  50. Implies(insect == 1, legs == 6),
  51. Implies(invertebrate == 1, Not(backbone == 1))
  52. ]
  53. # Define weights and sum of violated weights
  54. self.weights = {rule: 1 for rule in rules}
  55. self.total_violation_weight = Sum([If(Not(rule), self.weights[rule], 0) for rule in self.weights])
  56. def logic_forward(self, pseudo_label, data_point):
  57. attribute_names, target_names = self.attribute_names, self.target_names
  58. solver = self.solver
  59. total_violation_weight = self.total_violation_weight
  60. pseudo_label, data_point = pseudo_label[0], data_point[0]
  61. self.solver.reset()
  62. for name, value in zip(attribute_names, data_point):
  63. solver.add(eval(f"{name} == {value}"))
  64. for cate, name in zip(self.pseudo_label_list,target_names):
  65. value = 1 if (cate == pseudo_label) else 0
  66. solver.add(eval(f"{name} == {value}"))
  67. if solver.check() == sat:
  68. model = solver.model()
  69. total_weight = model.evaluate(total_violation_weight)
  70. return total_weight.as_long()
  71. else:
  72. # No solution found
  73. return 1e10

An efficient Python toolkit for Abductive Learning (ABL), a novel paradigm that integrates machine learning and logical reasoning in a unified framework.