diff --git a/knowledge_FOIL.ipynb b/knowledge_FOIL.ipynb
index e06f5abf1..63e943416 100644
--- a/knowledge_FOIL.ipynb
+++ b/knowledge_FOIL.ipynb
@@ -38,7 +38,7 @@
"source": [
"## OVERVIEW\n",
"\n",
- "Like the [learning module](https://github.com/aimacode/aima-python/blob/master/learning.ipynb), this chapter focuses on methods for generating a model/hypothesis for a domain. Unlike though the learning chapter, here we use prior knowledge to help us learn from new experiences and find a proper hypothesis.\n",
+ "Like the [learning module](https://github.com/aimacode/aima-python/blob/master/learning.ipynb), this chapter focuses on methods for generating a model/hypothesis for a domain; however, unlike the learning chapter, here we use prior knowledge to help us learn from new experiences and to find a proper hypothesis.\n",
"\n",
"### First-Order Logic\n",
"\n",
@@ -46,7 +46,7 @@
"\n",
"### Representation\n",
"\n",
- "In this module, we use dictionaries to represent examples, with keys the attribute names and values the corresponding example values. Examples also have an extra boolean field, 'GOAL', for the goal predicate. A hypothesis is represented as a list of dictionaries. Each dictionary in that list represents a disjunction. Inside these dictionaries/disjunctions we have conjunctions.\n",
+ "In this module, we use dictionaries to represent examples, with keys being the attribute names and values being the corresponding example values. Examples also have an extra boolean field, 'GOAL', for the goal predicate. A hypothesis is represented as a list of dictionaries. Each dictionary in that list represents a disjunction. Inside these dictionaries/disjunctions we have conjunctions.\n",
"\n",
"For example, say we want to predict if an animal (cat or dog) will take an umbrella given whether or not it rains or the animal wears a coat. The goal value is 'take an umbrella' and is denoted by the key 'GOAL'. An example:\n",
"\n",
@@ -69,7 +69,7 @@
"source": [
"# Inductive Logic Programming (FOIL)\n",
"\n",
- "Inductive logic programming (ILP) combines inductive methods with the power of first-order representations, concentrating in particular on the representation of hypotheses as logic programs. The general knowledge-based induction problem is to solve the entailment constrant:
\n",
+ "Inductive logic programming (ILP) combines inductive methods with the power of first-order representations, concentrating in particular on the representation of hypotheses as logic programs. The general knowledge-based induction problem is to solve the entailment constraint:
\n",
"$ Background ∧ Hypothesis ∧ Descriptions \\vDash Classifications $\n",
"\n",
"for the __unknown__ $Hypothesis$, given the $Background$ knowledge described by $Descriptions$ and $Classifications$.\n",
@@ -83,13 +83,13 @@
"use first-order literals instead of attributes, and the $Hypothesis$ is a set of clauses (set of first order rules, where each rule is similar to a Horn clause) instead of a decision tree.
\n",
"\n",
"\n",
- "The FOIL algorithm learns new rules, one at a time, in order to cover all given possitive and negative examples.
\n",
+ "The FOIL algorithm learns new rules, one at a time, in order to cover all given positive and negative examples.
\n",
"More precicely, FOIL contains an inner and an outer while loop.
\n",
- "- __outer loop__: (function __foil()__) add rules untill all positive examples are covered.
\n",
+ "- __outer loop__: (function __foil()__) add rules until all positive examples are covered.
\n",
" (each rule is a conjuction of literals, which are chosen inside the inner loop)\n",
" \n",
" \n",
- "- __inner loop__: (function __new_clause()__) add new literals untill all negative examples are covered, and some positive examples are covered.
\n",
+ "- __inner loop__: (function __new_clause()__) add new literals until all negative examples are covered, and some positive examples are covered.
\n",
" - In each iteration, we select/add the most promising literal, according to an estimate of its utility. (function __new_literal()__)
\n",
" \n",
" - The evaluation function to estimate utility of adding literal $L$ to a set of rules $R$ is (function __gain()__) : \n",
@@ -102,14 +102,16 @@
" - Calculate the extended examples for the chosen literal (function __extend_example()__)
\n",
" (the set of examples created by extending example with each possible constant value for each new variable in literal)\n",
" \n",
- "- Finally the algorithm returns a disjunction of first order rules (= conjuction of literals)\n",
+ "- Finally, the algorithm returns a disjunction of first order rules (= conjuction of literals)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
- "metadata": {},
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [
{
"data": {
@@ -262,7 +264,6 @@
" share_vars = variables(clause[0])\n",
" for l in clause[1]:\n",
" share_vars.update(variables(l))\n",
- " # creates literals with different order every time \n",
" for pred, arity in self.pred_syms:\n",
" new_vars = {standardize_variables(expr('x')) for _ in range(arity - 1)}\n",
" for args in product(share_vars.union(new_vars), repeat=arity):\n",
@@ -277,23 +278,36 @@
"\n",
" return max(literals, key = partial(self.gain , examples = examples))\n",
"\n",
+ "\n",
" def gain(self, l ,examples):\n",
- " pre_pos= len(examples[0])\n",
- " pre_neg= len(examples[1])\n",
- " extended_examples = [sum([list(self.extend_example(example, l)) for example in examples[i]], []) for i in range(2)]\n",
- " post_pos = len(extended_examples[0]) \n",
- " post_neg = len(extended_examples[1]) \n",
- " if pre_pos + pre_neg ==0 or post_pos + post_neg==0:\n",
+ " """\n",
+ " Find the utility of each literal when added to the body of the clause. \n",
+ " Utility function is: \n",
+ " gain(R, l) = T * (log_2 (post_pos / (post_pos + post_neg)) - log_2 (pre_pos / (pre_pos + pre_neg)))\n",
+ "\n",
+ " where: \n",
+ " \n",
+ " pre_pos = number of possitive bindings of rule R (=current set of rules)\n",
+ " pre_neg = number of negative bindings of rule R \n",
+ " post_pos = number of possitive bindings of rule R' (= R U {l} )\n",
+ " post_neg = number of negative bindings of rule R' \n",
+ " T = number of possitive bindings of rule R that are still covered \n",
+ " after adding literal l \n",
+ "\n",
+ " """\n",
+ " pre_pos = len(examples[0])\n",
+ " pre_neg = len(examples[1])\n",
+ " post_pos = sum([list(self.extend_example(example, l)) for example in examples[0]], []) \n",
+ " post_neg = sum([list(self.extend_example(example, l)) for example in examples[1]], []) \n",
+ " if pre_pos + pre_neg ==0 or len(post_pos) + len(post_neg)==0:\n",
" return -1\n",
" # number of positive example that are represented in extended_examples\n",
" T = 0\n",
" for example in examples[0]:\n",
- " def represents(d):\n",
- " return all(d[x] == example[x] for x in example)\n",
- " if any(represents(l_) for l_ in extended_examples[0]):\n",
+ " represents = lambda d: all(d[x] == example[x] for x in example)\n",
+ " if any(represents(l_) for l_ in post_pos):\n",
" T += 1\n",
- " value = T * (log(post_pos / (post_pos + post_neg) + 1e-12,2) - log(pre_pos / (pre_pos + pre_neg),2))\n",
- " #print (l, value)\n",
+ " value = T * (log(len(post_pos) / (len(post_pos) + len(post_neg)) + 1e-12,2) - log(pre_pos / (pre_pos + pre_neg),2))\n",
" return value\n",
"\n",
"\n",
@@ -302,8 +316,7 @@
" List of omitted examples is returned."""\n",
" uncovered = []\n",
" for example in examples:\n",
- " def represents(d):\n",
- " return all(d[x] == example[x] for x in example)\n",
+ " represents = lambda d: all(d[x] == example[x] for x in example)\n",
" if any(represents(l) for l in extended_examples):\n",
" self.tell(subst(example, target))\n",
" else:\n",
@@ -408,7 +421,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "[[Parent(x, y), [Mother(x, y)]], [Parent(x, y), [Father(x, y)]]]\n"
+ "[[Parent(x, y), [Father(x, y)]], [Parent(x, y), [Mother(x, y)]]]\n"
]
}
],
@@ -430,7 +443,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "Suppose that we have some possitive and negative results for the relation 'GrandParent(x,y)' and we want to find a set of rules that satisfies the examples.
\n",
+ "Suppose that we have some positive and negative results for the relation 'GrandParent(x,y)' and we want to find a set of rules that satisfies the examples.
\n",
"One possible set of rules for the relation $Grandparent(x,y)$ could be:
\n",
"\n",
"
\n",
@@ -448,7 +461,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "[[Grandparent(x, y), [Parent(x, v_5), Parent(v_5, y)]]]\n"
+ "[[Grandparent(x, y), [Parent(x, v_6), Parent(v_6, y)]]]\n"
]
}
],
@@ -610,7 +623,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.5.3"
+ "version": "3.6.5"
}
},
"nbformat": 4,
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: