From ef39aaf4daa76bb63cfb0c041184065e567e2d35 Mon Sep 17 00:00:00 2001 From: Anthony Marakis Date: Sat, 12 Aug 2017 18:29:15 +0300 Subject: [PATCH 1/4] Update knowledge.ipynb --- knowledge.ipynb | 100 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 3 deletions(-) diff --git a/knowledge.ipynb b/knowledge.ipynb index 0155d4f6f..8f844e8c4 100644 --- a/knowledge.ipynb +++ b/knowledge.ipynb @@ -19,7 +19,9 @@ }, "outputs": [], "source": [ - "from knowledge import *" + "from knowledge import *\n", + "\n", + "from notebook import pseudocode" ] }, { @@ -70,7 +72,7 @@ "collapsed": true }, "source": [ - "## [CURRENT-BEST LEARNING](https://github.com/aimacode/aima-pseudocode/blob/master/md/Current-Best-Learning.md)\n", + "## CURRENT-BEST LEARNING\n", "\n", "### Overview\n", "\n", @@ -85,6 +87,54 @@ "When specializing and generalizing, we should take care to not create inconsistencies with previous examples. To avoid that caveat, backtracking is needed. Thankfully, there is not just one specialization or generalization, so we have a lot to choose from. We will go through all the specialization/generalizations and we will refine our hypothesis as the first specialization/generalization consistent with all the examples seen up to that point." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Pseudocode" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/markdown": [ + "### AIMA3e\n", + "__function__ Current-Best-Learning(_examples_, _h_) __returns__ a hypothesis or fail \n", + " __if__ _examples_ is empty __then__ \n", + "   __return__ _h_ \n", + " _e_ ← First(_examples_) \n", + " __if__ _e_ is consistent with _h_ __then__ \n", + "   __return__ Current-Best-Learning(Rest(_examples_), _h_) \n", + " __else if__ _e_ is a false positive for _h_ __then__ \n", + "   __for each__ _h'_ __in__ specializations of _h_ consistent with _examples_ seen so far __do__ \n", + "     _h''_ ← Current-Best-Learning(Rest(_examples_), _h'_) \n", + "     __if__ _h''_ ≠ _fail_ __then return__ _h''_ \n", + " __else if__ _e_ is a false negative for _h_ __then__ \n", + "   __for each__ _h'_ __in__ generalizations of _h_ consistent with _examples_ seen so far __do__ \n", + "     _h''_ ← Current-Best-Learning(Rest(_examples_), _h'_) \n", + "     __if__ _h''_ ≠ _fail_ __then return__ _h''_ \n", + " __return__ _fail_ \n", + "\n", + "---\n", + "__Figure ??__ The current-best-hypothesis learning algorithm. It searches for a consistent hypothesis that fits all the examples and backtracks when no consistent specialization/generalization can be found. To start the algorithm, any hypothesis can be passed in; it will be specialized or generalized as needed." + ], + "text/plain": [ + "" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pseudocode('Current-Best-Learning')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -432,7 +482,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## [VERSION-SPACE LEARNING](https://github.com/aimacode/aima-pseudocode/blob/master/md/Version-Space-Learning.md)\n", + "## VERSION-SPACE LEARNING\n", "\n", "### Overview\n", "\n", @@ -441,6 +491,50 @@ "After we update the set on an example, all the hypotheses in the set are consistent with that example. So, when all the examples have been parsed, all the remaining hypotheses in the set are consistent with all the examples. That means we can pick hypotheses at random and we will always get a valid hypothesis." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Pseudocode" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/markdown": [ + "### AIMA3e\n", + "__function__ Version-Space-Learning(_examples_) __returns__ a version space \n", + " __local variables__: _V_, the version space: the set of all hypotheses \n", + "\n", + " _V_ ← the set of all hypotheses \n", + " __for each__ example _e_ in _examples_ __do__ \n", + "   __if__ _V_ is not empty __then__ _V_ ← Version-Space-Update(_V_, _e_) \n", + " __return__ _V_ \n", + "\n", + "---\n", + "__function__ Version-Space-Update(_V_, _e_) __returns__ an updated version space \n", + " _V_ ← \\{_h_ ∈ _V_ : _h_ is consistent with _e_\\} \n", + "\n", + "---\n", + "__Figure ??__ The version space learning algorithm. It finds a subset of _V_ that is consistent with all the _examples_." + ], + "text/plain": [ + "" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pseudocode('Version-Space-Learning')" + ] + }, { "cell_type": "markdown", "metadata": { From 522bd2d58e6fd6ce774a0627de22ca8e90f7b3b7 Mon Sep 17 00:00:00 2001 From: Anthony Marakis Date: Sat, 12 Aug 2017 18:30:01 +0300 Subject: [PATCH 2/4] Update notebook.py --- notebook.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/notebook.py b/notebook.py index 2df7b7721..432e5acf9 100644 --- a/notebook.py +++ b/notebook.py @@ -2,7 +2,7 @@ from games import TicTacToe, alphabeta_player, random_player, Fig52Extended, infinity from logic import parse_definite_clause, standardize_variables, unify, subst from learning import DataSet -from IPython.display import HTML, Markdown, display +from IPython.display import HTML, display from collections import Counter import matplotlib.pyplot as plt @@ -21,6 +21,18 @@ def psource(*functions): print('\n\n'.join(inspect.getsource(fn) for fn in functions)) + +def pseudocode(algorithm): + from urllib.request import urlopen + from IPython.display import Markdown + + url = "https://raw.githubusercontent.com/aimacode/aima-pseudocode/master/md/{}.md".format(algorithm) + f = urlopen(url) + md = f.read().decode('utf-8') + md = md.split('\n', 1)[-1].strip() + md = '#' + md + return Markdown(md) + # ______________________________________________________________________________ From 8632113a286ffb358c4b1edce37badc6a033df7f Mon Sep 17 00:00:00 2001 From: Anthony Marakis Date: Tue, 15 Aug 2017 14:20:47 +0300 Subject: [PATCH 3/4] Update knowledge.ipynb --- knowledge.ipynb | 91 ++++++++----------------------------------------- 1 file changed, 14 insertions(+), 77 deletions(-) diff --git a/knowledge.ipynb b/knowledge.ipynb index 8f844e8c4..2ffb20362 100644 --- a/knowledge.ipynb +++ b/knowledge.ipynb @@ -21,7 +21,7 @@ "source": [ "from knowledge import *\n", "\n", - "from notebook import pseudocode" + "from notebook import pseudocode, psource" ] }, { @@ -145,40 +145,16 @@ "\n", "We have functions to calculate the list of all specializations/generalizations, to check if an example is consistent/false positive/false negative with a hypothesis. We also have an auxiliary function to add a disjunction (or operation) to a hypothesis, and two other functions to check consistency of all (or just the negative) examples.\n", "\n", - "You can read the source by running the cells below:" + "You can read the source by running the cell below:" ] }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "%psource current_best_learning" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "%psource specializations" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ - "%psource generalizations" + "psource(current_best_learning, specializations, generalizations)" ] }, { @@ -552,68 +528,29 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "%psource version_space_learning" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "%psource version_space_update" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "%psource all_hypotheses" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": true - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ - "%psource values_table" + "psource(version_space_learning, version_space_update)" ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": true - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ - "%psource build_attr_combinations" + "psource(all_hypotheses, values_table)" ] }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ - "%psource build_h_combinations" + "psource(build_attr_combinations, build_h_combinations)" ] }, { From b51593f43428d52488dbeb661a356b3da7a42715 Mon Sep 17 00:00:00 2001 From: Anthony Marakis Date: Tue, 15 Aug 2017 14:22:37 +0300 Subject: [PATCH 4/4] bringing it all together in notebook --- notebook.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/notebook.py b/notebook.py index 432e5acf9..b1e024f60 100644 --- a/notebook.py +++ b/notebook.py @@ -1,3 +1,5 @@ +from inspect import getsource + from utils import argmax, argmin from games import TicTacToe, alphabeta_player, random_player, Fig52Extended, infinity from logic import parse_definite_clause, standardize_variables, unify, subst @@ -15,14 +17,8 @@ #______________________________________________________________________________ -def psource(*functions): - """Print the source code for the given function(s).""" - import inspect - - print('\n\n'.join(inspect.getsource(fn) for fn in functions)) - - def pseudocode(algorithm): + """Print the pseudocode for the given algorithm.""" from urllib.request import urlopen from IPython.display import Markdown @@ -33,6 +29,21 @@ def pseudocode(algorithm): md = '#' + md return Markdown(md) + +def psource(*functions): + """Print the source code for the given function(s).""" + source_code = '\n\n'.join(getsource(fn) for fn in functions) + try: + from pygments.formatters import HtmlFormatter + from pygments.lexers import PythonLexer + from pygments import highlight + + display(HTML(highlight(source_code, PythonLexer(), HtmlFormatter(full=True)))) + + except ImportError: + print(source_code) + + # ______________________________________________________________________________ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy