mirror of
https://github.com/RustyRaptor/Python-Machine-Learning-Second-Edition.git
synced 2026-12-03 16:05:20 -07:00
commit
This commit is contained in:
1 parent
1cf55acd75
commit
96f3e58e99
51 files changed
+1120
-1053
No files matched your search
@@ -0,0 +1,52 @@
|
||||
Python Machine Learning - Code Examples
|
||||
|
||||
|
||||
## Chapter 15 - Classifying Images with Deep Convolutional Neural Networks
|
||||
|
||||
### Chapter Outline
|
||||
|
||||
- Building blocks of convolutional neural networks
|
||||
- Understanding CNNs and learning feature hierarchies
|
||||
- Performing discrete convolutions
|
||||
- Performing a discrete convolution in one dimension
|
||||
- The effect of zero-padding in a convolution
|
||||
- Determining the size of the convolution output
|
||||
- Performing a discrete convolution in 2D
|
||||
- Subsampling
|
||||
- Putting everything together to build a CNN 508
|
||||
- Working with multiple input or color channels 508
|
||||
- Regularizing a neural network with dropout 512
|
||||
- Implementing a deep convolutional neural network using TensorFlow
|
||||
- The multilayer CNN architecture 514 Loading and preprocessing the data
|
||||
- Implementing a CNN in the TensorFlow low-level API
|
||||
- Implementing a CNN in the TensorFlow layers API
|
||||
- Summary
|
||||
|
||||
### A note on using the code examples
|
||||
|
||||
The recommended way to interact with the code examples in this book is via Jupyter Notebook (the `.ipynb` files). Using Jupyter Notebook, you will be able to execute the code step by step and have all the resulting outputs (including plots and images) all in one convenient document.
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
Setting up Jupyter Notebook is really easy: if you are using the Anaconda Python distribution, all you need to install jupyter notebook is to execute the following command in your terminal:
|
||||
|
||||
conda install jupyter notebook
|
||||
|
||||
Then you can launch jupyter notebook by executing
|
||||
|
||||
jupyter notebook
|
||||
|
||||
A window will open up in your browser, which you can then use to navigate to the target directory that contains the `.ipynb` file you wish to open.
|
||||
|
||||
**More installation and setup instructions can be found in the [README.md file of Chapter 1](../ch01/README.md)**.
|
||||
|
||||
**(Even if you decide not to install Jupyter Notebook, note that you can also view the notebook files on GitHub by simply clicking on them: [`ch15.ipynb`](ch15.ipynb))**
|
||||
|
||||
In addition to the code examples, I added a table of contents to each Jupyter notebook as well as section headers that are consistent with the content of the book. Also, I included the original images and figures in hope that these make it easier to navigate and work with the code interactively as you are reading the book.
|
||||
|
||||

|
||||
|
||||
|
||||
When I was creating these notebooks, I was hoping to make your reading (and coding) experience as convenient as possible! However, if you don't wish to use Jupyter Notebooks, I also converted these notebooks to regular Python script files (`.py` files) that can be viewed and edited in any plaintext editor.
|
||||
@@ -1787,15 +1787,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[NbConvertApp] Converting notebook ch15.ipynb to script\n",
|
||||
"[NbConvertApp] Writing 28884 bytes to ch15.py\n"
|
||||
"[NbConvertApp] Converting notebook ch15.ipynb to script\r\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
+19
-12
@@ -1,6 +1,3 @@
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
|
||||
# coding: utf-8
|
||||
|
||||
@@ -19,6 +16,8 @@ from __future__ import print_function
|
||||
# In[1]:
|
||||
|
||||
|
||||
get_ipython().magic('load_ext watermark')
|
||||
get_ipython().magic("watermark -a 'Sebastian Raschka & Vahid Mirjalili' -d -p numpy,scipy,tensorflow")
|
||||
|
||||
|
||||
# *The use of `watermark` is optional. You can install this IPython extension via "`pip install watermark`". For more information, please see*: https://github.com/rasbt/watermark.
|
||||
@@ -43,12 +42,14 @@ from __future__ import print_function
|
||||
|
||||
|
||||
from IPython.display import Image
|
||||
get_ipython().magic('matplotlib inline')
|
||||
|
||||
|
||||
# In[3]:
|
||||
|
||||
|
||||
# This is for Python 2.7 compatibility
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
# # Building blocks of convolutional neural networks
|
||||
@@ -58,6 +59,7 @@ from IPython.display import Image
|
||||
# In[4]:
|
||||
|
||||
|
||||
Image(filename='images/15_01.png', width=700)
|
||||
|
||||
|
||||
# ## Performing discrete convolutions
|
||||
@@ -67,11 +69,13 @@ from IPython.display import Image
|
||||
# In[5]:
|
||||
|
||||
|
||||
Image(filename='images/15_02.png', width=700)
|
||||
|
||||
|
||||
# In[6]:
|
||||
|
||||
|
||||
Image(filename='images/15_03.png', width=700)
|
||||
|
||||
|
||||
# ### The effect of zero-padding in convolution
|
||||
@@ -79,6 +83,7 @@ from IPython.display import Image
|
||||
# In[7]:
|
||||
|
||||
|
||||
Image(filename='images/15_11.png', width=700)
|
||||
|
||||
|
||||
# ### Determining the size of the convolution output
|
||||
@@ -115,11 +120,13 @@ print('Numpy Results: ',
|
||||
# In[9]:
|
||||
|
||||
|
||||
Image(filename='images/15_04.png', width=700)
|
||||
|
||||
|
||||
# In[10]:
|
||||
|
||||
|
||||
Image(filename='images/15_05.png', width=900)
|
||||
|
||||
|
||||
# In[11]:
|
||||
@@ -163,6 +170,7 @@ print('Scipy Results: \n',
|
||||
# In[12]:
|
||||
|
||||
|
||||
Image(filename='images/15_06.png', width=700)
|
||||
|
||||
|
||||
# # Putting everything together to build a CNN
|
||||
@@ -194,6 +202,7 @@ print(img[100:102, 100:102, :])
|
||||
# In[14]:
|
||||
|
||||
|
||||
Image(filename='images/15_07.png', width=800)
|
||||
|
||||
|
||||
# ## Regularizing a neural network with dropout
|
||||
@@ -201,6 +210,7 @@ print(img[100:102, 100:102, :])
|
||||
# In[15]:
|
||||
|
||||
|
||||
Image(filename='images/15_08.png', width=800)
|
||||
|
||||
|
||||
# # Implementing a deep convolutional neural network using TensorFlow
|
||||
@@ -210,6 +220,7 @@ print(img[100:102, 100:102, :])
|
||||
# In[16]:
|
||||
|
||||
|
||||
Image(filename='images/15_09.png', width=800)
|
||||
|
||||
|
||||
# ## Loading and preprocessing the data
|
||||
@@ -349,7 +360,7 @@ def conv_layer(input_tensor, name,
|
||||
padding=padding_mode)
|
||||
print(conv)
|
||||
conv = tf.nn.bias_add(conv, biases,
|
||||
name='net_pre-activaiton')
|
||||
name='net_pre-activation')
|
||||
print(conv)
|
||||
conv = tf.nn.relu(conv, name='activation')
|
||||
print(conv)
|
||||
@@ -390,7 +401,7 @@ def fc_layer(input_tensor, name,
|
||||
layer = tf.matmul(input_tensor, weights)
|
||||
print(layer)
|
||||
layer = tf.nn.bias_add(layer, biases,
|
||||
name='net_pre-activaiton')
|
||||
name='net_pre-activation')
|
||||
print(layer)
|
||||
if activation_fn is None:
|
||||
return layer
|
||||
@@ -785,6 +796,7 @@ with tf.Session(graph=g) as sess:
|
||||
# In[4]:
|
||||
|
||||
|
||||
Image(filename='images/15_10.png', width=800)
|
||||
|
||||
|
||||
# ## Implementing a CNN in the TensorFlow layers API
|
||||
@@ -1015,13 +1027,8 @@ print('Test Accuracy: %.2f%%' % (100*
|
||||
#
|
||||
# Readers may ignore the next cell.
|
||||
|
||||
# In[ ]:
|
||||
|
||||
|
||||
|
||||
|
||||
# In[ ]:
|
||||
|
||||
# In[1]:
|
||||
|
||||
|
||||
get_ipython().system(' python ../.convert_notebook_to_script.py --input ch15.ipynb --output ch15.py')
|
||||
|
||||
Reference in new issue
Block a user