Update README.md, index.ipynb and delete tensorflow_graph_in_jupyter.py

main
Aurélien Geron 2019-01-08 12:22:09 +08:00
parent 02763e43b9
commit 23a9498e94
3 changed files with 24 additions and 74 deletions

View File

@ -2,27 +2,29 @@ Machine Learning Notebooks
========================== ==========================
This project aims at teaching you the fundamentals of Machine Learning in This project aims at teaching you the fundamentals of Machine Learning in
python. It contains the example code and solutions to the exercises in my O'Reilly book [Hands-on Machine Learning with Scikit-Learn and TensorFlow](http://shop.oreilly.com/product/0636920052289.do): python. It contains the example code and solutions to the exercises in the second edition of my O'Reilly book [Hands-on Machine Learning with Scikit-Learn, Keras and TensorFlow](https://www.oreilly.com/library/view/hands-on-machine-learning/9781492032632/):
[![book](http://akamaicovers.oreilly.com/images/0636920052289/cat.gif)](http://shop.oreilly.com/product/0636920052289.do) <img src="https://images-na.ssl-images-amazon.com/images/I/51tOhPQBmSL._SX379_BO1,204,203,200_.jpg" title="book" width="150" />
**Note**: If you are looking for the first edition notebooks, check out [ageron/handson-ml](https://github.com/ageron/handson-ml).
Simply open the [Jupyter](http://jupyter.org/) notebooks you are interested in: Simply open the [Jupyter](http://jupyter.org/) notebooks you are interested in:
* Using [jupyter.org's notebook viewer](http://nbviewer.jupyter.org/github/ageron/handson-ml/blob/master/index.ipynb) * Using [jupyter.org's notebook viewer](http://nbviewer.jupyter.org/github/ageron/handson-ml2/blob/master/index.ipynb)
* note: [github.com's notebook viewer](https://github.com/ageron/handson-ml/blob/master/index.ipynb) also works but it is slower and the math formulas are not displayed correctly, * note: [github.com's notebook viewer](https://github.com/ageron/handson-ml2/blob/master/index.ipynb) also works but it is slower and the math formulas are not displayed correctly,
* or by cloning this repository and running Jupyter locally. This option lets you play around with the code. In this case, follow the installation instructions below. * or by cloning this repository and running Jupyter locally. This option lets you play around with the code. In this case, follow the installation instructions below.
# Installation # Installation
First, you will need to install [git](https://git-scm.com/), if you don't have it already. First, you will need to install [git](https://git-scm.com/), if you don't have it already.
Next, clone this repository by opening a terminal and typing the following commands: Next, clone this repository by opening a terminal and typing the following commands (do not type the `$` signs, they just indicate that this is a terminal command):
$ cd $HOME # or any other development directory you prefer $ cd $HOME # or any other development directory you prefer
$ git clone https://github.com/ageron/handson-ml.git $ git clone https://github.com/ageron/handson-ml2.git
$ cd handson-ml $ cd handson-ml2
If you do not want to install git, you can instead download [master.zip](https://github.com/ageron/handson-ml/archive/master.zip), unzip it, rename the resulting directory to `handson-ml` and move it to your development directory. If you do not want to install git, you can instead download [master.zip](https://github.com/ageron/handson-ml2/archive/master.zip), unzip it, rename the resulting directory to `handson-ml2` and move it to your development directory.
If you want to go through chapter 16 on Reinforcement Learning, you will need to [install OpenAI gym](https://gym.openai.com/docs) and its dependencies for Atari simulations. If you want to go through chapter 16 on Reinforcement Learning, you will need to [install OpenAI gym](https://gym.openai.com/docs) and its dependencies for Atari simulations.
@ -34,7 +36,7 @@ Of course, you obviously need Python. Python 2 is already preinstalled on most s
$ python --version # for Python 2 $ python --version # for Python 2
$ python3 --version # for Python 3 $ python3 --version # for Python 3
Any Python 3 version should be fine, preferably 3.5 or 3.6 (TensorFlow support for Python 3.7 is [coming soon](https://github.com/tensorflow/tensorflow/issues/20517)). If you don't have Python 3, I recommend installing it (Python ≥2.6 should work, but it is deprecated so Python 3 is preferable). To do so, you have several options: on Windows or MacOSX, you can just download it from [python.org](https://www.python.org/downloads/). On MacOSX, you can alternatively use [MacPorts](https://www.macports.org/) or [Homebrew](https://brew.sh/). If you are using Python 3.6 on MacOSX, you need to run the following command to install the `certifi` package of certificates because Python 3.6 on MacOSX has no certificates to validate SSL connections (see this [StackOverflow question](https://stackoverflow.com/questions/27835619/urllib-and-ssl-certificate-verify-failed-error)): Any Python 3 version should be fine, preferably 3.5 or 3.6 (TensorFlow support for Python 3.7 is [coming soon](https://github.com/tensorflow/tensorflow/issues/20517)). If you don't have Python 3, I strongly recommend installing it (Python ≥2.6 may work with minor adjustments, but it is deprecated so Python 3 is preferable). To do so, you have several options: on Windows or MacOSX, you can just download it from [python.org](https://www.python.org/downloads/). On MacOSX, you can alternatively use [MacPorts](https://www.macports.org/) or [Homebrew](https://brew.sh/). If you are using Python 3.6 on MacOSX, you need to run the following command to install the `certifi` package of certificates because Python 3.6 on MacOSX has no certificates to validate SSL connections (see this [StackOverflow question](https://stackoverflow.com/questions/27835619/urllib-and-ssl-certificate-verify-failed-error)):
$ /Applications/Python\ 3.6/Install\ Certificates.command $ /Applications/Python\ 3.6/Install\ Certificates.command
@ -48,12 +50,15 @@ Another option is to download and install [Anaconda](https://www.continuum.io/do
If you choose to use Anaconda, read the next section, or else jump to the [Using pip](#using-pip) section. If you choose to use Anaconda, read the next section, or else jump to the [Using pip](#using-pip) section.
## Using Anaconda ## Using Anaconda
**Warning**: this section does not work yet, since TensorFlow 2.0 is not yet available Anaconda repositories.
When using Anaconda, you can optionally create an isolated Python environment dedicated to this project. This is recommended as it makes it possible to have a different environment for each project (e.g. one for this project), with potentially different libraries and library versions: When using Anaconda, you can optionally create an isolated Python environment dedicated to this project. This is recommended as it makes it possible to have a different environment for each project (e.g. one for this project), with potentially different libraries and library versions:
$ conda create -n mlbook python=3.5 anaconda $ conda create -n mlbook python=3.6 anaconda
$ conda activate mlbook $ conda activate mlbook
This creates a fresh Python 3.5 environment called `mlbook` (you can change the name if you want to), and it activates it. This environment contains all the scientific libraries that come with Anaconda. This includes all the libraries we will need (NumPy, Matplotlib, Pandas, Jupyter and a few others), except for TensorFlow, so let's install it: This creates a fresh Python 3.6 environment called `mlbook` (you can change the name if you want to), and it activates it. This environment contains all the scientific libraries that come with Anaconda. This includes all the libraries we will need (NumPy, Matplotlib, Pandas, Jupyter and a few others), except for TensorFlow, so let's install it:
$ conda install -n mlbook -c conda-forge tensorflow $ conda install -n mlbook -c conda-forge tensorflow
@ -66,6 +71,9 @@ Next, you can optionally install Jupyter extensions. These are useful to have ni
You are all set! Next, jump to the [Starting Jupyter](#starting-jupyter) section. You are all set! Next, jump to the [Starting Jupyter](#starting-jupyter) section.
## Using pip ## Using pip
**Warning**: this will work within a few days, when TensorFlow 2.0-preview is released.
If you are not using Anaconda, you need to install several scientific Python libraries that are necessary for this project, in particular NumPy, Matplotlib, Pandas, Jupyter and TensorFlow (and a few others). For this, you can either use Python's integrated packaging system, pip, or you may prefer to use your system's own packaging system (if available, e.g. on Linux, or on MacOSX when using MacPorts or Homebrew). The advantage of using pip is that it is easy to create multiple isolated Python environments with different libraries and different library versions (e.g. one environment for each project). The advantage of using your system's packaging system is that there is less risk of having conflicts between your Python libraries and your system's other packages. Since I have many projects with different library requirements, I prefer to use pip with isolated environments. Moreover, the pip packages are usually the most recent ones available, while Anaconda and system packages often lag behind a bit. If you are not using Anaconda, you need to install several scientific Python libraries that are necessary for this project, in particular NumPy, Matplotlib, Pandas, Jupyter and TensorFlow (and a few others). For this, you can either use Python's integrated packaging system, pip, or you may prefer to use your system's own packaging system (if available, e.g. on Linux, or on MacOSX when using MacPorts or Homebrew). The advantage of using pip is that it is easy to create multiple isolated Python environments with different libraries and different library versions (e.g. one environment for each project). The advantage of using your system's packaging system is that there is less risk of having conflicts between your Python libraries and your system's other packages. Since I have many projects with different library requirements, I prefer to use pip with isolated environments. Moreover, the pip packages are usually the most recent ones available, while Anaconda and system packages often lag behind a bit.
These are the commands you need to type in a terminal if you want to use pip to install the required libraries. Note: in all the following commands, if you chose to use Python 2 rather than Python 3, you must replace `pip3` with `pip`, and `python3` with `python`. These are the commands you need to type in a terminal if you want to use pip to install the required libraries. Note: in all the following commands, if you chose to use Python 2 rather than Python 3, you must replace `pip3` with `pip`, and `python3` with `python`.
@ -117,4 +125,4 @@ Note: you can also visit [http://localhost:8888/nbextensions](http://localhost:8
Congrats! You are ready to learn Machine Learning, hands on! Congrats! You are ready to learn Machine Learning, hands on!
# Contributors # Contributors
I would like to thank everyone who contributed to this project, either by providing useful feedback, filing issues or submitting Pull Requests. Special thanks go to Steven Bunkley and Ziembla who created the `docker` directory. I would like to thank everyone who contributed to this project, either by providing useful feedback, filing issues or submitting Pull Requests. Special thanks go to Haesun Park who helped on some of the exercise solutions, and to Steven Bunkley and Ziembla who created the `docker` directory.

View File

@ -18,15 +18,9 @@
"5. [Support Vector Machines](05_support_vector_machines.ipynb)\n", "5. [Support Vector Machines](05_support_vector_machines.ipynb)\n",
"6. [Decision Trees](06_decision_trees.ipynb)\n", "6. [Decision Trees](06_decision_trees.ipynb)\n",
"7. [Ensemble Learning and Random Forests](07_ensemble_learning_and_random_forests.ipynb)\n", "7. [Ensemble Learning and Random Forests](07_ensemble_learning_and_random_forests.ipynb)\n",
"8. [Dimensionality Reduction & Unsupervised Learning](08_dimensionality_reduction.ipynb)\n", "8. [Unsupervised Learning](08_unsupervised_learning.ipynb)\n",
"9. [Up and running with TensorFlow](09_up_and_running_with_tensorflow.ipynb)\n", "\n",
"10. [Introduction to Artificial Neural Networks](10_introduction_to_artificial_neural_networks.ipynb)\n", "Chapters 9 to 16 are in progress.\n",
"11. [Deep Learning](11_deep_learning.ipynb)\n",
"12. [Distributed TensorFlow](12_distributed_tensorflow.ipynb)\n",
"13. [Convolutional Neural Networks](13_convolutional_neural_networks.ipynb)\n",
"14. [Recurrent Neural Networks](14_recurrent_neural_networks.ipynb)\n",
"15. [Autoencoders](15_autoencoders.ipynb)\n",
"16. [Reinforcement Learning](16_reinforcement_learning.ipynb)\n",
"\n", "\n",
"## Scientific Python tutorials\n", "## Scientific Python tutorials\n",
"* [NumPy](tools_numpy.ipynb)\n", "* [NumPy](tools_numpy.ipynb)\n",
@ -35,11 +29,9 @@
"\n", "\n",
"## Math Tutorials\n", "## Math Tutorials\n",
"* [Linear Algebra](math_linear_algebra.ipynb)\n", "* [Linear Algebra](math_linear_algebra.ipynb)\n",
"* Calculus (coming soon)\n",
"\n", "\n",
"## Extra Material\n", "## Extra Material\n",
"* [Capsule Networks](extra_capsnets.ipynb)\n", "Work in progress\n",
"* [TensorFlow Reproducibility](extra_tensorflow_reproducibility.ipynb)\n",
"\n", "\n",
"## Misc.\n", "## Misc.\n",
"* [Equations](book_equations.ipynb) (list of equations in the book)\n" "* [Equations](book_equations.ipynb) (list of equations in the book)\n"

View File

@ -1,50 +0,0 @@
from __future__ import absolute_import, division, print_function, unicode_literals
# This module defines the show_graph() function to visualize a TensorFlow graph within Jupyter.
# As far as I can tell, this code was originally written by Alex Mordvintsev at:
# https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/deepdream/deepdream.ipynb
# The original code only worked on Chrome (because of the use of <link rel="import"...>, but the version below
# uses Polyfill (copied from this StackOverflow answer: https://stackoverflow.com/a/41463991/38626)
# so that it can work on other browsers as well.
import numpy as np
import tensorflow as tf
from IPython.display import clear_output, Image, display, HTML
def strip_consts(graph_def, max_const_size=32):
"""Strip large constant values from graph_def."""
strip_def = tf.GraphDef()
for n0 in graph_def.node:
n = strip_def.node.add()
n.MergeFrom(n0)
if n.op == 'Const':
tensor = n.attr['value'].tensor
size = len(tensor.tensor_content)
if size > max_const_size:
tensor.tensor_content = b"<stripped %d bytes>"%size
return strip_def
def show_graph(graph_def, max_const_size=32):
"""Visualize TensorFlow graph."""
if hasattr(graph_def, 'as_graph_def'):
graph_def = graph_def.as_graph_def()
strip_def = strip_consts(graph_def, max_const_size=max_const_size)
code = """
<script src="//cdnjs.cloudflare.com/ajax/libs/polymer/0.3.3/platform.js"></script>
<script>
function load() {{
document.getElementById("{id}").pbtxt = {data};
}}
</script>
<link rel="import" href="https://tensorboard.appspot.com/tf-graph-basic.build.html" onload=load()>
<div style="height:600px">
<tf-graph-basic id="{id}"></tf-graph-basic>
</div>
""".format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))
iframe = """
<iframe seamless style="width:1200px;height:620px;border:0" srcdoc="{}"></iframe>
""".format(code.replace('"', '&quot;'))
display(HTML(iframe))